How to properly download and configure Spring beans from an external jar utility

I currently have a jar utility that contains a number of data warehouse services. Behind the scenes, these data warehouse services are using Spring Data MongoDB, and everything is configured using the app-context.xml file in the jar utility. I want this utility box to be able to modify the repository without changing anything that uses this utility.

Now I want to create a Spring mvc web application that uses the data warehouse services from this jar utility.

How to configure this so that the Spring mvc web application (or any other bank) can easily use the data storage services without knowing too much about the jar utility, but there are still beans in the utility loaded correctly?

I was thinking of adding a new java bean class to the utility jar, which would load the application context into its own jar, and then set some properties for itself for the services. Then Spring mvc will create a bean using this new class in my jar utility, and reference the services through this bean.

/** * This bean would exist in the utility jar, and other jars/webapps would * create a new instance of this bean. */ public class Services { private MyAService myAService; private MyBService myBService; public Services() { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("app-context.xml"); // these are configured in the app-context.xml this.myAService = ctx.getBean("myAService"); this.myBService = ctx.getBean("myBService"); } } 

Is this a good way to do this? It looks like I will now have two Spring application contexts, is this normal? How can I ensure that the proper app-context.xml is loaded, and not from another jar? Is there a better way to do this?

+4
source share
2 answers

Since no one answered, I just went with my approach and seemed to work, albeit with a slight modification, to allow the bean to properly destroy the internal context.

In your jar utility, create a class that loads the application xml context like this:

 public class Services implements DisposableBean { ClassPathXmlApplicationContext ctx; private MyAService myAService; private MyBService myBService; public Services() { this.ctx = new ClassPathXmlApplicationContext("services-context.xml"); // these are configured in the services-context.xml this.myAService = ctx.getBean("myAService"); this.myBService = ctx.getBean("myBService"); } // Add getters for your services @Override public void destroy() throws Exception { this.myAService = null; this.myBService = null; this.ctx.destroy(); this.ctx = null; } } 

Make sure your "services-context.xml" file is unique in the classpath. You can do this by placing it in a folder structure that matches the package structure.

In your other bank / war, create beaning using something like:

 <bean id="services" class="Services" destroy-method="destroy"/> 

Or, if your other jar / war does not use spring, you do something like:

 Services s = new Services(); //... use your services, then clean up when done s.myAService.doSomething(); s.destroy(); 
+4
source

Two approaches with which you can solve this: (Please include the dependency as part of pom.xml)

  • To manually include only the necessary beans utility in this new application-context.xml with the path to these class paths. It is the beauty of spring to create only a selective bean.

  • Have a properties file (Include this in the new application-context.xml)

<context:property-placeholder location="WEB-INF/utility.properties"/>

<import resource="${application.path.of.utility.jar}"/>

And determine the path to the jar utility

 application.path.of.utility.jar=/utility/jar/path/application_context.xml 
0
source

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


All Articles