《一头扎进Spring4》学习笔记(二)Spring 之 ioc 详解
第一节 Spring ioc 简介
IOC(控制反转:Inverse of Control),又称作 依赖注入,是一种重要的面向对象编程的法则来削减计算机程序的耦合问题,也是轻量级的Spring框架的核心。
第二节 Spring ico 实例
1、接口类
1 public interface Tester { 2 public void test(); 3 }
2、实体类继承接口类
1 public class Lisi implements Tester{ 2 public void test(){ 3 System.out.println("李四-测试程序"); 4 } 5 } 6 7 public class ZhangSan implements Tester{ 8 public void test(){ 9 System.out.println("张三-测试程序"); 10 } 11 }
3、控制类
public class JavaWork { private Tester tester; public void setTester(Tester tester) { this.tester = tester; } public void doTest(){ tester.test(); } }
4、beans.xml,Spring管理bean
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans.xsd"> 6 7 <bean id="zhangsan" class="com.java1234.service.ZhangSan"></bean> 8 9 <bean id="lisi" class="com.java1234.service.Lisi"></bean> 10 11 <bean id="javaWork" class="com.java1234.service.JavaWork"> 12 <property name="tester" ref="lisi"></property> 13 </bean> 14 15 </beans>
property标签设置参数,ref 设置会自动调用JavaWork的set方法给tester赋值
5、测试类
1 public class Test2 { 2 3 public static void main(String[] args) { 4 ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml"); 5 JavaWork javaWork=(JavaWork)ac.getBean("javaWork"); 6 javaWork.doTest(); 7 } 8 }
第三节 装配一个bean
第四节 依赖注入
1、属性注入
2、构造函数注入(通过类型,通过索引,联合使用)
3、工厂方法注入(非静态工厂,静态工厂)
4、泛型依赖注入
5、实例:
实体类People
1 package com.java1234.entity; 2 3 public class People { 4 5 private int id; 6 private String name; 7 private int age; 8 9 public People() { 10 super(); 11 // TODO Auto-generated constructor stub 12 } 13 14 public People(int id, String name, int age) { 15 super(); 16 this.id = id; 17 this.name = name; 18 this.age = age; 19 } 20 21 public int getId() { 22 return id; 23 } 24 public void setId(int id) { 25 this.id = id; 26 } 27 public String getName() { 28 return name; 29 } 30 public void setName(String name) { 31 this.name = name; 32 } 33 public int getAge() { 34 return age; 35 } 36 public void setAge(int age) { 37 this.age = age; 38 } 39 40 @Override 41 public String toString() { 42 return "People [id=" + id + ", name=" + name + ", age=" + age + "]"; 43 } 44 45 }
非静态工厂类
1 package com.java1234.factory; 2 import com.java1234.entity.People; 3 4 public class PeopleFactory { 5 6 public People createPeople(){ 7 People p=new People(); 8 p.setId(5); 9 p.setName("小七"); 10 p.setAge(77); 11 return p; 12 } 13 }
静态工厂类
1 package com.java1234.factory; 2 import com.java1234.entity.People; 3 4 public class PeopleFactory2 { 5 6 public static People createPeople(){ 7 People p=new People(); 8 p.setId(8); 9 p.setName("小八"); 10 p.setAge(88); 11 return p; 12 } 13 }
beans.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans.xsd"> 6 7 <bean id="people" class="com.java1234.entity.People"></bean> 8 9 <bean id="people2" class="com.java1234.entity.People"> 10 <property name="id" value="1"></property> 11 <property name="name" value="张三"></property> 12 <property name="age" value="11"></property> 13 </bean> 14 15 <bean id="people3" class="com.java1234.entity.People"> 16 <constructor-arg type="int" value="2"></constructor-arg> 17 <constructor-arg type="String" value="李四"></constructor-arg> 18 <constructor-arg type="int" value="22"></constructor-arg> 19 </bean> 20 21 <bean id="people4" class="com.java1234.entity.People"> 22 <constructor-arg index="0" value="3"></constructor-arg> 23 <constructor-arg index="1" value="王五"></constructor-arg> 24 <constructor-arg index="2" value="55"></constructor-arg> 25 </bean> 26 27 <bean id="people5" class="com.java1234.entity.People"> 28 <constructor-arg index="0" type="int" value="4"></constructor-arg> 29 <constructor-arg index="1" type="String" value="招六"></constructor-arg> 30 <constructor-arg index="2" type="int" value="66"></constructor-arg> 31 </bean> 32 33 <bean id="peopleFactory" class="com.java1234.factory.PeopleFactory"></bean> 34 35 <bean id="people7" factory-bean="peopleFactory" factory-method="createPeople"></bean> 36 37 <bean id="people8" class="com.java1234.factory.PeopleFactory2" factory-method="createPeople"></bean> 38 </beans>
注:
1) 属性注入 property[name,value]
2) 构造函数注入 constructor-arg[type|index,value]
3) 工厂注入 非静态工厂先声明工厂bean,再通过 factory-bean、factory-method 引用
静态工厂直接指定工厂类class,再 factory-method
测试类
1 package com.java1234.test; 2 import org.springframework.context.ApplicationContext; 3 import org.springframework.context.support.ClassPathXmlApplicationContext; 4 import com.java1234.entity.People; 5 6 public class Test2 { 7 8 public static void main(String[] args) { 9 ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml"); 10 People people=(People)ac.getBean("people"); 11 System.out.println(people); 12 13 // 属性注入 14 People people2=(People)ac.getBean("people2"); 15 System.out.println(people2); 16 17 // 构造方法注入 18 People people3=(People)ac.getBean("people3"); 19 System.out.println(people3); 20 21 People people4=(People)ac.getBean("people4"); 22 System.out.println(people4); 23 24 People people5=(People)ac.getBean("people5"); 25 System.out.println(people5); 26 27 // 工厂方法注入 28 People people7=(People)ac.getBean("people7"); 29 System.out.println(people7); 30 31 People people8=(People)ac.getBean("people8"); 32 System.out.println(people8); 33 } 34 }
第五节 注入参数
1、基本类型值
2、注入 bean
3、内部 bean
4、null 值
5、级联属性
6、集合类型属性
7、实例
实体类People.java
1 package com.java1234.entity; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.HashSet; 6 import java.util.List; 7 import java.util.Map; 8 import java.util.Properties; 9 import java.util.Set; 10 11 public class People { 12 13 private int id; 14 private String name; 15 private int age; 16 private Dog dog; 17 private List<String> hobbies=new ArrayList<String>(); 18 private Set<String> loves=new HashSet<String>(); 19 private Map<String,String> works=new HashMap<String,String>(); 20 private Properties addresses=new Properties(); 21 22 public People() { 23 super(); 24 // TODO Auto-generated constructor stub 25 } 26 27 public People(int id, String name, int age) { 28 super(); 29 this.id = id; 30 this.name = name; 31 this.age = age; 32 } 33 34 public Set<String> getLoves() { 35 return loves; 36 } 37 38 public void setLoves(Set<String> loves) { 39 this.loves = loves; 40 } 41 42 public List<String> getHobbies() { 43 return hobbies; 44 } 45 public void setHobbies(List<String> hobbies) { 46 this.hobbies = hobbies; 47 } 48 public Map<String, String> getWorks() { 49 return works; 50 } 51 public void setWorks(Map<String, String> works) { 52 this.works = works; 53 } 54 55 public Properties getAddresses() { 56 return addresses; 57 } 58 public void setAddresses(Properties addresses) { 59 this.addresses = addresses; 60 } 61 62 public Dog getDog() { 63 return dog; 64 } 65 public void setDog(Dog dog) { 66 this.dog = dog; 67 } 68 69 public int getId() { 70 return id; 71 } 72 public void setId(int id) { 73 this.id = id; 74 } 75 public String getName() { 76 return name; 77 } 78 public void setName(String name) { 79 this.name = name; 80 } 81 public int getAge() { 82 return age; 83 } 84 public void setAge(int age) { 85 this.age = age; 86 } 87 88 @Override 89 public String toString() { 90 return "People [id=" + id + ", name=" + name + ", age=" + age 91 + ", dog=" + dog + ", hobbies=" + hobbies + ", loves=" + loves 92 + ", works=" + works + ", addresses=" + addresses + "]"; 93 } 94 95 96 97 }
实体类Dog.java
1 package com.java1234.entity; 2 3 public class Dog { 4 5 private String name; 6 7 public String getName() { 8 return name; 9 } 10 11 public void setName(String name) { 12 this.name = name; 13 } 14 15 }
beans.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans.xsd"> 6 7 8 <bean id="people1" class="com.java1234.entity.People"> 9 <property name="id" value="1"></property> 10 <property name="name" value="张三"></property> 11 <property name="age" value="11"></property> 12 </bean> 13 14 <bean id="dog1" class="com.java1234.entity.Dog"> 15 <property name="name" value="Jack"></property> 16 </bean> 17 18 <bean id="people2" class="com.java1234.entity.People"> 19 <property name="id" value="1"></property> 20 <property name="name" value="张三"></property> 21 <property name="age" value="11"></property> 22 <property name="dog" ref="dog1"></property> 23 </bean> 24 25 <bean id="people3" class="com.java1234.entity.People"> 26 <property name="id" value="1"></property> 27 <property name="name" value="张三"></property> 28 <property name="age" value="11"></property> 29 <property name="dog"> 30 <bean class="com.java1234.entity.Dog"> 31 <property name="name" value="Tom"></property> 32 </bean> 33 </property> 34 </bean> 35 36 <bean id="people4" class="com.java1234.entity.People"> 37 <property name="id" value="1"></property> 38 <property name="name" value="张三"></property> 39 <property name="age" value="11"></property> 40 <property name="dog"> 41 <null></null> 42 </property> 43 </bean> 44 45 <!-- <bean id="people5" class="com.java1234.entity.People"> 46 <property name="id" value="1"></property> 47 <property name="name" value="张三"></property> 48 <property name="age" value="11"></property> 49 <property name="dog.name" value="Jack2"></property> 50 </bean> --> 51 52 <bean id="people6" class="com.java1234.entity.People"> 53 <property name="id" value="1"></property> 54 <property name="name" value="张三"></property> 55 <property name="age" value="11"></property> 56 <property name="dog" ref="dog1"></property> 57 <property name="hobbies"> 58 <list> 59 <value>唱歌</value> 60 <value>跳舞</value> 61 </list> 62 </property> 63 <property name="loves"> 64 <set> 65 <value>唱歌2</value> 66 <value>跳舞2</value> 67 </set> 68 </property> 69 <property name="works"> 70 <map> 71 <entry> 72 <key><value>上午</value></key> 73 <value>写代码</value> 74 </entry> 75 <entry> 76 <key><value>下午</value></key> 77 <value>测试代码</value> 78 </entry> 79 </map> 80 </property> 81 <property name="addresses"> 82 <props> 83 <prop key="address1">aaaaa</prop> 84 <prop key="address2">bbbbb</prop> 85 </props> 86 </property> 87 </bean> 88 89 </beans>
测试类Junit test case
1 package com.java1234.test; 2 3 import org.junit.Before; 4 import org.junit.Test; 5 import org.springframework.context.ApplicationContext; 6 import org.springframework.context.support.ClassPathXmlApplicationContext; 7 8 import com.java1234.entity.People; 9 10 public class T { 11 12 private ApplicationContext ac; 13 14 @Before 15 public void setUp() throws Exception { 16 ac=new ClassPathXmlApplicationContext("beans.xml"); 17 } 18 19 // 基本类型值 20 @Test 21 public void test1() { 22 People people=(People)ac.getBean("people1"); 23 System.out.println(people); 24 } 25 26 // 注入bean 27 @Test 28 public void test2() { 29 People people=(People)ac.getBean("people2"); 30 System.out.println(people); 31 } 32 33 34 // 内部bean 35 @Test 36 public void test3() { 37 People people=(People)ac.getBean("people3"); 38 System.out.println(people); 39 } 40 41 // 注入null 42 @Test 43 public void test4() { 44 People people=(People)ac.getBean("people4"); 45 System.out.println(people); 46 } 47 48 // 级联属性 49 @Test 50 public void test5() { 51 People people=(People)ac.getBean("people5"); 52 System.out.println(people); 53 } 54 55 // 注入集合 56 @Test 57 public void test6() { 58 People people=(People)ac.getBean("people6"); 59 System.out.println(people); 60 } 61 }
第六节 Spring 自动装配
通过配置 default-autowire 属性,Spring IOC容器可以自动为程序注入bean;默认是no,不启动自动装配;default-autowire 的类型有 byName,byType,constructor;
byName:通过名称进行自动匹配;
byType:根据类型进行自动匹配;
constructor:和byType类似,只不过它是根据构造方法注入而言的,根据类型,自动注入。
建议:自动装配机制慎用,它屏蔽了装配细节,容易产生潜在的错误。
第七节 方法注入
Spring bean 作用域默认是 单例 singleton; 可以通过配置 prototype 实现多例。
方法注入 lookup-method
第八节 方法替换
第九节 bean之间的关系
1、继承(abstract="true"、parent="beanid")
2、依赖(depends-on="beanid")
3、引用
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans.xsd"> 6 7 <bean id="dog" class="com.java1234.entity.Dog"> 8 <property name="name" value="jack"></property> 9 </bean> 10 11 <bean id="abstractPeople" class="com.java1234.entity.People" abstract="true"> 12 <property name="className" value="高三5班"></property> 13 <property name="age" value="19"></property> 14 </bean> 15 16 <bean id="zhangsan" parent="abstractPeople" depends-on="autority"> 17 <property name="id" value="1"></property> 18 <property name="name" value="张三"></property> 19 </bean> 20 21 <bean id="lisi" parent="abstractPeople"> 22 <property name="id" value="2"></property> 23 <property name="name" value="李四"></property> 24 <property name="age" value="20"></property> 25 <property name="dog" ref="dog"></property> 26 </bean> 27 28 29 <bean id="autority" class="com.java1234.service.Authority"></bean> 30 </beans>
第十节 bean 作用范围
1、singleton Spring IOC 容器中仅有一个 bean 实例,bean 已单例的方式存在;
2、prototype 每次从容器中调用 bean 时, 都返回一个新的实例;
3、request 每次http 请求都会创建一个新的bean;
4、session 同一个 http session 共享一个 bean;
5、global session 同一个全局 session 共享一个 bean, 一般用于portlet应用环境;
6、application 同一个 application 共享一个 bean;