简体   繁体   English

Spring - 设置从不同上下文文件导入的bean的属性

[英]Spring - Set properties on beans imported from a different context file

Suppose that in your spring context file you have imported some context files that you cannot modify. 假设您在spring上下文文件中导入了一些无法修改的上下文文件。

Is there a way to set the properties of the beans imported ? 有没有办法设置导入的bean的属性? I do not want to copy and paste the bean definition from the imported context files and modify it because this would create a wrong dependency between my code and the external library. 我不想从导入的上下文文件中复制和粘贴bean定义并修改它,因为这会在我的代码和外部库之间产生错误的依赖关系。

I just need to modify one property of an existing bean. 我只需要修改现有bean的一个属性。

It should be possible in theory given that I can do it using a custom class that receives the bean to update as a dependency and modify its properties in the init-method. 理论上应该可以使用一个自定义类来接收bean作为依赖项进行更新并在init-method中修改其属性。

I am wondering if there is a standard syntax in Spring to do it. 我想知道Spring中是否有标准语法来执行此操作。

For example in library-context.xml there is the following bean definition: 例如,在library-context.xml中有以下bean定义:

<bean id="the.message" class="com.someco.SomeClass">
  <property name="message" value="default message" />
</bean>

I import this as an external dependency and so I do not have the option to modify this definition. 我将其导入为外部依赖项,因此我无法修改此定义。

Of course I can copy and paste this definition in my context and override it. 当然,我可以在我的上下文中复制并粘贴此定义并覆盖它。 This would be ok with a bean like the one in the example that is very simple. 对于像示例中的bean那样非常简单的bean就可以了。 The problem is that often the dependencies are much more complex and they can change in a different version of the library. 问题是,依赖关系通常要复杂得多,并且可以在库的不同版本中进行更改。

What I want is to set a property of the bean "the.message" ignoring all the other details. 我想要的是设置bean“the.message”的属性,忽略所有其他细节。

I am thinking to use something like: 我想使用类似的东西:

<bean id="myproxy" class="com.myapp.Proxy" init-method="copyProperties">
    <property name="proxied" value="the.message" />
    <property name="message" value="my message" />
</bean>

This "proxy" is only used to set the properties of "the.message". 此“代理”仅用于设置“the.message”的属性。

To do what you want to do, SomeClass would need to have a setter. 要做你想做的事, SomeClass需要有一个setter。 You would just inject the bean as you normally would and use that setter. 您可以像往常一样注入bean并使用该setter。 It would be easier using annotations but doable with XML as well. 使用注释会更容易,但也可以使用XML。

However, make sure you realize that doing this would change the value of the bean globally. 但是,请确保您意识到这样做会全局更改bean的值。 If anything was dependent on the original value, that will no longer exist. 如果任何东西依赖于原始值,那将不再存在。

I believe this could be done using a org.springframework.beans.factory.config.MethodInvokingFactoryBean in your importing context file. 我相信这可以使用导入上下文文件中的org.springframework.beans.factory.config.MethodInvokingFactoryBean来完成。

Using the imported definition from your example: 使用示例中的导入定义:

<bean id="the.message" class="com.someco.SomeClass">

message could be set in the importing context file like this: 可以在导入上下文文件中设置消息,如下所示:

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject" ref="the.message" />
    <property name="targetMethod" value="setMessage" />
    <property name="arguments">
        <list>
            <value type="java.lang.String">This message was set in importing context file</value>
        </list>
    </property>
</bean>

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

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