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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
AdrianRM
  • 2,622
  • 2
  • 25
  • 42
  • 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… – Donal Fellows Jul 21 '11 at 12:39
  • The Spring libraries are as well in WEB-INF/lib – AdrianRM Jul 22 '11 at 13:04
  • It could be related to this question: http://stackoverflow.com/questions/1242656/spring-annotation-based-controllers-not-working-if-it-is-inside-jar-file – AdrianRM Dec 14 '12 at 02:13

2 Answers2

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.

Community
  • 1
  • 1
AdrianRM
  • 2,622
  • 2
  • 25
  • 42
  • 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! –  Nov 12 '15 at 03:50
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…

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • 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! – AdrianRM Jul 22 '11 at 13:17