Java – No bean named ‘…’ is defined and Spring @Resource annotation

javaservletsspringtomcat

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.ContextLoaderListener</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?

Best Solution

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" />