我尝试使用 MyClass 扩展 PropertyPlaceholderConfigurer,并通过 Prop1 的 Autowired 注释和 Prop2 的基于 XML 的注释注入属性。Prop2 按预期工作,而 Prop1 为空。我下面的代码有问题吗?
<context:component-scan base-package="com.jchips12.test" />
<bean id="prop1" class="com.jchips12.test.Prop1">
<property name="name" value="Prop1" />
</bean>
<bean id="myClass" class="com.jchips12.test.MyClass">
<property name="prop2">
<bean class="com.jchips12.test.Prop2">
<property name="name" value="Prop2" />
</bean>
</property>
<property name="location" value="classpath:environment.properties" />
</bean>
MyClass.java
public class MyClass extends PropertyPlaceholderConfigurer{
@Autowired
private Prop1 prop1;
private Prop2 prop2;
public void setProp2(Prop2 prop2) {
this.prop2 = prop2;
}
...
}
道具 1 和道具 2
public class Prop1 {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Prop2 {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}