/**
* setter注入 添加set get 方法
*/
public class HelloImplConstr implements HelloApi {
private String message;
private int index;
// 空的构造器一定要有
public HelloImplConstr(){
}
@ConstructorProperties({"message","index"})
public HelloImplConstr(String message,int index){
this.message = message;
this.index = index;
}
public void sayHello() {
System.out.println(this.index+":"+this.message);
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
}
<!-- setter注入 -->
<bean id="beanSet" class="com.constructor.HelloImplConstr">
<property name="message" value="Hello Spring setter"/>
<property name="index">
<value>1</value>
</property>
</bean>
@Test
public void testConstructor(){
//读取配置文件实例化一个Ioc容器
ApplicationContext context = new ClassPathXmlApplicationContext("helloworld.xml");
//setter注入 根据set get 函数进行注入
HelloApi helloApi4 = context.getBean("beanSet",HelloApi.class);
helloApi4.sayHello();
}