Spring autowire stops working for classes in Tomcat classpath

Inside my FooLibrary.jar Spring library, a class com.example.FooImpl is created that has the bars property. I have many instances of Bar also created by Spring. The set of bars is auto-incremented in FooImpl as follows:

 @Component public class FooImpl { @Inject private Set<Bar> bars; 

In a stand-alone application, Spring creates Bar instances, creates FooImpl instances, and then autwires FooImpl.bars with a set of Bar s. He works.

Now I am running the same Spring configuration in webapp inside Tomcat. FooLibrary.jar is inside WEB-INF/lib , and everything continues to work as described above.

The problem is that the web application automatically compiles somes classes using the JavaCompiler , which cannot find their dependencies for dynamic compilation unless I put this library on the Tomcat launch path. The minute I add FooLibrary.jar to the Tomcat path (for example, in the Tomcat startup configuration inside Eclipse, or I assume that startup.sh or setclasspath.bat if you use stand-alone Tomcat), autowire stops working.

That is, when my webapp starts, Spring creates all instances of Bar , and then creates an instance of FooImpl , but never auto-magnifies the set of Bar in FooImpl.bars . Any idea why?

(This is because the Spring ContextLoaderListener launched from the webapp class loader, but the instances of FooImpl and Bar coming from the Tomcat class loader are interesting?)

0
source share
1 answer

Auto-reporting may fail due to incompatible types (for example, classes loaded into multiple class loaders).

Since you already place the JAR in the tomcat download path, it should also be visible to webapp, without having to place the JAR in WEB-INF / lib.

You can make the dependency scope provided so that Maven does not place it in WEB-INF / lib.

0
source

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


All Articles