我有@Autowired一个很大的问题,它始终与异常返回春3自动装配Autowired BeanInitializationException
bean的初始化失败;嵌套的例外是org.springframework.beans.factory.BeanInitializationException:房产“服务工厂”需要豆“A”
这里是一切的短暂(我使用Spring 3.2,我已经把所有的罐子中正确的地方WEB-INF/lib)。
的applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:annotation-config />
<context:component-scan base-package="com.sc" />
<!-- Comment out - we are going to use @Component @Autowired - 2014-01-07
<bean id="A" class="com.sc.A">
<property name="serviceFactory" ref="serviceFactoryBean" />
</bean>
-->
<bean id="serviceFactoryBean" class="com.sc.ServiceFactory" autowire="byName" />
</beans>
类com.sc.ServiceFactory的类com.sc.A
@Component
public class A
{
private Logger logger = Logger.getLogger(A.class);
// @Autowired -- 2014-01-07 comment out as not need it since annotation in setter below
// @Qualifier("serviceFactoryBean")
private ServiceFactory serviceFactory;
public A()
{ }
@Autowired // add 2014-01-07
@Required
public void setServiceFactory(ServiceFactory serviceFactory)
{
this.serviceFactory = serviceFactory;
}
public boolean checkSomething()
{
if(this.serviceFactory == null)
logger.error("serviceFactory is null. Autowired failed");
// do something
}
} // end of class A
内容的内容
// @Component -- comment out 2014-01-07
public class ServiceFactory
{
// do whatever
}
然后我编译的类和在码头跑它,当码头上去时,它总是抛出长长的例外
org.springframework.beans.factory.BeanCreationException:在URL中定义名称为'A'的bean时出错... bean初始化失败;嵌套的例外是org.springframework.beans.factory.BeanInitializationException:房产“服务工厂”需要豆“A”
我已经尝试与不标注无济于事许多组合。面对这个错误是非常令人沮丧的。
请帮
你有你的A级内的服务工厂的getter/setter?你也混合了一些东西。如果使用@Autowired,则不需要在xml中定义beans –
是的,我已经获取并在A类中为serviceFactory设置。 – Spring
我不确定,但删除'autowire =“byName”'看看它是否有效。顺便说一句'@ Required'根本不需要,'@ Autowired'有一个参数,它的默认值是'required = true'。 –