I have a big problem with @Autowired that always return with exception
Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Property 'serviceFactory' is required for bean 'A'
Here are the brief of everything (I am using Spring 3.2 and I have put all the jar in the right place 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>
Content of Class 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
Content of class com.sc.ServiceFactory
// @Component -- comment out 2014-01-07
public class ServiceFactory
{
// do whatever
}
then I compiled the classes and ran it in jetty and when jetty went up, It always throw up long exception of
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'A' defined in URL ... Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Property 'serviceFactory' is required for bean 'A'
I have try many combination with and without annotation to no avail. It is very frustrating to face this error.
Please help
There are a few things wrong (or rather misunderstood) in your question.
With your current context, you are declaring two bean definitions for each of A
and ServiceFactory
. One implicitly from the @Component
annotation and <component-scan>
and another from the explicit <bean>
declarations. Choose one or the other or you might find yourself in a position where Spring doesn't know which to use.
With the above, what happens is that Spring tries to generate an A
bean because of its @Component
annotation and sees @Required
on the setServiceFactory()
method, but doesn't know what to do with it. @Required
's javadoc states
Marks a method (typically a JavaBean setter method) as being 'required': that is, the setter method must be configured to be dependency-injected with a value.
But in your case, it isn't. Add @Autowired
to it.
@Required
@Autowired
public void setServiceFactory(ServiceFactory serviceFactory) {
this.serviceFactory = serviceFactory;
}
Now Spring will know what to do with that method, ie. inject a ServiceFactory
bean.