中易网

java spring 里的helloworld我也遇到问题了,请帮我指正是哪里的问题可以吗?

答案:2  悬赏:20  
解决时间 2021-02-22 16:26
我的问题叙述:我构建了简单的文件,也使用了规范的get 和set方法,在配置文件里也注明了变量的名字,可是意外的出错了。我使用的spring版本是4.0.7
请问可能的问题是什么呢?我应该如何解决这个问题?

首先,我的class是这样的一个简单的class

public class Restaruant {

private String restName;

public void greetCustomer() {

if(restName == null) {

System.out.println("Wellcome to our restaruant!");

} else {

System.out.println(restName + ": Wellcome to our restaruant!");

}

}

private String getRestName() {

return restName;

}

private void setRestName(String myName) {

this.restName = restName;

}

}

然后我在配置文件里写了




xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">









最后我的主程序是这样运行的
public class TestSpringObject {

public static void main(String[] args) {

ApplicationContext context =

new ClassPathXmlApplicationContext("SpringConfig.xml");

Restaruant restaruantObjRestaruant = (Restaruant) context.getBean("restaruantBean");

restaruantObjRestaruant.greetCustomer();

}

}

得到的问题是这样的:

Jul 17, 2014 3:36:58 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh

INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@61e4705b: startup date [Thu Jul 17 15:36:58 CST 2014]; root of context hierarchy

Jul 17, 2014 3:36:58 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

INFO: Loading XML bean definitions from class path resource [SpringConfig.xml]

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restaruantBean' defined in class path resource [SpringConfig.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'restName' of bean class [main.Restaruant]: Bean property 'restName' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

at

... 13 more

问题已经解决了, 是我的get和set方法是private的问题。在解决问后,新的问题出现了。打印出来的结果如下:
Wellcome to our restaruant!

我不能理解为什么。应该是要打印我给它注入的Pete的问候才对的呀。

刚刚看了一下get 和 set的parameter,发现了我写的粗心了。问题已经解决了,真是抱歉,大侠们辛苦了。
最佳答案
你没有myName 这个属性
全部回答
java -cp ./ hello
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
she runs very fart改为感叹句
广厦居建材在哪里啊,我有事要去这个地方
(a*a*a-a*a-a-1)如何分解因式
女孩子都不喜欢找个没主见什么都听你说的男朋
山西太原南宫古玩市场第327号地摊,是在哪个位
回河口村地址有知道的么?有点事想过去
梦到家里养了两只白色小猫咪,逗一只猫咪的时
日本提款机可以一次性提取100万日元吗
到底是该买领动还是该买新明锐
龙应台有几姐妹呢?没兄弟吗
晨源里·鹭港(南门)怎么去啊,有知道地址的么
为什么cs35从外面锁门有个灯亮
怎么才可以判断自己的JDK已经完全安装成功了
加油脱盐乳清粉中有反式脂肪酸吗
办公室用桶装水好还是净水器好
推荐资讯
乌龟孵蛋时用什么沙
将一根木头的一端搁在地上,另一端始终用竖直
买了德国白金pre版的爱他美,不知道真假,看
魔兽世界小宠物
金玉手机卖店地址在哪,我要去那里办事
女孩子偷偷在男生头上揪头发(一次揪几根)是
如果没有俄语专业四级证书影响毕业么?
正宗德州扒鸡在什么地方啊,我要过去处理事情
牧场物语里月见草有什么用
中闽百汇泉秀商场我想知道这个在什么地方
读《叮叮河流旅行记》有感
锦泰亨馍馍哥火车站旗舰店在什么地方啊,我要
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?