Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用PropertyOverrridConfigurator重写bean引用_Java_Spring - Fatal编程技术网

Java 使用PropertyOverrridConfigurator重写bean引用

Java 使用PropertyOverrridConfigurator重写bean引用,java,spring,Java,Spring,根据,您不能使用属性覆盖配置器机制覆盖bean引用。也就是说,您只能提供文字值: 指定的覆盖值始终为空 文字值;它们不是 翻译成bean引用。这 公约也适用于下列情况: XML bean中的原始值 定义指定一个bean引用 如果我仍然希望使用覆盖属性文件重新配置布线,解决方法是什么? 我知道我可以回过头来注入引用的bean,而不是它的名称。然后我可以使用属性覆盖机制覆盖有线bean名称。但该解决方案意味着依赖Spring-ApplicationContextAware接口及其getBean(St

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

指定的覆盖值始终为空 文字值;它们不是 翻译成bean引用。这 公约也适用于下列情况: XML bean中的原始值 定义指定一个bean引用

如果我仍然希望使用覆盖属性文件重新配置布线,解决方法是什么?


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

我想你把
属性OverrideConfiguer
属性PlaceHolderConfigure
搞混了。两者相互关联,非常相似,但行为不同,包括后者有能力做这样的事情:

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

其中
x.y.z
是包含bean名称的属性

因此,您可以使用外部化属性通过
PropertyPlaceHolderConfigure
更改配线


编辑:另一种选择是完全放弃XML配置,并使用。这使java的表达能力具有了充分的灵活性,因此您几乎可以随心所欲。

正如skaffman在其回答的评论中提到的,这是您需要的解决方法

这个例子对我很有用:

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

}
重写.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>


谢谢。我没有考虑占位符,因为我希望有一个默认的连接定义(XML),并允许不修改我的XML文件的人重写。这难道不意味着如果我使用占位符,我就不能在XML中提供默认值吗?@Grzegorz:您应该能够通过SpEL找到一些有用的东西: