如何从外部实用程序jar正确加载和配置Spring bean [英] How to properly load and configure Spring beans from an external utility jar

查看:177
本文介绍了如何从外部实用程序jar正确加载和配置Spring bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有一个包含许多数据存储区服务的实用程序jar。在幕后,这些数据存储区服务使用Spring Data MongoDB,并且所有内容都使用实用程序jar中的app-context.xml文件进行配置。我希望这个实用程序jar能够更改后备存储而不必更改任何使用此实用程序jar的内容。

Currently I have a utility jar that contains a number of datastore services. Behind the scenes these datastore services use Spring Data MongoDB, and everything is configured using an app-context.xml file in the utility jar. I want this utility jar to be able to change the backing store without having to change anything that uses this utility jar.

现在,我想创建一个spring mvc Web应用程序它使用来自此实用程序jar的数据存储区服务。

Now, I want to create a spring mvc web application that uses the datastore services from this utility jar.

如何设置它以便spring mvc web app(或任何其他jar)可以轻松使用数据存储区服务不必过多地了解实用程序jar,但仍然可以正确加载实用程序jar中的bean吗?

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?

我正在考虑添加一个新的java bean类到实用程序jar,它会在自己的jar中加载app-context,然后为服务设置一些属性。然后spring mvc将在我的实用程序jar中使用这个新类创建一个bean,并通过这个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. Then the spring mvc would create a bean using this new class in my utility jar, 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");
    }
}

这是一个很好的方法吗?好像我现在有两个弹簧应用程序上下文,那可以吗?如何确保加载正确的app-context.xml,而不是另一个jar加载?有没有更好的方法呢?

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? Is there a better way of doing this?

推荐答案

由于没有人回答,我只是采用我的方法,它似乎工作,虽然稍作修改,允许bean正确破坏内部上下文。

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.

在你的实用程序jar中创建一个加载应用程序上下文xml的类,如:

In your utility jar create a class that loads the app context xml like so:

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;
    }
}

确保你的services-context.xml文件在类路径上是唯一的。你可以把它放在一个与包结构相匹配的文件夹结构中。

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

在你的另一个jar / war中,用以下东西创建beaning:

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

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

或者,如果你的其他jar / war不使用spring,那么你会做类似的事情: / p>

Or, if your other jar/war doesn't use spring, then you do something like:

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

这篇关于如何从外部实用程序jar正确加载和配置Spring bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆