Java – 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" 

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):


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?

Best Solution

Is missing <context:annotation-config/>?

<context:component-scan base-package="package1"/>
<context:component-scan base-package="package2"/>
<tx:annotation-driven transaction-manager="transactionManager" />


<context:component-scan base-package="package1, package2"/>
<tx:annotation-driven transaction-manager="transactionManager" />