java - 使用 PropertyOverrrideConfigurer 覆盖 bean 引用

标签 java spring

根据 Spring documentation on PropertyOverrideConfigurer您不能使用属性覆盖配置器机制覆盖 bean 引用。也就是说,您只能提供文字值:

Specified override values are always literal values; they are not translated into bean references. This convention also applies when the original value in the XML bean definition specifies a bean reference.

如果我仍想使用覆盖属性文件重新配置接线,有什么解决方法?

我知道我可以回退到不注入(inject)引用的 bean,而是注入(inject)它的名称。然后我可以使用属性覆盖机制覆盖有线 bean 名称。但该解决方案意味着依赖于 Spring - ApplicationContextAware 接口(interface)及其 getBean(String) 方法。还有更好的吗?

最佳答案

正如 skaffman 在他们的回答评论中提到的那样,spel是您要求的解决方法。

这个例子对我有用:

狗.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);
    }

}

覆盖.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);
    }

}

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

关于java - 使用 PropertyOverrrideConfigurer 覆盖 bean 引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4802504/

相关文章:

java - 如何在oracle java中获取驱动器容量

java - 想要阻止毫秒增加

java - 如何确定从 Runtime.exec() 调用的 mysqldump 是否失败

java - 什么是<form :select path> in spring tag used for?

Java/Spring 库服务异步

java - 使一组 3D 点围绕一个点旋转

java - 在整个应用程序中仅将属性加载到应用程序中一次

java - 在注释驱动的上下文中 Autowiring bean 集合

java - 如果 Java 应用程序作为服务运行,如何关闭 Spring 上下文?

java - Spring - 基于枚举的动态工厂