Spring component scan for nested jar

13,173

Solution 1

Make sure inner.jar (or whatever you name it to) 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 inner.jar you want to include.

Also make sure you don't get confused between jar, base package and fully-qualified class name. If I have a class com.mycoolcompany.service.Booya inside Blah.jar, typically I just need to do

<context:component-scan base-package="com.mycoolcompany.service.*" />

And ensure Blah.jar is on the parent project's classpath

Solution 2

If you are sure that the jar is under your classpath and still your classs is not autowired.

Are you sure that this classes are annotated properly?? Because what happens when you say component-scan, spring tries to find classes annotated with @Component or subtypes of compnenets like @Service, etc.

My guess is this is your third party dependency which might not have annotated classes. In this case you should define beans manually in application context. HTH

Share:
13,173
mel3kings
Author by

mel3kings

Melchor Tatlonghari: http://www.linkedin.com/pub/melchor-tatlonghari/6a/198/b11 https://github.com/mel3kings https://www.credly.com/users/melchor-tatlonghari/badges https://www.sysdotoutdotprint.com/ https://medium.com/free-code-camp/how-a-stackoverflow-account-can-secure-you-a-seat-at-the-recognised-developer-table-cc782e1c84de https://medium.com/@mckinseydigital/3-mistakes-i-made-learning-the-software-engineering-skillset-and-how-to-avoid-them-8e194ff09b01

Updated on June 05, 2022

Comments

  • mel3kings
    mel3kings almost 2 years

    Basically I have this Outer.jar, declared in it is an application context with a component scan:

    <context:component-scan
            base-package="x.y.z.class" />
    

    However this x.y.z.class is in an inner.jar which is a dependency of outer.jar,

    I'm getting an error that class not found .../Outer.jar/x/y/z/class , how can specify to check in the inner.jar?

    UPDATE: Initialize Application context as:

     org.springframework.context.ApplicationContext ctx = 
                     new ClassPathXmlApplicationContext("applicationContext.xml");
    
    exception: I/O failure during classpath scanning; nested exception is java.io.FileNotFoundException: ..\default\deploy\test.war\WEB-INF\lib\inner.jar\x\y\z
     and it says inner.jar/x/y/z/class not found
    

    Outer.jar has inner.jar as the dependency

  • mel3kings
    mel3kings over 10 years
    ive added the jar file in the maven, however it still returns file not found. I initialize the application context through: org.springframework.context.ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); and it says inner.jar/x/y/z/class not found
  • gerrytan
    gerrytan over 10 years
    Updated my answer to with additional info about jar, package and class
  • gerrytan
    gerrytan over 10 years
    @melt321 can you elaborate how do you ensure jars are present in the classpath and the correct class package name is setup for component scanning?
  • mel3kings
    mel3kings over 10 years
    forgot to setup the web.xml for spring context loader listener. is now working. Thanks!