可选的Spring bean引用 [英] Optional Spring bean references

查看:89
本文介绍了可选的Spring bean引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我使用ContextLoaderListener从许多罐中加载上下文文件:

In my application I am using ContextLoaderListener to load context files from many jars using:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:META-INF/contextBeans.xml</param-value>
</context-param>

这意味着我可以在不进行导入的情况下从其他罐子中引用bean。

This means I can reference beans from other jars without doing import.

在应用程序中有多个部署选项,在某些部署中可以排除jar。为了支持我,我希望一些bean引用是可选的。例如:

In the application there are multiple deployment options and in some deployments jars can be excluded. To support that I would like some bean references to be optional. For example:

<bean id="mainAppBean" class="com.someapp.MyApplication">
    <constructor-arg index="0" ref="localBean"/>
     <constructor-arg index="1" ref="optionalBeanReference1"/>
    <constructor-arg index="2" ref="optionalBeanReference2"/>
 </bean>

在上面的示例中,如果找不到引用,我想让optionalBeanReference1等于null(标记它)

In the example above I would like to have optionalBeanReference1 equal null if the reference was not found (mark it optional in some way)

这可以在Spring完成吗?或者你推荐哪种方法来处理动态参考?

Can this be done in Spring? or what method do you recommend for handling dynamic references?

推荐答案


你推荐哪种方法来处理动态参考?

what method do you recommend for handling dynamic references?

我认为@ cristian的@Autowired答案很好。如果该类型的bean可用,那将调用setter方法。但是,如果你有多个相同类型的bean,我相信Spring会抛出异常。如果你因为这个或其他原因不能使用@Autowired,我会看到几个解决方案:

I think @cristian's @Autowired answer is a good one. That will call the setter methods if the beans of that type are available. However, if you have multiple beans of the same type, I believe Spring throws an exception. If you cannot use @Autowired for this or some other reason, I see a couple of solutions:


  1. 你可以上课 ApplicationContextAware 并自己在上下文中查找bean:

  1. You could make your class ApplicationContextAware and lookup the beans in the context yourself:

public void setApplicationContext(ApplicationContext applicationContext) {
    if (applicationContext.containsBean("optionalBeanReference1")) {
        setOptionalBeanReference1(
            (OptionalBeanReference1)applicationContext.bean(
                "optionalBeanReference1");
    }
    ...
}


  • 你可以反转每个可选类都可以在mainAppBean上设置本身。我在某些情况下使用它,直接依赖会导致循环或其他问题。

  • You could invert the dependency. Each of the optional classes could set themselves on the mainAppBean. I use this in certain situations when a direct dependency would cause loops or other problems.

    <bean id="optionalBeanReference1" class="com.someapp.SomeClass">
        <constructor-arg index="0" ref="mainAppBean"/>
    </bean>
    

    然后在SomeClass中:

    Then in the SomeClass:

    public SomeClass(com.someapp.MyApplication mainAppBean) {
        mainAppBean.setOptionalBeanReference1(this);
    }
    


  • 您可以保持直接依赖关系,然后导入文件定义bean或导入另一个文件,在该文件中使用工厂bean将bean定义为具有空值。请参阅此工厂代码

    祝你好运。

    这篇关于可选的Spring bean引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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