简体   繁体   English

如何从外部实用程序jar正确加载和配置Spring bean

[英]How to properly load and configure Spring beans from an external utility jar

Currently I have a utility jar that contains a number of datastore services. 目前我有一个包含许多数据存储区服务的实用程序jar。 Behind the scenes these datastore services use Spring Data MongoDB, and everything is configured using an app-context.xml file in the utility jar. 在幕后,这些数据存储区服务使用Spring Data MongoDB,并且所有内容都使用实用程序jar中的app-context.xml文件进行配置。 I want this utility jar to be able to change the backing store without having to change anything that uses this utility jar. 我希望这个实用程序jar能够更改后备存储,而无需更改使用此实用程序jar的任何内容。

Now, I want to create a spring mvc web application that uses the datastore services from this utility jar. 现在,我想创建一个spring mvc Web应用程序,它使用此实用程序jar中的数据存储区服务。

How do I set this up so that the spring mvc web app (or any other jar) can easily use the datastore services without having to know too much about the utility jar, but still have the beans in the utility jar loaded properly? 我如何设置它,以便spring mvc web应用程序(或任何其他jar)可以轻松使用数据存储区服务,而不必过多了解实用程序jar,但仍然可以正确加载实用程序jar中的bean?

I was thinking of adding a new java bean class to the utility jar that would load the app-context in it's own jar, and then set some properties on itself for the services. 我正在考虑向实用程序jar中添加一个新的java bean类,它将app-context加载到它自己的jar中,然后为服务设置一些属性。 Then the spring mvc would create a bean using this new class in my utility jar, and reference the services through this bean. 然后spring mvc将在我的实用程序jar中使用这个新类创建一个bean,并通过这个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 go about this? 这是一个很好的方法吗? It seems like I would now have two spring application contexts, is that ok? 好像我现在有两个弹簧应用程序上下文,那可以吗? How do I ensure that the proper app-context.xml is loaded, and not one from another jar? 如何确保加载正确的app-context.xml,而不是另一个jar加载? Is there a better way of doing this? 有没有更好的方法呢?

Since nobody has answered, I just went with my approach and it seems to work, though with a slight modification to allow the bean to destroy the internal context properly. 由于没有人回答,我只是采用我的方法,它似乎工作,虽然稍作修改,允许bean正确破坏内部上下文。

In your utility jar create a class that loads the app context xml like so: 在您的实用程序jar中创建一个加载应用程序上下文xml的类,如下所示:

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 on the classpath. 确保您的“services-context.xml”文件在类路径中是唯一的。 You can do this by putting it in a folder structure that matches the package structure. 您可以通过将其放在与包结构匹配的文件夹结构中来完成此操作。

In your other jar/war, create the beaning using something like: 在你的另一个罐子/战争中,使用以下内容创建beaning:

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

Or, if your other jar/war doesn't use spring, then you do something like: 或者,如果你的其他jar / war不使用spring,那么你会做类似的事情:

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

Two approaches by which you can solve this : (Please include the dependency as part of pom.xml) 您可以通过两种方法解决此问题:(请将依赖项作为pom.xml的一部分包含在内)

  1. To manually include only the required utility beans into this new application-context.xml with the path referring to those class paths. 仅使用引用这些类路径的路径手动将所需的实用程序bean包含到此新的application-context.xml中。 That's the beauty of spring to create the selective bean only. 这只是创造选择性豆类的春天之美。

  2. Have a properties file (Include this in the new application-context.xml) 有一个属性文件(在新的application-context.xml中包含它)

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

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

And define path ao the utility jar 并定义路径ao实用程序jar

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM