我没有编写 setter getter 并使用 autowire 而不是得到以下错误 -
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'aS400Statusfacade' defined in class path resource [CXF-Bean.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'aS400StatusDAO' of bean class [com.metlife.mymet.admin.facade.AS400Statusfacade]: Bean property 'aS400StatusDAO' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1279)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(AccessController.java:250)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
我的Spring-conf.xml
<bean id="aS400Statusfacade"
class="com.metlife.mymet.admin.facade.AS400Statusfacade">
<property name="aS400StatusDAO" ref="aS400StatusDAO"></property>
</bean>
我的 Autowiring 类(class)-
public class AS400Statusfacade {
Logger logger=Logger.getLogger(AS400Statusfacade.class);
@Autowired
AS400StatusDAO aS400StatusDAO=null;
public String getAS400StatusInformation() {
return aS400StatusDAO.getAS400Status();
}
}
最佳答案
我认为您遇到的问题是,如果没有 setter 和显式标记,Spring 不知道您想要从中得到什么。
尝试删除显式属性元素。
这将允许 @Autowired 注释尝试解析该值。
进一步说明这一点:
spring-conf.xml
<bean id="aS400Statusfacade"
class="com.metlife.mymet.admin.facade.AS400Statusfacade">
<!--
The following property tag is not necessary. Spring's
@Autowired and associated bean post-processor, will introspect
and set the property with an appropriate candidate bean.
Bean property setter, is not required for the post-processor
to access/set the property.
-->
<!--
<property name="aS400StatusDAO" ref="aS400StatusDAO"></property>
-->
</bean>
AS400Status外观图
public class AS400Statusfacade {
Logger logger=Logger.getLogger(AS400Statusfacade.class);
@Autowired
AS400StatusDAO aS400StatusDAO=null;
public String getAS400StatusInformation() {
return aS400StatusDAO.getAS400Status();
}
}
关于java - org.springframework.beans.factory.BeanCreationException : Error creating bean with name XXX defined in class path resource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32089164/