No bean named '...' is defined and Spring @Resource annotation
I use @Resource to annotate bean classes, @Autowired to autowire dependencies, and in Spring configuration file these things:
context:component-scan base-package="package1,package2" tx:annotation-driven
So, it works fine (tested). Spring scans package1, package2, classes with @Resource annotation and then I can get them using getBean() IF TESTED FROM CONSOLE APPLICATION [say, with main() function].
But when I try to use next approach (to use Spring in container managed environment = with Tomcat):
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:beans.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.ContextLoader开发者_如何转开发Listener</listener-class> </listener>
compile a jar with all the bean classes and put this jar into WEB-INF/lib
then what I see? I cannot getBean() any of those @Resource annotated beans!
Spring simply cannot find them. Still I can getBean() beans that are explicitly present in beans.xml. Where's the problem?Is missing <context:annotation-config/>
?
<context:annotation-config/>
<context:component-scan base-package="package1"/>
<context:component-scan base-package="package2"/>
<tx:annotation-driven transaction-manager="transactionManager" />
or
<context:annotation-config/>
<context:component-scan base-package="package1, package2"/>
<tx:annotation-driven transaction-manager="transactionManager" />
I'm not sure how it's working in standalone mode, but the "" element in your Spring context allows the use "@Resource" annotations. Look at the Spring doc for more information.
精彩评论