No bean named "..." and Spring @Resource annotation

I use @Resource to comment out the bean classes, @Autowired for autowire dependencies, and in my Spring config file do the following:

    context: component-scan base-package = "package1, package2" 
    tx: annotation-driven

So it works great (tested). Spring scans package1, package2, classes with @Resource annotation and then I can get them with getBean () IF TESTED FROM CONSOLE APPLICATION [say with main () function]. In the meantime, there is no need to worry about it. ”

But when I try the following approach (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 bean classes and put this jar in WEB-INF / lib

what i see? I cannot getBean () any of these @Resource annotated beans!
Spring just won't find them.
However, I can getBean () beans that are explicitly present in beans.xml.


Where is the problem?

+2


a source to share


2 answers


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

      

+2


a source


I'm not sure how it works offline, but the "" element in your Spring context allows @Resource annotations. Refer to Spring doc for more information.



0


a source







All Articles