di:就是依赖注入,给属性赋值。
di注入的分类:
1.设值注入,调用java类中的set方法,给属性赋值。
2. 构造注入,调用java类中的有参数构造方法,创建对象的同时,给属性赋值。
di的语法:
1. 基于xml的配置文件,在xml中使用标签和属性,完成属性的赋值。
2.基于注解的方式,使用注解创建对象,给属性赋值。
设值注入(set方法注入)
简单类型的设值注入
package com.atChina.Test;
public class Student {
private String name;
private int age;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!-- 引用Spring的多个Schema空间的格式定义文件 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd ">
<!-- 设值注入: 调用类中的set方法完成属性赋值
简单类型: spring中把string和java基本数据类型,称为简单类型
简单类型的设值注入:
<bean id="xx" class="yy">
<property name="属性名" value="简单类型的属性值"/>
<property name="属性名" value="简单类型的属性值"/>
...
</bean>
-->
<bean id="student" class="com.atChina.Test.Student">
<property name="name" value="宋江"/>
<property name="age" value="20" />
</bean>
</beans>
引用类型的设值注入:
<?xml version="1.0" encoding="UTF-8"?>
<!-- 引用Spring的多个Schema空间的格式定义文件 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd ">
<!-- 设值注入: 调用类中的set方法完成属性赋值
简单类型: spring中把string和java基本数据类型,称为简单类型
1)简单类型的设值注入:
<bean id="xx" class="yy">
<property name="属性名" value="简单类型的属性值"/>
<property name="属性名" value="简单类型的属性值"/>
...
</bean>
2)引用类型的设值注入
语法1: 使用ref作为属性
<bean id="xx" class="yy">
<property name="属性名" ref="bean的id"/>
</bean>
语法2: 使用ref作为子标签
<bean id="xx" class="yy">
<property name="属性名">
<ref bean="bean的id"/>
<property/>
</bean>
-->
<!-- 使用语法1,给引用类型赋值,ref作为属性 -->
<bean id="student" class="com.atChina.Test2.Student">
<property name="name" value="宋江"/>
<property name="age" value="20" />
<property name="school" ref="xuexiao"/>
</bean>
<!-- 使用语法2,ref作为子标签性 -->
<bean id="student2" class="com.atChina.Test2.Student">
<property name="name" value="吴用"/>
<property name="age" value="22" />
<property name="school">
<ref bean="xuexiao"/>
</property>
</bean>
<bean id="xuexiao" class="com.atChina.Test2.School">
<property name="name" value="同济大学"/>
<property name="address" value="上海市" />
</bean>
</beans>
package com.atChina.Test2;
public class Student {
private String name;
private int age;
private School school;
public Student(){
System.out.println("无参数构造方法...");
}
public void setSchool(School school) {
this.school = school;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", school=" + school
+ "]";
}
}
package com.atChina.Test2;
public class School {
private String address;
private String name;
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "School [address=" + address + ", name=" + name + "]";
}
public void setName(String name) {
this.name = name;
}
}