请问可能的问题是什么呢?我应该如何解决这个问题?
首先,我的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,发现了我写的粗心了。问题已经解决了,真是抱歉,大侠们辛苦了。