Component scan does not find @ Component in JAR in Tomcat webapp

I just logged a bug in Spring bugsystem ( https://jira.springsource.org/browse/SPR-8551 ), but I'm still not sure if I missed something

I found a problem with <context:component-scan/> in this statement. Given the following two classes that are in the same JAR in the WEB-INF / lib web application (the JAR file has a 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 in another type of standalone application, b gets the assigned instance of TheBean, but if I run it, say in JSP, ctx.getBean() returns null.

So, I'm doing something wrong or not taking it into account, is this just a mistake ...?

EDIT 8/8/2011: it seems to work well, since I tried to simplify this problem, but still, when I try to get it to work, it fails when initializing OpenCms. Now I am trying to find the differences between working versions and what does not work. (Classloader, assignment of corresponding classes in different JARs or directly in WEB-INF / classes, calls through reflection, etc.)

+6
source share
2 answers

As I wrote in a comment, the answer is given here: Spring Annotation-based controllers do not work if they are inside the jar file

When exporting a jar file using the export utility in eclipse, this is the "Add directory entries" option.

+10
source

The obvious question is: do you have such things 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 them, Spring will not actually boot, not to mention the proper layout of beans ...

+2
source

Source: https://habr.com/ru/post/893249/


All Articles