2

I have a spring web application which takes in an external jar which contains some annotated controllers in packakge com.x.y.controller.

I have included com.x.y.controller package in my component scan path in spring-servlet.xml

<context:component-scan base-package="com.x.y.controller" />.

But When I deploy the application to my local tomcat server, those controller bean in my external jar does not get created.

However, when I extract the jar into my class path when I build the application(meaning having my package fully expanded into my web application class path) everything seems working fine. It seems weird to me because we also have some service classes with @component annotation in external jar. And these class seems working fine.

Does anyone know if it is expected behavior of spring? Or am I missing some configuration for spring to find those controllers?

Currently during development, if I change a class, changes are reflected to tomcat without rebuilding even if the class is in an external jar. If I need to extract the jar for those controllers to work, tomcat does not seem to take in changes in those controllers even if I restart the server. A rebuild is required to extract the jar again for any change to take effect. This would be very painful for development. I am relative new to spring. Does anybody see a solution for this if I have to go that way?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Xue Bin
  • 21
  • 1
  • 2
  • did you find any solution to this problem. please write an answer if so. I'm having the same issue. – Darshana Jul 25 '17 at 15:33
  • I've solved this problem. I added ${CATALINA_BASE}/conf/Catalina/localhost/${APP_NAME}.xml and in this file added application classloader. In tomcat 8+ this is JarResources property, in tomcat less than 8 version it is Loader property. Here my gist with additional explanation https://gist.github.com/durdyev/542dffb60beb30f5c6ce293418555ef7 – idurdyev Nov 22 '18 at 05:34

1 Answers1

1

Make sure external.jar exist on your classpath (either by adding to maven dependency, eclipse project settings, using -cp jvm command line arguments, etc), and just refer to the package name of the classes inside external .jar you want to include.

And add <context:annotation-config/> to your spring-servlet.xml

CHHIBI AMOR
  • 1,186
  • 2
  • 13
  • 27
  • Hi CHHIBI,The external.jar is there in my tomcat WEB-INF/lib folder. – Xue Bin Dec 24 '14 at 02:11
  • @XueBin It depends how you export the JAR: http://stackoverflow.com/questions/1242656/spring-annotation-based-controllers-not-working-if-it-is-inside-jar-file And Try to add to your spring-servlet.xml – CHHIBI AMOR Dec 24 '14 at 08:34
  • The external.jar is a maven dependency. I have tried adding , but it still doesn't work. Could it be those controllers packaged in jar are not within the web application context? – Xue Bin Dec 24 '14 at 13:01
  • @XueBin add this `context-param` in your `web.xml` ` contextConfigLocation classpath: spring-servlet.xml ` – CHHIBI AMOR Dec 24 '14 at 14:43
  • Hi CHHIBI, thanks for such prompt reply. I had a check on the project. The context-param has been set correctly. spring-servlet.xml has been read. I have other packages defined in it with component-scan and they all works. Just those controllers packaged in another module not working. I think it's related to http://stackoverflow.com/questions/1242656/spring-annotation-based-controllers-not-working-if-it-is-inside-jar-file. – Xue Bin Dec 24 '14 at 15:38