2014-01-06 59 views
0

我有@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”

我已经尝试与不标注无济于事许多组合。面对这个错误是非常令人沮丧的。

请帮

+0

你有你的A级内的服务工厂的getter/setter?你也混合了一些东西。如果使用@Autowired,则不需要在xml中定义beans –

+0

是的,我已经获取并在A类中为serviceFactory设置。 – Spring

+0

我不确定,但删除'autowire =“byName”'看看它是否有效。顺便说一句'@ Required'根本不需要,'@ Autowired'有一个参数,它的默认值是'required = true'。 –

回答

4

有几件事情错了(或者说误解)在你的问题。

使用您当前的上下文,您将为AServiceFactory中的每一个声明两个bean定义。一个隐含地来自@Component注释和<component-scan>,另一个来自明确的<bean>声明。选择一个或另一个,或者你可能会发现自己处于一个Spring不知道使用哪个位置的位置。

上述内容,什么情况是,春尝试生成一个A豆因其@Component注解并在setServiceFactory()方法看到@Required,但不知道该怎么办。 @Required的javadoc的状态

标记的方法(通常是JavaBean setter方法)作为是 ‘必要’:即,setter方法必须被配置为与一个值 依赖性注入。

但在你的情况下,它不是。将@Autowired添加到它。

@Required 
@Autowired 
public void setServiceFactory(ServiceFactory serviceFactory) { 
    this.serviceFactory = serviceFactory; 
} 

现在春天会知道该怎么做,即。注入一个ServiceFactory bean。

+0

我已经决定在类A中使用注释 - 所以我从applicationContext.xml中取出(注释掉)。我从ServiceFactory中删除了@Component,因此ServiceFactory只依赖于applicationContext。我按照你的建议放置Autowired,但它仍然不起作用。 – Spring

+0

@Spring你可以编辑你的问题并添加它现在给你的异常吗? –

+0

@Spring请注意,您已在场上拥有'@ Autowired',您不需要同时使用setter和field。 –

0

感谢您的解决方案。

经过几天的努力,下面的工作对我来说很有用。我试图要么要求或自动装配Autowired按Spring文档,并且它不能正常工作

@Required
@Autowired

相关问题