5

Suppose that in your spring context file you have imported some context files that you cannot modify.

Is there a way to set the properties of the beans imported ? 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.

I just need to modify one property of an existing 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.

I am wondering if there is a standard syntax in Spring to do it.

For example in library-context.xml there is the following bean definition:

<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. 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.

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".

  • possible duplicate of [Can I replace a Spring bean definition at runtime?](http://stackoverflow.com/questions/4041300/can-i-replace-a-spring-bean-definition-at-runtime) –  Oct 23 '14 at 08:53
  • 5
    It does not seem the same problem. I do not want to change it at runtime. I only need to set a property in my context file. –  Oct 23 '14 at 09:07

2 Answers2

0

To do what you want to do, SomeClass would need to have a setter. You would just inject the bean as you normally would and use that setter. It would be easier using annotations but doable with XML as well.

However, make sure you realize that doing this would change the value of the bean globally. If anything was dependent on the original value, that will no longer exist.

Jamie Bisotti
  • 2,605
  • 19
  • 23
0

I believe this could be done using a org.springframework.beans.factory.config.MethodInvokingFactoryBean in your importing context file.

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>
JTP
  • 231
  • 3
  • 5