标签:构造函数 factory vat splay singleton enter 创建对象 web 特殊
Spring框架基础
测试Spring的IOC(控制反转)和DI(依赖注入)
为了防止每次创建对象都要创建一个Spring容器,所以在xml中加一个监听器
0.导包
1.创建配置文件
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 http://www.springframework.org/schema/beans/spring-beans.xsd"> 5 6 <!-- spring:管理对象的容器 --> 7 <!-- spring IOC:控制反转,通过spring创建对象(无需手动创建) --> 8 <!-- DI:依赖注入;就是给对象的某些属性赋值 --> 9 10 <!-- 将User对象交给spring管理 --> 11 <!-- bean元素:描述需要spring管理的对象;name属性:给对象取个别名,获取对象时需要; 12 class属性:被管理对象的完整类名;id属性:与name属性一模一样,不可重复,不能使用特殊字符(因此要name)--> 13 <!-- 创建方式一:spring调用无参构造创建对象 --> 14 <!-- scope:singleton:表示单例对象,在spring容器中只会存在一个实例 --> 15 <!-- scope:prototype:表示多例对象,每次获取都会创建一个新的对象;整合struts2时必须为多例 --> 16 <!-- 生命周期方法配置init,destroy --> 17 <bean name="user" class="com.bean.User" scope="singleton" init-method="init" destroy-method="destroy"></bean> 18 19 <!-- 创建方式二:通过静态工厂的某个方法创建一个user对象 --> 20 <bean name="user1" class="com.bean.UserFactory" factory-method="createUser"></bean> 21 22 <!-- 创建方式三:实例工厂创建对象 --> 23 <bean name="user2" factory-bean="userFactory" factory-method="createUser2"></bean> 24 <bean name="userFactory" class="com.bean.UserFactory"></bean> 25 26 <!-- 模块化配置,可以导入其他spring配置文件 --> 27 <!--<import resource="applicationContext.xml"/>--> 28 </beans>
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 xmlns:p="http://www.springframework.org/schema/p" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 6 7 <!-- set方式注入 --> 8 <bean name="user" class="com.bean.User"> 9 <!-- 值类型注入 --> 10 <property name="name" value="xdzy"/> 11 <property name="age" value="18"/> 12 <!-- 引用类型注入 --> 13 <property name="car" ref="car"/> 14 </bean> 15 16 <!-- 将car对象配置到spring容器 --> 17 <bean name="car" class="com.bean.Car"> 18 <property name="name" value="兰博基尼"/> 19 <property name="color" value="红色"/> 20 </bean> 21 22 <!-- 构造函数注入 --> 23 <bean name="user1" class="com.bean.User"> 24 <!-- 当构造方法参数位置不同,可以通过index确定参数位置 --> 25 <!-- 当构造方法参数类型不同,可以通过type确定参数类型 --> 26 <constructor-arg name="name" value="xdzy" index="0" type="java.lang.String"/> 27 <constructor-arg name="car" ref="car"/> 28 </bean> 29 30 <!-- p属性注入 --> 31 <!-- 需加入xmlns:p="http://www.springframework.org/schema/p" --> 32 <bean name="user2" class="com.bean.User" p:name="xdzy" p:age="18" p:car-ref="car"/> 33 34 <!-- spel表达式注入;可以实现动态注入 --> 35 <bean name="user3" class="com.bean.User"> 36 <property name="name" value="#{user.name}"/> 37 <property name="age" value="#{user2.age}"/> 38 <property name="car" ref="car"/> 39 </bean> 40 41 <!-- 数组注入 --> 42 <bean name="cb" class="com.bean.CollectionBean"> 43 <!-- 只有一个值时 --> 44 <!--<property name="arr" value="tom"/>--> 45 <!-- 多值多元素注入 --> 46 <property name="arr"> 47 <array> 48 <value>xdzy</value> 49 <value>jady</value> 50 <ref bean="user3"/> 51 </array> 52 </property> 53 </bean> 54 55 <!-- 集合注入 --> 56 <bean name="cb2" class="com.bean.CollectionBean"> 57 <!-- 只有一个值时 --> 58 <!--<property name="list" value="tom"/>--> 59 <!-- 多值多元素注入 --> 60 <property name="list"> 61 <list> 62 <value>xdzy</value> 63 <value>jady</value> 64 <ref bean="user3"/> 65 </list> 66 </property> 67 </bean> 68 69 <!-- map注入 --> 70 <bean name="cb3" class="com.bean.CollectionBean"> 71 <property name="map"> 72 <map> 73 <entry key="name" value="xdzy"/> 74 <entry key="user" value-ref="user3"/> 75 <entry key-ref="user1" value-ref="user2"/> 76 </map> 77 </property> 78 </bean> 79 80 <!-- 资源注入 --> 81 <bean name="cb4" class="com.bean.CollectionBean"> 82 <property name="properties"> 83 <props> 84 <prop key="driverClass">com.jdbc.mysql.Driver</prop> 85 <prop key="userName">admin</prop> 86 <prop key="password">123</prop> 87 </props> 88 </property> 89 </bean> 90 </beans>
相应的实体类
1 package com.bean; 2 3 /** 4 * @author: 肖德子裕 5 * @date: 2018/9/5 19:31 6 * @description: 实体bean 7 */ 8 public class User { 9 private String name; 10 private int age; 11 private Car car; 12 13 public void init(){ 14 System.out.println("初始化方法"); 15 } 16 17 public void destroy(){ 18 System.out.println("销毁方法"); 19 } 20 21 public User() { 22 System.out.println("无参构造创建一个对象"); 23 } 24 25 public User(String name, Car car) { 26 System.out.println("构造函数注入"); 27 this.name = name; 28 this.car = car; 29 } 30 31 public User(Car car,String name) { 32 System.out.println("构造函数注入2"); 33 this.name = name; 34 this.car = car; 35 } 36 37 public User(Integer name, Car car) { 38 System.out.println("构造函数注入3"); 39 this.name = name+""; 40 this.car = car; 41 } 42 43 public Car getCar() { 44 return car; 45 } 46 47 public void setCar(Car car) { 48 this.car = car; 49 } 50 51 public String getName() { 52 return name; 53 } 54 55 public void setName(String name) { 56 this.name = name; 57 } 58 59 public int getAge() { 60 return age; 61 } 62 63 public void setAge(int age) { 64 this.age = age; 65 } 66 67 @Override 68 public String toString() { 69 return "User{" + 70 "name=‘" + name + ‘\‘‘ + 71 ", age=" + age + 72 ", car=" + car + 73 ‘}‘; 74 } 75 }
1 package com.bean; 2 3 /** 4 * @author: 肖德子裕 5 * @date: 2018/9/5 22:56 6 * @description: 静态工厂 7 */ 8 public class UserFactory { 9 public static User createUser(){ 10 System.out.println("静态工厂创建对象"); 11 return new User(); 12 } 13 14 public User createUser2(){ 15 System.out.println("实例工厂创建对象"); 16 return new User(); 17 } 18 }
1 package com.bean; 2 3 /** 4 * @author: 肖德子裕 5 * @date: 2018/9/5 23:38 6 * @description: 车bean 7 */ 8 public class Car { 9 private String name; 10 private String color; 11 12 public String getName() { 13 return name; 14 } 15 16 public void setName(String name) { 17 this.name = name; 18 } 19 20 public String getColor() { 21 return color; 22 } 23 24 public void setColor(String color) { 25 this.color = color; 26 } 27 28 @Override 29 public String toString() { 30 return "Car{" + 31 "name=‘" + name + ‘\‘‘ + 32 ", color=‘" + color + ‘\‘‘ + 33 ‘}‘; 34 } 35 }
1 package com.bean; 2 3 import java.util.Arrays; 4 import java.util.List; 5 import java.util.Map; 6 import java.util.Properties; 7 8 /** 9 * @author: 肖德子裕 10 * @date: 2018/9/6 07:56 11 * @description: 复杂类型注入 12 */ 13 public class CollectionBean { 14 private Object[] arr;//数组 15 private List list;//集合 16 private Map map;//map集合 17 private Properties properties;//资源文件 18 19 public Object[] getArr() { 20 return arr; 21 } 22 23 public void setArr(Object[] arr) { 24 this.arr = arr; 25 } 26 27 public List getList() { 28 return list; 29 } 30 31 public void setList(List list) { 32 this.list = list; 33 } 34 35 public Map getMap() { 36 return map; 37 } 38 39 public void setMap(Map map) { 40 this.map = map; 41 } 42 43 public Properties getProperties() { 44 return properties; 45 } 46 47 public void setProperties(Properties properties) { 48 this.properties = properties; 49 } 50 51 @Override 52 public String toString() { 53 return "CollectionBean{" + 54 "arr=" + Arrays.toString(arr) + 55 ", list=" + list + 56 ", map=" + map + 57 ", properties=" + properties + 58 ‘}‘; 59 } 60 }
相应的测试类
1 package com.test; 2 3 import com.bean.User; 4 import org.junit.Test; 5 import org.springframework.context.ApplicationContext; 6 import org.springframework.context.support.ClassPathXmlApplicationContext; 7 8 /** 9 * @author: 肖德子裕 10 * @date: 2018/9/5 19:59 11 * @description: 测试能否从xml中获取对象 12 */ 13 public class Demo1 { 14 //调用无参创建对象 15 @Test 16 public void test(){ 17 //创建容器对象 18 ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); 19 //获取user对象 20 /*User user=(User)ac.getBean("user"); 21 System.out.println(user);*/ 22 } 23 24 //静态工厂创建对象 25 @Test 26 public void test1(){ 27 //创建容器对象 28 ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); 29 //获取user对象 30 User user=(User)ac.getBean("user1"); 31 System.out.println(user); 32 } 33 34 //实例工厂创建对象 35 @Test 36 public void test2(){ 37 //创建容器对象 38 ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); 39 //获取user对象 40 User user=(User)ac.getBean("user2"); 41 System.out.println(user); 42 } 43 }
1 package com.test; 2 3 import com.bean.CollectionBean; 4 import com.bean.User; 5 import org.junit.Test; 6 import org.springframework.context.ApplicationContext; 7 import org.springframework.context.support.ClassPathXmlApplicationContext; 8 9 /** 10 * @author: 肖德子裕 11 * @date: 2018/9/5 19:59 12 * @description: 给属性注入值 13 */ 14 public class Demo2 { 15 //set 16 @Test 17 public void test(){ 18 //创建容器对象 19 ApplicationContext ac=new ClassPathXmlApplicationContext("com/bean/applicationContext.xml"); 20 //获取user对象 21 User user=(User)ac.getBean("user"); 22 System.out.println(user); 23 } 24 //构造 25 @Test 26 public void test1(){ 27 //创建容器对象 28 ApplicationContext ac=new ClassPathXmlApplicationContext("com/bean/applicationContext.xml"); 29 //获取user对象 30 User user=(User)ac.getBean("user1"); 31 System.out.println(user); 32 } 33 //p属性 34 @Test 35 public void test2(){ 36 //创建容器对象 37 ApplicationContext ac=new ClassPathXmlApplicationContext("com/bean/applicationContext.xml"); 38 //获取user对象 39 User user=(User)ac.getBean("user2"); 40 System.out.println(user); 41 } 42 //spel 43 @Test 44 public void test3(){ 45 //创建容器对象 46 ApplicationContext ac=new ClassPathXmlApplicationContext("com/bean/applicationContext.xml"); 47 //获取user对象 48 User user=(User)ac.getBean("user3"); 49 System.out.println(user); 50 } 51 //数组or集合 52 @Test 53 public void test4(){ 54 //创建容器对象 55 ApplicationContext ac=new ClassPathXmlApplicationContext("com/bean/applicationContext.xml"); 56 //获取user对象 57 CollectionBean cb=(CollectionBean)ac.getBean("cb2"); 58 System.out.println(cb); 59 } 60 //map 61 @Test 62 public void test5(){ 63 //创建容器对象 64 ApplicationContext ac=new ClassPathXmlApplicationContext("com/bean/applicationContext.xml"); 65 //获取user对象 66 CollectionBean cb=(CollectionBean)ac.getBean("cb3"); 67 System.out.println(cb); 68 } 69 //资源 70 @Test 71 public void test6(){ 72 //创建容器对象 73 ApplicationContext ac=new ClassPathXmlApplicationContext("com/bean/applicationContext.xml"); 74 //获取user对象 75 CollectionBean cb=(CollectionBean)ac.getBean("cb4"); 76 System.out.println(cb); 77 } 78 }
标签:构造函数 factory vat splay singleton enter 创建对象 web 特殊
原文地址:https://www.cnblogs.com/xdzy/p/9596105.html