Java WAR - Download Spring beans from an external JAR

I would like to load inside my Spring MVC web application (in the form of a WAR) some Spring beans framework annotated with @Service from an external jar that responds to database access and located in the classpath under / WEB -INF / lib. If possible, it would be advisable to download them automatically using the @Autowired annotation.

I successfully executed the solution in link1 :

 this.ctx = new ClassPathXmlApplicationContext("services-context.xml"); this.myAService = ctx.getBean("myAService"); 

However, this solution uses the Spring getBean API function , which is considered bad practice (see link2 ).

I also tried, with no luck, two more things to load the external jar's applicationContext:

  • WAR's appContext.xml:

     <import resource="classpath*:/WEB-INF/lib/pathToExternalJar/applicationContext.xml"> 
  • WAR's web xml -> download the appContext jar as described here ( link3 ). (e.g. * applicationContext.xml):

      <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:localSpringContext.xml classpath:*applicationContext.xml </param-value> </context-param> 

What is the best approach to loading these beans and how to do it?

+4
source share
1 answer

WAR's appContext.xml and WARM web browser are possible. If you often need to perform integration tests based on both localSpringContext.xml and the external jar applicationContext.xml, I recommend using the appContext.xml WAR approach.

Updated1:

WAR's appContext.xml:

 <import resource="classpath:{classpath}/applicationContext.xml"/> 

WARM Web Browser:

 <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:localSpringContext.xml classpath:{classpath}/applicationContext.xml </param-value> </context-param> 

For example, if your applicationContext.xml is under the package: com / gmail / hippoom

you can get it using classpath: com / gmail / hippoom / applicationContext.xml or classpath *: applicationContext.xml with a wildcard.

+3
source

Source: https://habr.com/ru/post/1490659/


All Articles