Overriding bean help using PropertyOverrrideConfigurer

According to Spring's documentation on PropertyOverrideConfigurer, you cannot override bean links with a property override configuration mechanism. That is, you can only provide literal values:

The specified override values ​​are always literal values; they are not translated into bean links. This convention also applies when the original value in the XML bean defines a bean reference.

What is a workaround if I still wanted to reconfigure the wiring using overriding property files?

I know that I can return to the injection, and not to the reference bean, but instead of its name. Then I could override the wired bean name using the property override mechanism. But this solution involves using the Spring interface - ApplicationContextAwareand its method getBean(String). Anything better?

+3
source share
2 answers

As the scaffman mentions in the comment of his answer, spel is the workaround you are asking for.

This example works for me:

Dog.java

public class Dog {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return ReflectionToStringBuilder.toString(this);
    }

}

override.properties

#person.d=#{dog1}
person.d=#{dog2}

Person.java

@Component
public class Person {

    private Dog d = new Dog();

    {
        d.setName("buster");
    }

    public Dog getD() {
        return d;
    }

    public void setD(Dog d) {
        this.d = d;
    }

    @Override
    public String toString() {
        return ReflectionToStringBuilder.toString(this);
    }

}

Main.java

public class Main {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext c = new ClassPathXmlApplicationContext("sandbox/spring/dog/beans.xml");
        System.out.println(c.getBean("person"));
    }

}

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:c="http://www.springframework.org/schema/c"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="sandbox.spring.dog"/>

    <bean
        class="org.springframework.beans.factory.config.PropertyOverrideConfigurer"
        p:locations="classpath:/sandbox/spring/dog/override.properties"/>

    <bean
        id="dog1"
        class="sandbox.spring.dog.Dog"
        p:name="rover"/>

    <bean
        id="dog2"
        class="sandbox.spring.dog.Dog"
        p:name="spot"/>        

</beans>
+1
source

I think you are confused PropertyOverrideConfigurerwith PropertyPlaceholderConfigurer. These two are interconnected and very similar, but have different behavior, including the latter has the ability to do such things:

<property name="myProp">
   <ref bean="${x.y.z}" />
</property>

x.y.z - , bean.

, PropertyPlaceholderConfigurer.

edit: XML @Bean -style config Java, Java, , .

0

Source: https://habr.com/ru/post/1787895/


All Articles