Spring
01: Spring简介
02: Spring–Spring组成及拓展
03: Spring–IoC理论推导&IoC本质
04: Spring–第一个Spring程序HelloSpring&修改上节案例
05: Spring–IoC创建对象方式
06: Spring–Spring配置
07: Spring–依赖注入(Dependency Injection)
08: Spring–Bean的作用域
09: Spring–自动装配Bean(使用自动装配xml配置)
10: Spring–使用注解实现自动装配(推荐使用)
11: Spring–注解开发
12: Spring–使用JavaConfig实现配置
13: Spring–静态代理再理解&AOP
14: Spring–AOP(通过 Spring API 实现&自定义类来实现Aop&使用注解实现)
15: Spring–回忆Mybatis
16: Spring–整合Mybatis
17: Spring–Spring声明式事务
1.Spring–依赖注入(Dependency Injection)
依赖注入(Dependency Injection,DI)。
- 依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源 .
- 注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配 .
1.1 构造器注入
前面的beans.xml一直用的构造器注入
1.2 Set 注入 (重点)
1.2.1 测试pojo类
Address.java
package com.tian.pojo;
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Student.java
package com.tian.pojo;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String, String> card;
private Set<String> games;
private String wife;
private Properties info;
public void setName(String name) {
this.name = name;
}
public void setAddress(Address address) {
this.address = address;
}
public void setBooks(String[] books) {
this.books = books;
}
public void setHobbys(List<String> hobbys) {
this.hobbys = hobbys;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public void setGames(Set<String> games) {
this.games = games;
}
public void setWife(String wife) {
this.wife = wife;
}
public void setInfo(Properties info) {
this.info = info;
}
public String getName() {
return name;
}
public Address getAddress() {
return address;
}
public String[] getBooks() {
return books;
}
public List<String> getHobbys() {
return hobbys;
}
public Map<String, String> getCard() {
return card;
}
public Set<String> getGames() {
return games;
}
public String getWife() {
return wife;
}
public Properties getInfo() {
return info;
}
public void show() {
System.out.println("name=" + name
+ ",address=" + address.getAddress()
+ ",books="
);
for (String book : books) {
System.out.print("<<" + book + ">>\t");
}
System.out.println("\n爱好:" + hobbys);
System.out.println("card:" + card);
System.out.println("games:" + games);
System.out.println("wife:" + wife);
System.out.println("info:" + info);
}
}
1.2.1 常量注入
beans.xml
<bean id="student" class="com.kuang.pojo.Student">
<property name="name" value="小明"/>
</bean>
1.2.1.1 测试
Test.java
@org.junit.Test
public void test() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.getName());
}
运行结果:
1.2.2 Bean(类)注入
注意点:这里的值是一个引用,ref
<bean id="addr" class="com.tian.pojo.Address">
<property name="address" value="重庆"/>
</bean>
<bean id="student" class="com.tian.pojo.Student">
<property name="name" value="小明"/>
<property name="address" ref="addr"/>
</bean>
1.2.3 数组注入
<bean id="student" class="com.tian.pojo.Student">
<property name="name" value="小明"/>
<property name="address" ref="addr"/>
<property name="books">
<array>
<value>西游记</value>
<value>红楼梦</value>
<value>水浒传</value>
</array>
</property>
</bean>
1.2.4 List注入
<property name="hobbys">
<list>
<value>听歌</value>
<value>看电影</value>
<value>爬山</value>
</list>
</property>
1.2.5 Map注入
<property name="card">
<map>
<entry key="中国邮政" value="456456456465456"/>
<entry key="建设" value="1456682255511"/>
</map>
</property>
1.2.6 set注入
<property name="games">
<set>
<value>LOL</value>
<value>BOB</value>
<value>COC</value>
</set>
</property>
1.2.7 null注入
<property name="wife"><null/></property>
1.2.8 Properties注入
<property name="info">
<props>
<prop key="学号">20190604</prop>
<prop key="性别">男</prop>
<prop key="姓名">小明</prop>
</props>
</property>
1.3 测试结果
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="addr" class="com.tian.pojo.Address">
<property name="address" value="重庆"/>
</bean>
<bean id="student" class="com.tian.pojo.Student">
<property name="name" value="小明"/>
<property name="address" ref="addr"/>
<property name="books">
<array>
<value>西游记</value>
<value>红楼梦</value>
<value>水浒传</value>
</array>
</property>
<property name="hobbys">
<list>
<value>听歌</value>
<value>看电影</value>
<value>爬山</value>
</list>
</property>
<property name="card">
<map>
<entry key="中国邮政" value="456456456465456"/>
<entry key="建设" value="1456682255511"/>
</map>
</property>
<property name="games">
<set>
<value>LOL</value>
<value>BOB</value>
<value>COC</value>
</set>
</property>
<property name="wife">
<null/>
</property>
<property name="info">
<props>
<prop key="学号">20190604</prop>
<prop key="性别">男</prop>
<prop key="姓名">小明</prop>
</props>
</property>
</bean>
</beans>
Test.java
public class Test {
@org.junit.Test
public void test() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
student.show();
}
}
运行结果: