Spring系列(四)
系列目录
DI(Dependency Injection) 依赖注入
在创建对象的过程中Spring依据配置对对象的属性进行配置。
1. 基于set方法注入
通常的 javabean 属性都会私有化,而对暴露set和get方法,此时spring可以通过set方法将属性注入对象。
若有自定义对象,则需在配置文件中配置相应bean,并引用。
【案例】
public class Hero {
private int Id;
private String name;
private List<String> jobs;
private Set<String> set;
private Map<String,String> map;
private Properties prop;
private Dog dog;
private Cat cat;
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getJobs() {
return jobs;
}
public void setJobs(List<String> jobs) {
this.jobs = jobs;
}
public Set<String> getSet() {
return set;
}
public void setSet(Set<String> set) {
this.set = set;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public Properties getProp() {
return prop;
}
public void setProp(Properties prop) {
this.prop = prop;
}
@Override
public String toString() {
return "Hero{" +
"Id=" + Id +
", name='" + name + '\'' +
", jobs=" + jobs +
", set=" + set +
", map=" + map +
", prop=" + prop +
", dog=" + dog +
", cat=" + cat +
'}';
}
}
public class Dog {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class Cat {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Cat{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
/**
* set方式注入,写死,不方便
* 自定义bean
*/
@Test
public void test01(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Hero h = context.getBean(Hero.class);
System.out.println(h);
((ClassPathXmlApplicationContext)context).close();
}
//配置文件:
<bean id="hero" class="cn.work.domain.Hero">
<property name="id" value="123"/>
<property name="name" value="项羽"/>
<property name="jobs" >
<list>
<value>上单</value>
<value>打野</value>
<value>辅助</value>
<value>下单</value>
</list>
</property>
<property name="set">
<set>
<value>aaa</value>
<value>bbb</value>
<value>ccc</value>
<value>aaa</value>
</set>
</property>
<property name="map">
<map>
<entry key="addr" value="王者荣耀"/>
<entry key="addr" value="LOL"/>
<entry key="skill" value="风火轮"/>
<entry key="age" value="19"/>
</map>
</property>
<property name="prop">
<props>
<prop key="k1">v1</prop>
<prop key="k2">v2</prop>
<prop key="k3">v3</prop>
<prop key="k4">v4</prop>
</props>
</property>
<property name="dog" ref="dog"/>
<property name="cat" ref="cat"/>
</bean>
<bean id="dog" class="cn.work.domain.Dog">
<property name="name" value="旺财"/>
<property name="age" value="3"/>
</bean>
<bean id="cat" class="cn.work.domain.Cat">
<property name="name" value="汤姆"/>
<property name="age" value="2"/>
</bean>
2. 基于构造方法的注入
对象属性设置的另一种方法是在对象创建的过程中通过构造方法传入并设置对象的属性。
【案例】
public class Person {
private String name;
private int age;
private Dog dog;
public Person(String name, int age, Dog dog) {
this.name = name;
this.age = age;
this.dog = dog;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", dog=" + dog +
'}';
}
}
public class Dog {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
/**
* 测试 基于构造器的注入
*/
@Test
public void test04(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext4.xml");
Person person = context.getBean(Person.class);
System.out.println(person);
((ClassPathXmlApplicationContext)context).close();
}
<bean id="person" class="cn.work.domain.Person">
<!--
index:第几个参数
name:叫什么名字的参数
type:什么类型的参数
value:取什么值,直接赋值方式
ref:取什么值,通过引用方式
不一定都要写出,能唯一确定就行
-->
<constructor-arg index="0" name="name" type="java.lang.String" value="zs"/>
<constructor-arg index="1" name="age" type="int" value="11"/>
<constructor-arg index="2" name="dog" type="cn.work.domain.Dog" ref="dog" />
</bean>
<bean id="dog" class="cn.work.domain.Dog"/>