views:

158

answers:

1

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?

A: 

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.

David M. Karr
EugeneP
I said I wasn't sure how it was working standalone because what I saw that was wrong with your configuration wouldn't have resolved itself in a standalone application vs. a web application.I also see that the critical information I tried to provide was elided because I didn't escape the angle brackets. You need to have the "context:annotation-config" element.
David M. Karr