我嘗試使用MyClass擴展PropertyPlaceholderConfigurer,並通過Autowired註釋爲Prop1和基於XML的Prop2注入屬性。 Prop1按預期工作,而Prop1爲空。我的代碼在下面有什麼問題嗎?當PropertyPlaceholderConfigurer被擴展時,我無法使用註釋擴展類
<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;
}
...
}
PROP1和PROP2
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;
}
}
我appreaciate你的答案,並會讀你接受這個作爲一個答案之前給了參考,也你有一個錯字,它不是PROP2說失敗,其prop1。 – jchips12