How To: Load a spring application from a jar file

Consider the following situation:

Loading 3 application context files in a Swing application. 2 of the context files are in jar-files.
All context files depend on the component-scan option. The Application context gets loaded from my Swing app.
The method I use, is the import resource option in the context file. Read on how to configure this in your application context.

My DAO class

1
2
3
4
5
6
7
8
9
package nl.foo.bar.dao;

import org.springframework.stereotype.Repository;

import nl.foo.bar.domain.GOClass;

@Repository("goClassDAO")
public class GOClassDAOJPA extends BaseDAOJPA<GOClass> implements GOClassDAO {
}

ApplicationContext-dao.xml

1
2
3
4
<context:component-scan base-package="nl.foo.bar.dao">
        <context:include-filter type="annotation"
           expression="org.springframework.stereotype.Repository"/>
    </context:component-scan>

ApplicationContext-controller.xml (this gets loaded from my swing application)

1
2
3
4
5
6
7
8
<import resource="classpath:ApplicationContext-dao.xml" />
   
    <context:annotation-config/>
   
  <context:component-scan base-package="nl.foo.bar.gui.controller">
    <context:include-filter type="annotation"    
                      expression="org.springframework.stereotype.Controller"/>
  </context:component-scan>

Code snippet from my swing application

1
2
3
String[] locations = {"ApplicationContext-controller.xml"};
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(locations);
    ApplicationController appController = (ApplicationController)ctx.getBean("applicationController");

I have tried various other options in the past (like adding “classpath:*/ApplicationContext-dao.xml” and variations to the locations) too no success.

The way to go is the import-tag in my opinion. Works like a charm.

Kind regards,
Marc de Kwant

Tags: , , , ,

Leave a Reply