Component Scan not finding @Component's in a JAR in Tomcat webapp
Asked Answered
C

2

7

I just filed a bug in the Spring bugsystem ( https://jira.springsource.org/browse/SPR-8551 ), but I am still unsure if I am missing something

I tracked down a problem with <context:component-scan/> to this statement. Given the two following classes which are in the same JAR in WEB-INF/lib of a web application (The JAR file has the directory structure):

test/TheBean.java:

package test;
@Component
public class TheBean{
}

test/BeanSearcher.java:

package test;
public class BeanSearcher{

  public void init(){ 
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); 
    ctx.scan("test"); 
    ctx.refresh(); 
    TheBean b=  ctx.getBean(TheBean.class); 
    // What is the value of b? 
  }
}

If I run new BeanSearcher().init() in a jUnit test case or other type of standalone application, b is getting assigned an instance of TheBean, but if I run it, say, in a JSP, ctx.getBean() is returning null.

So, am I doing something wrong or not taking something into account, is this just a bug...?

EDIT 8/8/2011: It seems to work good as I tried to simplify the problem, but still, when I try to make it work, in the initialization of OpenCms, it fails. Now I am trying to look for the differences between working versions and the one which doesn't work. (Classloader, ubication of the relevant classes in different JARs or directly in WEB-INF/classes, calls via reflection, etc.)

Carrack answered 21/7, 2011 at 9:23 Comment(3)
Are the Spring libraries in the webapp or are they installed directly into the Tomcat main lib dir? I've seen screwy problems when the latter is true…Terrain
The Spring libraries are as well in WEB-INF/libCarrack
It could be related to this question: #1243156Carrack
C
10

As I wrote in the comment, the solution is given by the answer here: Spring Annotation-based controllers not working if it is inside jar file

When you export the jar file using the export utility in eclipse there is a option called Add directory entries.

Carrack answered 24/12, 2012 at 16:46 Comment(1)
Thanks a bunch. I dont know what what it does (Add directory entries), but spring is now able to scan my components in linux enviroment!Selaginella
T
2

The obvious question is whether you have things like these in your web.xml:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/foo.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

Without these, Spring won't actually load at all, let alone properly build beans…

Terrain answered 22/7, 2011 at 13:12 Comment(1)
I have, consciously, not added this configuration to the web.xml file as I expect everything to be capsuled in my JAR. As you can see, I am using an AnnotationConfigApplicationContext which does not require a context XML file. Or do I need it anyhow? Thanks!Carrack

© 2022 - 2024 — McMap. All rights reserved.