控制反转(IoC)又称为dependency injection(依赖注入),是spring核心的一部分。依赖注入分construtor injection,setter injection.
1.constuctor-arg injection
package com.mydomain;
publicclass Person {
private String name;
privateint age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
publicclass Person {
private String name;
privateint age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
bean配置
<bean id="person" class="com.mydomain.Person">
<constructor-arg value="Goodwin"></constructor-arg>
<constructor-arg value="20"></constructor-arg>
</bean>
<constructor-arg value="Goodwin"></constructor-arg>
<constructor-arg value="20"></constructor-arg>
</bean>
接口注入也是构建起注入的一种
interface Service{
public String get();
public List<T> list();
}
public String get();
public List<T> list();
}