第一节:spring ioc 简介
IOC(控制反转:Inversion of Control),又称作依赖注入dependency injection( DI ),是一种重要的面向对象编程的法则来削减计算机程序的耦合问题,也是轻量级的Spring 框架的核心。
第二节:spring ioc 实例讲解
package com.wishwzp.service; public interface Tester { public void test(); }
package com.wishwzp.service; public class LiSi implements Tester{ public void test(){ System.out.println("李四-测试程序"); } }
package com.wishwzp.service; public class ZhangSan implements Tester{ public void test(){ System.out.println("张三-测试程序"); } }
package com.wishwzp.service; public class JavaWork { private Tester tester; public void setTester(Tester tester) { this.tester = tester; } public void doTest(){ // ZhangSan zhangsan = new ZhangSan(); // zhangsan.test(); tester.test(); } }
package com.wishwzp.test; import com.wishwzp.service.JavaWork; import com.wishwzp.service.LiSi; public class Test { /** * 主管执行命令 * @param args */ public static void main(String[] args) { JavaWork javawork = new JavaWork(); javawork.setTester(new LiSi()); javawork.doTest(); } }
package com.wishwzp.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wishwzp.service.JavaWork; import com.wishwzp.service.Tester; public class Test2 { public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml"); // Tester t = null; // t=(Tester) ac.getBean("zhangsan"); // t.test(); JavaWork javaWork=(JavaWork)ac.getBean("javaWork"); javaWork.doTest(); } }
第三节:装配一个bean
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="zhangsan" class="com.wishwzp.service.ZhangSan"></bean> <bean id="lisi" class="com.wishwzp.service.LiSi"></bean> <bean id="javaWork" class="com.wishwzp.service.JavaWork"> <property name="tester" ref="zhangsan"></property> </bean> </beans>
第四节:依赖注入
1,属性注入;
2,构造函数注入;(通过类型;通过索引;联合使用)
3,工厂方法注入;(非静态工厂,静态工厂)
4,泛型依赖注入;(Spring4 整合Hibernate4 的时候顺带说)
package com.wishwzp.entity; public class People { private int id; private String name; private int age; public People() { super(); // TODO Auto-generated constructor stub } public People(int id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } 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 "People [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
package com.wishwzp.factory; import com.wishwzp.entity.People; public class PeopleFactory { public People createPeople(){ People p=new People(); p.setId(5); p.setName("小七"); p.setAge(77); return p; } }
package com.wishwzp.factory; import com.wishwzp.entity.People; public class PeopleFactory2 { public static People createPeople(){ People p=new People(); p.setId(8); p.setName("小八"); p.setAge(88); return p; } }
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="people" class="com.wishwzp.entity.People"></bean>
<!-- 属性注入 --> <bean id="people2" class="com.wishwzp.entity.People"> <property name="id" value="1"></property> <property name="name" value="张三"></property> <property name="age" value="11"></property> </bean>
<!-- 构造方法注入----通过类型--> <bean id="people3" class="com.wishwzp.entity.People"> <constructor-arg type="int" value="2"></constructor-arg> <constructor-arg type="String" value="李四"></constructor-arg> <constructor-arg type="int" value="22"></constructor-arg> </bean>
<!-- 构造方法注入---通过索引--> <bean id="people4" class="com.wishwzp.entity.People"> <constructor-arg index="0" value="3"></constructor-arg> <constructor-arg index="1" value="王五"></constructor-arg> <constructor-arg index="2" value="55"></constructor-arg> </bean>
<!-- 构造方法注入---联合使用--> <bean id="people5" class="com.wishwzp.entity.People"> <constructor-arg index="0" type="int" value="4"></constructor-arg> <constructor-arg index="1" type="String" value="招六"></constructor-arg> <constructor-arg index="2" type="int" value="66"></constructor-arg> </bean>
<!-- 工厂方法注入 --> <bean id="peopleFactory" class="com.wishwzp.factory.PeopleFactory"></bean>
<!-- 非静态工厂 --> <bean id="people7" factory-bean="peopleFactory" factory-method="createPeople"></bean>
<!-- 静态工厂 --> <bean id="people8" class="com.wishwzp.factory.PeopleFactory2" factory-method="createPeople"></bean> </beans>
package com.wishwzp.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wishwzp.entity.People; public class Test2 { public static void main(String[] args) { ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml"); People people=(People)ac.getBean("people"); System.out.println(people); // 属性注入 People people2=(People)ac.getBean("people2"); System.out.println(people2); // 构造方法注入 People people3=(People)ac.getBean("people3"); System.out.println(people3); People people4=(People)ac.getBean("people4"); System.out.println(people4); People people5=(People)ac.getBean("people5"); System.out.println(people5); // 工厂方法注入 People people7=(People)ac.getBean("people7"); System.out.println(people7); People people8=(People)ac.getBean("people8"); System.out.println(people8); } }
第五节:注入参数
1,基本类型值;
2,注入bean;
3,内部bean;
4,null 值;
5,级联属性;
6,集合类型属性;
1,基本类型值;
1 package com.wishwzp.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.wishwzp.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 27 }
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.wishwzp.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 15 </beans>
1 package com.wishwzp.entity; 2 3 public class People { 4 5 private int id; 6 private String name; 7 private int age; 8 9 10 public People() { 11 super(); 12 // TODO Auto-generated constructor stub 13 } 14 15 16 public People(int id, String name, int age) { 17 super(); 18 this.id = id; 19 this.name = name; 20 this.age = age; 21 } 22 23 24 public int getId() { 25 return id; 26 } 27 28 29 public void setId(int id) { 30 this.id = id; 31 } 32 33 34 public String getName() { 35 return name; 36 } 37 38 39 public void setName(String name) { 40 this.name = name; 41 } 42 43 44 public int getAge() { 45 return age; 46 } 47 48 49 public void setAge(int age) { 50 this.age = age; 51 } 52 53 54 @Override 55 public String toString() { 56 return "People [id=" + id + ", name=" + name + ", age=" + age + "]"; 57 } 58 59 }
运行结果显示:
2,注入bean;
1 package com.wishwzp.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.wishwzp.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 // 注入bean 20 @Test 21 public void test2() { 22 People people=(People)ac.getBean("people2"); 23 System.out.println(people); 24 } 25 26 27 }
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="dog1" class="com.wishwzp.entity.Dog"> 8 <property name="name" value="Jack"></property> 9 </bean> 10 11 <bean id="people2" class="com.wishwzp.entity.People"> 12 <property name="id" value="1"></property> 13 <property name="name" value="张三2"></property> 14 <property name="age" value="11"></property> 15 <property name="dog" ref="dog1"></property> 16 </bean> 17 </beans>
1 package com.wishwzp.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 }
1 package com.wishwzp.entity; 2 3 public class People { 4 5 private int id; 6 private String name; 7 private int age; 8 private Dog dog; 9 10 11 public People() { 12 super(); 13 // TODO Auto-generated constructor stub 14 } 15 16 public People(int id, String name, int age) { 17 super(); 18 this.id = id; 19 this.name = name; 20 this.age = age; 21 } 22 23 24 public int getId() { 25 return id; 26 } 27 28 29 public void setId(int id) { 30 this.id = id; 31 } 32 33 34 public String getName() { 35 return name; 36 } 37 38 39 public void setName(String name) { 40 this.name = name; 41 } 42 43 44 public int getAge() { 45 return age; 46 } 47 48 49 public void setAge(int age) { 50 this.age = age; 51 } 52 53 54 public Dog getDog() { 55 return dog; 56 } 57 58 59 public void setDog(Dog dog) { 60 this.dog = dog; 61 } 62 63 64 @Override 65 public String toString() { 66 return "People [id=" + id + ", name=" + name + ", age=" + age + ", dog=" + dog.getName() + "]"; 67 } 68 69 }
运行结果显示:
3,内部bean;
1 package com.wishwzp.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.wishwzp.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 // 内部bean 20 @Test 21 public void test3() { 22 People people=(People)ac.getBean("people3"); 23 System.out.println(people); 24 } 25 26 27 }
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="people3" class="com.wishwzp.entity.People"> 8 <property name="id" value="1"></property> 9 <property name="name" value="张三"></property> 10 <property name="age" value="11"></property> 11 <property name="dog"> 12 <bean class="com.wishwzp.entity.Dog"> 13 <property name="name" value="Tom"></property> 14 </bean> 15 </property> 16 </bean> 17 </beans>
1 package com.wishwzp.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 }
1 package com.wishwzp.entity; 2 3 public class People { 4 5 private int id; 6 private String name; 7 private int age; 8 private Dog dog; 9 10 11 public People() { 12 super(); 13 // TODO Auto-generated constructor stub 14 } 15 16 public People(int id, String name, int age) { 17 super(); 18 this.id = id; 19 this.name = name; 20 this.age = age; 21 } 22 23 24 public int getId() { 25 return id; 26 } 27 28 29 public void setId(int id) { 30 this.id = id; 31 } 32 33 34 public String getName() { 35 return name; 36 } 37 38 39 public void setName(String name) { 40 this.name = name; 41 } 42 43 44 public int getAge() { 45 return age; 46 } 47 48 49 public void setAge(int age) { 50 this.age = age; 51 } 52 53 54 public Dog getDog() { 55 return dog; 56 } 57 58 59 public void setDog(Dog dog) { 60 this.dog = dog; 61 } 62 63 64 @Override 65 public String toString() { 66 return "People [id=" + id + ", name=" + name + ", age=" + age + ", dog=" + dog.getName() + "]"; 67 } 68 69 }
运行结果显示:
4,null 值;
1 package com.wishwzp.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.wishwzp.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 // 注入null 20 @Test 21 public void test4() { 22 People people=(People)ac.getBean("people4"); 23 System.out.println(people); 24 } 25 26 27 }
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="people4" class="com.wishwzp.entity.People"> 8 <property name="id" value="1"></property> 9 <property name="name" value="张三"></property> 10 <property name="age" value="11"></property> 11 <property name="dog"> 12 <null></null> 13 </property> 14 </bean> 15 </beans>
1 package com.wishwzp.entity; 2 3 public class People { 4 5 private int id; 6 private String name; 7 private int age; 8 private Dog dog; 9 10 11 public People() { 12 super(); 13 // TODO Auto-generated constructor stub 14 } 15 16 public People(int id, String name, int age) { 17 super(); 18 this.id = id; 19 this.name = name; 20 this.age = age; 21 } 22 23 24 public int getId() { 25 return id; 26 } 27 28 29 public void setId(int id) { 30 this.id = id; 31 } 32 33 34 public String getName() { 35 return name; 36 } 37 38 39 public void setName(String name) { 40 this.name = name; 41 } 42 43 44 public int getAge() { 45 return age; 46 } 47 48 49 public void setAge(int age) { 50 this.age = age; 51 } 52 53 54 public Dog getDog() { 55 return dog; 56 } 57 58 59 public void setDog(Dog dog) { 60 this.dog = dog; 61 } 62 63 @Override 64 public String toString() { 65 return "People [id=" + id + ", name=" + name + ", age=" + age + ", dog=" + dog + "]"; 66 } 67 68 }
1 package com.wishwzp.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 }
运行结果显示:
5,级联属性;
1 package com.wishwzp.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.wishwzp.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 test5() { 22 People people=(People)ac.getBean("people5"); 23 System.out.println(people); 24 } 25 26 27 }
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 --> 8 <bean id="dog1" class="com.wishwzp.entity.Dog"> 9 <!-- <property name="name" value="Jack2"></property> --> 10 </bean> 11 <!-- 引用bean --> 12 <bean id="people5" class="com.wishwzp.entity.People"> 13 <property name="id" value="1"></property> 14 <property name="name" value="张三"></property> 15 <property name="age" value="11"></property> 16 <!-- 使用property的ref属性建立bean之间的引用关系 --> 17 <property name="dog" ref="dog1"></property> 18 <!-- 为级联属性赋值,首先必须引用级联属性的类 --> 19 <property name="dog.name" value="Jack2"></property> 20 </bean> 21 </beans>
1 package com.wishwzp.entity; 2 3 public class People { 4 5 private int id; 6 private String name; 7 private int age; 8 private Dog dog; 9 10 11 public People() { 12 super(); 13 // TODO Auto-generated constructor stub 14 } 15 16 public People(int id, String name, int age) { 17 super(); 18 this.id = id; 19 this.name = name; 20 this.age = age; 21 } 22 23 24 public int getId() { 25 return id; 26 } 27 28 29 public void setId(int id) { 30 this.id = id; 31 } 32 33 34 public String getName() { 35 return name; 36 } 37 38 39 public void setName(String name) { 40 this.name = name; 41 } 42 43 44 public int getAge() { 45 return age; 46 } 47 48 49 public void setAge(int age) { 50 this.age = age; 51 } 52 53 54 public Dog getDog() { 55 return dog; 56 } 57 58 59 public void setDog(Dog dog) { 60 this.dog = dog; 61 } 62 63 @Override 64 public String toString() { 65 return "People [id=" + id + ", name=" + name + ", age=" + age + ", dog=" + dog.getName() + "]"; 66 } 67 68 }
1 package com.wishwzp.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 }
运行结果显示:
6,集合类型属性;
1 package com.wishwzp.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.wishwzp.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 test6() { 22 People people=(People)ac.getBean("people6"); 23 System.out.println(people); 24 } 25 26 }
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="dog1" class="com.wishwzp.entity.Dog"> 8 <property name="name" value="Jack"></property> 9 </bean> 10 11 <bean id="people6" class="com.wishwzp.entity.People"> 12 <property name="id" value="1"></property> 13 <property name="name" value="张三"></property> 14 <property name="age" value="11"></property> 15 <property name="dog" ref="dog1"></property> 16 <property name="hobbies"> 17 <list> 18 <value>唱歌</value> 19 <value>跳舞</value> 20 </list> 21 </property> 22 <property name="loves"> 23 <set> 24 <value>唱歌2</value> 25 <value>跳舞2</value> 26 </set> 27 </property> 28 <property name="works"> 29 <map> 30 <entry> 31 <key><value>上午</value></key> 32 <value>写代码</value> 33 </entry> 34 <entry> 35 <key><value>下午</value></key> 36 <value>测试代码</value> 37 </entry> 38 </map> 39 </property> 40 <property name="addresses"> 41 <props> 42 <prop key="address1">aaaaa</prop> 43 <prop key="address2">bbbbb</prop> 44 </props> 45 </property> 46 </bean> 47 </beans>
1 package com.wishwzp.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 23 public People() { 24 super(); 25 // TODO Auto-generated constructor stub 26 } 27 28 public People(int id, String name, int age) { 29 super(); 30 this.id = id; 31 this.name = name; 32 this.age = age; 33 } 34 35 36 public int getId() { 37 return id; 38 } 39 40 41 public void setId(int id) { 42 this.id = id; 43 } 44 45 46 public String getName() { 47 return name; 48 } 49 50 51 public void setName(String name) { 52 this.name = name; 53 } 54 55 56 public int getAge() { 57 return age; 58 } 59 60 61 public void setAge(int age) { 62 this.age = age; 63 } 64 65 66 public Dog getDog() { 67 return dog; 68 } 69 70 71 public void setDog(Dog dog) { 72 this.dog = dog; 73 } 74 75 public List<String> getHobbies() { 76 return hobbies; 77 } 78 79 public void setHobbies(List<String> hobbies) { 80 this.hobbies = hobbies; 81 } 82 83 public Set<String> getLoves() { 84 return loves; 85 } 86 87 public void setLoves(Set<String> loves) { 88 this.loves = loves; 89 } 90 91 public Map<String, String> getWorks() { 92 return works; 93 } 94 95 public void setWorks(Map<String, String> works) { 96 this.works = works; 97 } 98 99 public Properties getAddresses() { 100 return addresses; 101 } 102 103 public void setAddresses(Properties addresses) { 104 this.addresses = addresses; 105 } 106 107 @Override 108 public String toString() { 109 return "People [id=" + id + ", name=" + name + ", age=" + age + ", dog=" + dog.getName() + ", hobbies=" + hobbies 110 + ", loves=" + loves + ", works=" + works + ", addresses=" + addresses + "]"; 111 } 112 113 }
1 package com.wishwzp.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 }
运行结果显示:
四月 24, 2017 9:21:01 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7c53a9eb: startup date [Mon Apr 24 21:21:01 CST 2017]; root of context hierarchy
四月 24, 2017 9:21:01 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
People [id=1, name=张三, age=11, dog=Jack, hobbies=[唱歌, 跳舞], loves=[唱歌2, 跳舞2], works={上午=写代码, 下午=测试代码}, addresses={address2=bbbbb, address1=aaaaa}]
第六节:Spring 自动装配
通过配置default-autowire 属性,Spring IOC 容器可以自动为程序注入bean;默认是no,不启用自动装配;
default-autowire 的类型有byName,byType,constructor;
byName:通过名称进行自动匹配;
byType:根据类型进行自动匹配;
constructor:和byType 类似,只不过它是根据构造方法注入而言的,根据类型,自动注入;
建议:自动装配机制慎用,它屏蔽了装配细节,容易产生潜在的错误;
1.byName:通过名称进行自动匹配;
1 package com.wishwzp.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.wishwzp.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 @Test 20 public void test1() { 21 People people=(People)ac.getBean("people1"); 22 System.out.println(people); 23 } 24 }
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 default-autowire="byName"> 7 8 <bean id="dog" class="com.wishwzp.entity.Dog"> 9 <property name="name" value="Jack"></property> 10 </bean> 11 12 <bean id="dog2" class="com.wishwzp.entity.Dog"> 13 <property name="name" value="Tom"></property> 14 </bean> 15 16 <bean id="people1" class="com.wishwzp.entity.People"> 17 <property name="id" value="1"></property> 18 <property name="name" value="张三"></property> 19 <property name="age" value="11"></property> 20 </bean> 21 22 </beans>
1 package com.wishwzp.entity; 2 3 public class People { 4 5 private int id; 6 private String name; 7 private int age; 8 private Dog dog; 9 10 public int getId() { 11 return id; 12 } 13 14 public void setId(int id) { 15 this.id = id; 16 } 17 18 public String getName() { 19 return name; 20 } 21 22 public void setName(String name) { 23 this.name = name; 24 } 25 26 public int getAge() { 27 return age; 28 } 29 30 public void setAge(int age) { 31 this.age = age; 32 } 33 34 public Dog getDog() { 35 return dog; 36 } 37 38 public void setDog(Dog dog) { 39 this.dog = dog; 40 } 41 42 @Override 43 public String toString() { 44 return "People [id=" + id + ", name=" + name + ", age=" + age + ", dog=" + dog.getName() + "]"; 45 } 46 47 }
1 package com.wishwzp.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 }
运行结果显示:
2.byType:根据类型进行自动匹配;
1 package com.wishwzp.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.wishwzp.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 @Test 20 public void test1() { 21 People people=(People)ac.getBean("people1"); 22 System.out.println(people); 23 } 24 }
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 default-autowire="byType"> 7 8 <!-- 9 <bean id="dog" class="com.wishwzp.entity.Dog"> 10 <property name="name" value="Jack"></property> 11 </bean> 12 --> 13 14 <bean id="dog2" class="com.wishwzp.entity.Dog"> 15 <property name="name" value="Tom"></property> 16 </bean> 17 18 <bean id="people1" class="com.wishwzp.entity.People"> 19 <property name="id" value="1"></property> 20 <property name="name" value="张三"></property> 21 <property name="age" value="11"></property> 22 </bean> 23 24 </beans>
1 package com.wishwzp.entity; 2 3 public class People { 4 5 private int id; 6 private String name; 7 private int age; 8 private Dog dog; 9 10 public int getId() { 11 return id; 12 } 13 14 public void setId(int id) { 15 this.id = id; 16 } 17 18 public String getName() { 19 return name; 20 } 21 22 public void setName(String name) { 23 this.name = name; 24 } 25 26 public int getAge() { 27 return age; 28 } 29 30 public void setAge(int age) { 31 this.age = age; 32 } 33 34 public Dog getDog() { 35 return dog; 36 } 37 38 public void setDog(Dog dog) { 39 this.dog = dog; 40 } 41 42 @Override 43 public String toString() { 44 return "People [id=" + id + ", name=" + name + ", age=" + age + ", dog=" + dog.getName() + "]"; 45 } 46 47 }
1 package com.wishwzp.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 }
运行结果显示:
3.constructor:和byType 类似,只不过它是根据构造方法注入而言的,根据类型,自动注入;
1 package com.wishwzp.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.wishwzp.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 @Test 20 public void test1() { 21 People people=(People)ac.getBean("people1"); 22 System.out.println(people); 23 } 24 }
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 default-autowire="constructor"> 7 8 <!-- 9 <bean id="dog" class="com.wishwzp.entity.Dog"> 10 <property name="name" value="Jack"></property> 11 </bean> 12 --> 13 14 <bean id="dog2" class="com.wishwzp.entity.Dog"> 15 <property name="name" value="Tom"></property> 16 </bean> 17 18 <bean id="people1" class="com.wishwzp.entity.People"> 19 <property name="id" value="1"></property> 20 <property name="name" value="张三"></property> 21 <property name="age" value="11"></property> 22 </bean> 23 24 </beans>
1 package com.wishwzp.entity; 2 3 public class People { 4 5 private int id; 6 private String name; 7 private int age; 8 private Dog dog; 9 10 public People() { 11 super(); 12 } 13 14 public People(Dog dog) { 15 super(); 16 System.out.println("constructor"); 17 this.dog = dog; 18 } 19 20 public int getId() { 21 return id; 22 } 23 24 public void setId(int id) { 25 this.id = id; 26 } 27 28 public String getName() { 29 return name; 30 } 31 32 public void setName(String name) { 33 this.name = name; 34 } 35 36 public int getAge() { 37 return age; 38 } 39 40 public void setAge(int age) { 41 this.age = age; 42 } 43 44 public Dog getDog() { 45 return dog; 46 } 47 48 public void setDog(Dog dog) { 49 this.dog = dog; 50 } 51 52 @Override 53 public String toString() { 54 return "People [id=" + id + ", name=" + name + ", age=" + age + ", dog=" + dog.getName() + "]"; 55 } 56 57 }
1 package com.wishwzp.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 }
运行结果显示:
第七节:方法注入
Spring bean 作用域默认是单例singleton; 可以通过配置prototype ,实现多例;
方法注入lookup-method
1.我们默认使用scope="singleton",也就是默认不写。我们发现每次获取都是一样的值,都是相等的。
T.java:
1 package com.wishwzp.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.wishwzp.entity.Dog; 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 @Test 20 public void test1() { 21 Dog dog1 = (Dog) ac.getBean("dog"); 22 Dog dog2 = (Dog) ac.getBean("dog"); 23 System.out.println(dog1==dog2); 24 } 25 26 27 }
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="dog" class="com.wishwzp.entity.Dog"><!-- 不写的话,默认是使用scope="singleton" --> 9 <property name="name" value="Jack"></property> 10 </bean> 11 12 </beans>
Dog.java:
1 package com.wishwzp.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 16 }
运行结果显示:
true
2.我们使用scope="prototype"。我们发现每次获取都是不一样的值,都是不相等的。
T.java:
1 package com.wishwzp.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.wishwzp.entity.Dog; 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 @Test 20 public void test1() { 21 Dog dog1 = (Dog) ac.getBean("dog"); 22 Dog dog2 = (Dog) ac.getBean("dog"); 23 System.out.println(dog1==dog2); 24 } 25 26 27 }
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="dog" class="com.wishwzp.entity.Dog" scope="prototype"> 9 <property name="name" value="Jack"></property> 10 </bean> 11 12 </beans>
Dog.java:
1 package com.wishwzp.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 16 }
运行结果显示:
false
3.当我们使用注入bean的时候,我们发现虽然用的是scope="prototype",但是我们发现每次获取都是一样的值,都是相等的。
T.java:
1 package com.wishwzp.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.wishwzp.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 @Test 20 public void test1() { 21 People people1 = (People) ac.getBean("people1"); 22 People people2 = (People) ac.getBean("people1"); 23 System.out.println(people1.getDog()==people2.getDog()); 24 } 25 26 27 }
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="dog" class="com.wishwzp.entity.Dog" scope="prototype"> 9 <property name="name" value="Jack"></property> 10 </bean> 11 12 <bean id="people1" class="com.wishwzp.entity.People"> 13 <property name="id" value="1"></property> 14 <property name="name" value="张三"></property> 15 <property name="age" value="11"></property> 16 <property name="dog" ref="dog"></property> 17 </bean> 18 19 </beans>
People.java:
1 package com.wishwzp.entity; 2 3 4 public class People { 5 6 private int id; 7 private String name; 8 private int age; 9 private Dog dog; 10 11 public int getId() { 12 return id; 13 } 14 public void setId(int id) { 15 this.id = id; 16 } 17 public String getName() { 18 return name; 19 } 20 public void setName(String name) { 21 this.name = name; 22 } 23 public int getAge() { 24 return age; 25 } 26 public void setAge(int age) { 27 this.age = age; 28 } 29 30 public Dog getDog() { 31 return dog; 32 } 33 public void setDog(Dog dog) { 34 this.dog = dog; 35 } 36 @Override 37 public String toString() { 38 return "People [id=" + id + ", name=" + name + ", age=" + age 39 + ", dog=" + dog.getName() + "]"; 40 } 41 42 43 }
Dog.java:
1 package com.wishwzp.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 16 }
运行结果显示:
true
3.当我们使用注入bean的时候,我们发现虽然用的是scope="prototype",但是我们发现每次获取都是一样的值,都是相等的。(这个时候我们需要改变每次获取的都是不一样的值,都是不相等的)
这里我们需要将People.java设置成public abstract class People {}类,并且将getDog设置成public abstract Dog getDog();,然后修改beans.xml中<lookup-method name="getDog" bean="dog"/>,这里的name="getDog"就是public abstract Dog getDog();的getDog(),bean="dog"就是beans.xml配置文件中上面一个<bean id="dog" >的。
T.java:
1 package com.wishwzp.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.wishwzp.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 @Test 20 public void test1() { 21 People people1 = (People) ac.getBean("people1"); 22 People people2 = (People) ac.getBean("people1"); 23 System.out.println(people1.getDog()==people2.getDog()); 24 } 25 26 27 }
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="dog" class="com.wishwzp.entity.Dog" scope="prototype"> 8 <property name="name" value="Jack"></property> 9 </bean> 10 11 <bean id="people1" class="com.wishwzp.entity.People"> 12 <property name="id" value="1"></property> 13 <property name="name" value="张三"></property> 14 <property name="age" value="11"></property> 15 <!-- 16 <property name="dog" ref="dog"></property> 17 --> 18 <lookup-method name="getDog" bean="dog"/> 19 </bean> 20 21 </beans>
People.java:
1 package com.wishwzp.entity; 2 3 4 public abstract class People { 5 6 private int id; 7 private String name; 8 private int age; 9 private Dog dog; 10 11 public int getId() { 12 return id; 13 } 14 public void setId(int id) { 15 this.id = id; 16 } 17 public String getName() { 18 return name; 19 } 20 public void setName(String name) { 21 this.name = name; 22 } 23 public int getAge() { 24 return age; 25 } 26 public void setAge(int age) { 27 this.age = age; 28 } 29 30 public abstract Dog getDog(); 31 32 public void setDog(Dog dog) { 33 this.dog = dog; 34 } 35 @Override 36 public String toString() { 37 return "People [id=" + id + ", name=" + name + ", age=" + age 38 + ", dog=" + dog.getName() + "]"; 39 } 40 41 42 }
Dog.java:
1 package com.wishwzp.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 16 }
运行结果显示:
false
第八节:方法替换
T.java:
1 package com.wishwzp.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.wishwzp.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 @Test 20 public void test1() { 21 People people=(People)ac.getBean("people1"); 22 System.out.println(people.getDog().getName()); 23 } 24 25 26 }
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.wishwzp.entity.People"> 9 <property name="id" value="1"></property> 10 <property name="name" value="张三"></property> 11 <property name="age" value="11"></property> 12 <replaced-method name="getDog" replacer="people2"></replaced-method> 13 </bean> 14 15 <bean id="people2" class="com.wishwzp.entity.People2"></bean> 16 </beans>
People2.java:
1 package com.wishwzp.entity; 2 3 import java.lang.reflect.Method; 4 5 import org.springframework.beans.factory.support.MethodReplacer; 6 7 8 public class People2 implements MethodReplacer { 9 10 @Override 11 public Object reimplement(Object arg0, Method arg1, Object[] arg2) 12 throws Throwable { 13 Dog dog=new Dog(); 14 dog.setName("Tom"); 15 return dog; 16 } 17 18 }
People.java:
1 package com.wishwzp.entity; 2 3 4 public class People { 5 6 private int id; 7 private String name; 8 private int age; 9 private Dog dog; 10 11 12 public int getId() { 13 return id; 14 } 15 public void setId(int id) { 16 this.id = id; 17 } 18 public String getName() { 19 return name; 20 } 21 public void setName(String name) { 22 this.name = name; 23 } 24 public int getAge() { 25 return age; 26 } 27 public void setAge(int age) { 28 this.age = age; 29 } 30 31 public Dog getDog() { 32 Dog dog=new Dog(); 33 dog.setName("Jack"); 34 return dog; 35 } 36 public void setDog(Dog dog) { 37 this.dog = dog; 38 } 39 @Override 40 public String toString() { 41 return "People [id=" + id + ", name=" + name + ", age=" + age 42 + ", dog=" + dog.getName() + "]"; 43 } 44 45 }
Dog.java:
1 package com.wishwzp.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 16 }
运行结果显示:
Tom
第九节:bean 之间的关系
1,继承;
2,依赖;
3,引用;
1,继承;
T.java:
1 package com.wishwzp.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.wishwzp.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 @Test 20 public void test1() { 21 People zhangsan=(People)ac.getBean("zhangsan"); 22 System.out.println(zhangsan); 23 24 People lisi=(People)ac.getBean("lisi"); 25 System.out.println(lisi); 26 } 27 28 29 }
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="abstractPeople" class="com.wishwzp.entity.People" abstract="true"><!-- 抽象的类 --> 8 <property name="className" value="高三5班"></property> 9 <property name="age" value="19"></property> 10 </bean> 11 12 <bean id="zhangsan" parent="abstractPeople"><!-- 继承了抽象类abstractPeople --> 13 <property name="id" value="1"></property> 14 <property name="name" value="张三"></property> 15 </bean> 16 17 <bean id="lisi" parent="abstractPeople"><!-- 继承了抽象类abstractPeople,并重写覆盖了age属性 --> 18 <property name="id" value="2"></property> 19 <property name="name" value="李四"></property> 20 <property name="age" value="20"></property> 21 </bean> 22 </beans>
People.java:
1 package com.wishwzp.entity; 2 3 4 public class People { 5 6 private int id; 7 private String name; 8 private int age; 9 private String className; 10 11 public People() { 12 System.out.println("初始化People"); 13 } 14 public int getId() { 15 return id; 16 } 17 public void setId(int id) { 18 this.id = id; 19 } 20 public String getName() { 21 return name; 22 } 23 public void setName(String name) { 24 this.name = name; 25 } 26 public int getAge() { 27 return age; 28 } 29 public void setAge(int age) { 30 this.age = age; 31 } 32 public String getClassName() { 33 return className; 34 } 35 public void setClassName(String className) { 36 this.className = className; 37 } 38 @Override 39 public String toString() { 40 return "People [id=" + id + ", name=" + name + ", age=" + age + ", className=" + className + "]"; 41 } 42 43 }
运行结果显示:
初始化People
初始化People
People [id=1, name=张三, age=19, className=高三5班]
People [id=2, name=李四, age=20, className=高三5班]
2,依赖;
T.java:
1 package com.wishwzp.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.wishwzp.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 @Test 20 public void test1() { 21 People zhangsan=(People)ac.getBean("zhangsan"); 22 System.out.println(zhangsan); 23 24 People lisi=(People)ac.getBean("lisi"); 25 System.out.println(lisi); 26 } 27 28 29 }
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="abstractPeople" class="com.wishwzp.entity.People" abstract="true"><!-- 抽象的类 --> 8 <property name="className" value="高三5班"></property> 9 <property name="age" value="19"></property> 10 </bean> 11 12 <bean id="zhangsan" parent="abstractPeople"><!-- 继承了抽象类abstractPeople --> 13 <property name="id" value="1"></property> 14 <property name="name" value="张三"></property> 15 </bean> 16 17 <bean id="lisi" parent="abstractPeople"><!-- 继承了抽象类abstractPeople,并重写覆盖了age属性 --> 18 <property name="id" value="2"></property> 19 <property name="name" value="李四"></property> 20 <property name="age" value="20"></property> 21 </bean> 22 23 <bean id="authority" class="com.wishwzp.service.Authority"></bean> 24 </beans>
People.java:
1 package com.wishwzp.entity; 2 3 4 public class People { 5 6 private int id; 7 private String name; 8 private int age; 9 private String className; 10 11 public People() { 12 System.out.println("初始化People"); 13 } 14 public int getId() { 15 return id; 16 } 17 public void setId(int id) { 18 this.id = id; 19 } 20 public String getName() { 21 return name; 22 } 23 public void setName(String name) { 24 this.name = name; 25 } 26 public int getAge() { 27 return age; 28 } 29 public void setAge(int age) { 30 this.age = age; 31 } 32 public String getClassName() { 33 return className; 34 } 35 public void setClassName(String className) { 36 this.className = className; 37 } 38 @Override 39 public String toString() { 40 return "People [id=" + id + ", name=" + name + ", age=" + age + ", className=" + className + "]"; 41 } 42 43 }
Authority.java:
1 package com.wishwzp.service; 2 3 public class Authority { 4 5 public Authority() { 6 System.out.println("获取权限"); 7 } 8 9 }
运行结果显示:
初始化People
初始化People
获取权限
People [id=1, name=张三, age=19, className=高三5班]
People [id=2, name=李四, age=20, className=高三5班]
----------------------
但是根据我们的需求,必须要在初始化people之前获取权限才可以。这时候我们就需要依赖关系了,people需要在初始化之前依赖authority。
所以我们修改一下beans.xml文件。
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="abstractPeople" class="com.wishwzp.entity.People" abstract="true"><!-- 抽象的类 --> 8 <property name="className" value="高三5班"></property> 9 <property name="age" value="19"></property> 10 </bean> 11 12 <bean id="zhangsan" parent="abstractPeople" depends-on="authority"><!-- 继承了抽象类abstractPeople --><!-- 添加了依赖关系depends-on="authority" --> 13 <property name="id" value="1"></property> 14 <property name="name" value="张三"></property> 15 </bean> 16 17 <bean id="lisi" parent="abstractPeople"><!-- 继承了抽象类abstractPeople,并重写覆盖了age属性 --> 18 <property name="id" value="2"></property> 19 <property name="name" value="李四"></property> 20 <property name="age" value="20"></property> 21 </bean> 22 23 <bean id="authority" class="com.wishwzp.service.Authority"></bean> 24 </beans>
运行结果显示:
获取权限
初始化People
初始化People
People [id=1, name=张三, age=19, className=高三5班]
People [id=2, name=李四, age=20, className=高三5班]
3,引用;
T.java:
1 package com.wishwzp.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.wishwzp.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 @Test 20 public void test1() { 21 People zhangsan=(People)ac.getBean("zhangsan"); 22 System.out.println(zhangsan); 23 24 People lisi=(People)ac.getBean("lisi"); 25 System.out.println(lisi); 26 } 27 28 29 }
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="dog" class="com.wishwzp.entity.Dog"> 8 <property name="name" value="jack"></property> 9 </bean> 10 11 <bean id="abstractPeople" class="com.wishwzp.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="authority"><!-- 继承了抽象类abstractPeople --><!-- 添加了依赖关系depends-on="authority" --> 17 <property name="id" value="1"></property> 18 <property name="name" value="张三"></property> 19 </bean> 20 21 <bean id="lisi" parent="abstractPeople"><!-- 继承了抽象类abstractPeople,并重写覆盖了age属性 --> 22 <property name="id" value="2"></property> 23 <property name="name" value="李四"></property> 24 <property name="age" value="20"></property> 25 <!-- 这里引用了dog --> 26 <property name="dog" ref="dog"></property> 27 </bean> 28 29 <bean id="authority" class="com.wishwzp.service.Authority"></bean> 30 </beans>
People.java:
1 package com.wishwzp.entity; 2 3 4 public class People { 5 6 private int id; 7 private String name; 8 private int age; 9 private String className; 10 private Dog dog; 11 12 public People() { 13 System.out.println("初始化People"); 14 } 15 public int getId() { 16 return id; 17 } 18 public void setId(int id) { 19 this.id = id; 20 } 21 public String getName() { 22 return name; 23 } 24 public void setName(String name) { 25 this.name = name; 26 } 27 public int getAge() { 28 return age; 29 } 30 public void setAge(int age) { 31 this.age = age; 32 } 33 public String getClassName() { 34 return className; 35 } 36 public void setClassName(String className) { 37 this.className = className; 38 } 39 40 public Dog getDog() { 41 return dog; 42 } 43 public void setDog(Dog dog) { 44 this.dog = dog; 45 } 46 47 @Override 48 public String toString() { 49 return "People [id=" + id + ", name=" + name + ", age=" + age + ", className=" + className + ", dog=" + dog 50 + "]"; 51 } 52 }
Authority.java:
1 package com.wishwzp.service; 2 3 public class Authority { 4 5 public Authority() { 6 System.out.println("获取权限"); 7 } 8 9 }
Dog.java:
1 package com.wishwzp.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 }
运行结果显示:
获取权限
初始化People
初始化People
People [id=1, name=张三, age=19, className=高三5班, dog=null]
People [id=2, name=李四, age=20, className=高三5班, dog=com.wishwzp.entity.Dog@78e67e0a]
第十节: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;
这里重点我说一下1.singleton和2.prototype两个。。。。
T.java:
1 package com.wishwzp.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.wishwzp.entity.Dog; 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 @Test 20 public void test1() { 21 Dog dog=(Dog)ac.getBean("dog"); 22 Dog dog2=(Dog)ac.getBean("dog"); 23 System.out.println(dog==dog2); 24 } 25 26 }
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="dog" class="com.wishwzp.entity.Dog" scope="singleton"><!-- scope属性不写的话默认是singleton --> 8 <property name="name" value="jack"></property> 9 </bean> 10 11 </beans>
Dog.java:
1 package com.wishwzp.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 }
运行结果显示:
true
-----------我们改一下beans.xml文件,将scope改成prototype
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="dog" class="com.wishwzp.entity.Dog" scope="prototype"><!-- scope属性不写的话默认是singleton --> 8 <property name="name" value="jack"></property> 9 </bean> 10 11 </beans>
运行结果显示:
false
-------------------------------------------------------------------------------------------------------------------------------