-1

I have a standalone spring project which is working fine. While running the project I simply load the application context in the main method like:

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
SampleBean sampleBean = (SampleBean) context.getBean("sampleBean"); 

and things get moving.

Now I have to create a jar file for this project and include it in another Maven project which is not a Spring project. Here, I have to call the functions present in the included Spring jar file using classes which in itself contain autowiring.

Now since I am directly calling the Class function without loading the application context, other dependencies of autowiring are failing.

Please suggest how do I load the application context of the included Spring jar.

MWiesner
  • 8,868
  • 11
  • 36
  • 70
user1184527
  • 113
  • 1
  • 12
  • Possible Duplicate - http://stackoverflow.com/questions/6303242/loading-spring-application-context-files-that-are-inside-a-jar-in-classpath – Omkar Puttagunta Feb 12 '16 at 12:58
  • This link talks about how to include and load Spring jar in another Spring application. So in the answer http://stackoverflow.com/a/6304113/1184527 says that a program A which is a Spring application is added into another program B (which again has the Spring dependencies). The dependency is updated in Program B's pom.xml and the application context.xml is loaded from program B's main method. – user1184527 Feb 12 '16 at 15:39

1 Answers1

0

You can load them in your web.xml file like

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value> 
            classpath:application-context.xml.xml
        </param-value>
    </context-param>

and then you can use them in your code like normal way using @ Annotation.


There is one more way but I haven't tested it yet.

  <import resource="classpath:/application-context.xml"/>
Pulkit
  • 3,953
  • 6
  • 31
  • 55