Announcement Announcement Module
No announcement yet.
Import resource from external JAR or classpath Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Import resource from external JAR or classpath


    My issue is this. I want to define some beans in a central jar to be used by several other projects in my app. Normally I'd use OSGi and Spring DM to do this quite easily but unfortunately the architecture of this project doesn't allow that at the moment.

    So instead what I wanted to do was use the <import tag to import the central spring context file into any others that need it and then reference those beans. First of all is that how the import works? If I do something like:

    <import resource="classpath*:/META-INF/spring/locator-applicationContext.xml"/>
    Would I then just be able to reference any beans defined in that context file?

    If so my central problem is that the context file referenced may be in different locations based on how the code is being run. For instance we package our Jar into an 'uber jar' where all the dependencies are unpacked into the main Jar. In that case the referenced context would just be inside the main Jar in the normal location for Spring context files.

    But when I want to just run our unit tests in our IDE there is no packaging done and instead the IDE defines all the dependent Jars on the classpath. So in that case the referenced context.xml file will be within a different Jar file. If I use an import like above using the "classpath*:" should Spring be able to find the file in either location (even within a Jar on the classpath)? At this point my unit test are failing because it can't seem to find the beans defined in the external context.

    Any help would be greatly appreciated!


  • #2
    Any ideas on this? Is this generally the best practice for referencing external spring contexts when implementing Spring beans in a central Jar?


    • #3
      Just to make sure I don't confuse anyone it turned out the classpath wildcard stuff I was using was correct. It was a different issue causing my NPE.


      • #4
        I have a similar situation and problem... were you able to resolve this?


        • #5
          I wish I had left a better response to this since it was awhile back and I don't remember the exact cause of my problem. I just went back and checked the code from this time and the import I ended up using looks similar to the above:

          <import resource="classpath*:META-INF/**/publisher-applicationContext.xml"/>

          Not sure if the ** is even necessary. I know that we had a lot of issues packaging our uber jars because resources of the same name in the same location were overwriting each other. This of course caused all kinds of problems. I solved that by using the shade plugin instead of the package plugin or whatever it's called. I'll put the content of our shade plugin at the bottom of this post. Is your problem coming after packaging? Because if so the shade plugin fixed most of those issues for us.