This is my app.xml :
<context:component-scan base-package="destiny.web" />
<context:annotation-config/>
And there is a Dao
(interface) , and DaoImpl
(annotated with @Repository
) inside destiny.web package.
There is a开发者_如何学Cnother Spring3's destiny.web.AppConfig class :
@Configuration
public class AppConfig
{
@Inject
private Dao daoImpl
public AppConfig()
{
System.out.println("dao = " + daoImpl);
}
}
It prints 'null' , why ?
I am sure all these beans/configuration/repositories are scanned. But it seems @Configuration doesn't know other scanned beans . Did I miss anything ?
I try to solve it by @ImportResource :
@Configuration
@ImportResource("classpath:app.xml")
public class AppConfig
But it seems causing cyclic bean scan and throws this exception :
{main} org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Only one AsyncAnnotationBeanPostProcessor may exist within the context.
Offending resource: class path resource [app.xml]
How to solve it ?
Thanks.
Spring will invoke constructor firstly
before inject / autowiring
the other component. therefore your dao is null while you print at the constructor, because the dao still not injected yet
.
Have a try to create test application for your configapp.
public class Main {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("stackoverflow.xml");
AppConfig appConfig = context.getBean(AppConfig.class);
appConfig.getConfig("smtp.host");
}
}
have you tried it also with the annotation @Autowired
instead of @Inject
?
精彩评论