Shaun Hare
Shaun Hare

Reputation: 3871

Spring project that imports another spring project

I have two spring projects. Both are currently portlets.

I would like to take one and convert it to a jar and add it as a dependancy (using maven) to the other then call the code)

Any advice on the steps would be great.

I have tried and although everything (inc context) gets packaged in my jar , it appears when calling classes from within the portlet consuming the jar the application context is not loaded/working

Current jar structure 
application-context.xml
META-INF/
<package>/classes

EDIT__ Re errors: Cannot Find Bean Definition for class x

Upvotes: 1

Views: 2767

Answers (2)

Tahir Akhtar
Tahir Akhtar

Reputation: 11645

Technically, the answers provided by Andrew will work. But as he notes it is usually not a good idea to merge together beans from different projects this way. You might want to step back a little and consider these questions:

  1. Why are the two different projects in the first place? Do you want to reuse the second project across several parent projects?
  2. What components/services exposed by the child context need to be consumed directly by the parent context.

Re-thinking these dependencies might lead to a more elegant and maintainable solution.

Upvotes: 2

Andrew White
Andrew White

Reputation: 53516

There are a couple of ways to do this but it highly depends on what you are trying to do. You can include the context in your web.xml...

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext.xml, classpath:childContext.xml</param-value>
</context-param>

Another option is to import the child context directly from your application context.

<import  resource="classpath:childContext.xml/>

I'm not an overly big fan of this since these contexts get merged which can be a nightmare for bean names; however, they do work.

It's usually better that your JAR exposes some sort of factory or static constructor that reads it's own Spring config XML and then simply exposes what you want to use as a service. This might not be the best solution for your particular problem but it is something to consider.

Upvotes: 7

Related Questions