作为skaffman在他们的回答评论中提到,spel是您所要求的解决方法。
这个例子对我的作品:
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>
https://jira.springsource.org/browse/SPR-4905 – 2011-01-26 08:40:10