IOC 控制反转:创建对象的方式 变成了由Spring来主导
IOC底层原理:对象工厂
1、导入jar包:4个核心jar和1个依赖jar
spring-beans-4.3.9.RELEASE.jar
spring-context-4.3.9.RELEASE.jar
spring-core-4.3.9.RELEASE.jar
spring-expression-4.3.9.RELEASE.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
2、添加xml配置文件:applicationContext.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=" 5 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 6 <bean id="foo" class="x.y.Foo"> 7 <meta key="cacheName" value="foo"/> 8 <property name="name" value="Rick"/> 9 </bean> 10 </beans>
3、在xml中beans标签里注册类
Class属性 一定为全路径 (因为ioc的底层实现是通过反射技术)
id:通过id来 获取本类对象,一般为类名(首字母小写)
1 <bean id="studentId" class="com.spring.Student"> 2 </bean>
4、获取对象方式
1 //初始化spring 容器 2 ApplicationContext con = new ClassPathXmlApplicationContext("applicationContext.xml"); 3 //通过 getBean() 获取对象 4 Student stu = (Student)con.getBean("studentId"); 5 stu.study();
scope
scope= "prototype"多实例 。 scope= " singleton "单例
默认为单例模式 singleton
lazy-init
lazy-init = "true"; 懒加载:使用时再创建对象
lazy-init = "false"; 默认为false。容器初始化时创建对象
DI 依赖注入
概念 :启动Spring容器 加载bean配置的时候,完成对变量的赋值行为
一:使用xml表配置注入:
1、有参构造注入
1 public Student(String name, int age) { 2 super(); 3 this.name = name; 4 this.age = age; 5 }
1 <bean id="studentId" class="com.spring.Student" scope="singleton" lazy-init="true"> 2 3 <constructor-arg name="name" value="刘杨哥哥"></constructor-arg> 4 <constructor-arg name="age" value="20"></constructor-arg> 5 6 </bean>
2、Set方法注入
1 public String getName() { 2 return name; 3 } 4 public void setName(String name) { 5 this.name = name; 6 } 7 8 public int getAge() { 9 return age; 10 } 11 public void setAge(int age) { 12 this.age = age; 13 }
1 <bean id="studentId" class="com.spring.Student" scope="singleton" lazy-init="true"> 2 3 <property name="name" value="liuyang"></property> 4 <property name="age" value="20"></property> 5 6 </bean>
3、属性为对象的注入方式
1 package com.spring_ioc1; 2 3 public class Clazz { 4 5 private Student stu; 6 7 public Student getStu() { 8 return stu; 9 } 10 public void setStu(Student stu) { 11 this.stu = stu; 12 } 13 14 public void clzzbegin(){ 15 16 // clazz依赖学生 17 18 System.out.println("=== 开始上课 ==="); 19 stu.study(); 20 } 21 22 }
1 <!-- 依赖注入: clazz依赖学生: 所以需注入学生 --> 2 <bean id="clazzId" class="com.spring.Clazz" scope="singleton" lazy-init="true"> 3 <property name="stu" ref="studentId"></property> 4 </bean>
4、特殊类型的对象的注入
数组、List集合、Map集合
1 package com.spring_ioc1; 2 3 import java.util.ArrayList; 4 import java.util.Map; 5 6 public class Property { 7 8 private String arr[]; 9 private ArrayList<String> list; 10 private Map map; 11 12 public String[] getArr() { 13 return arr; 14 } 15 public void setArr(String[] arr) { 16 this.arr = arr; 17 } 18 public ArrayList<String> getList() { 19 return list; 20 } 21 public void setList(ArrayList<String> list) { 22 this.list = list; 23 } 24 public Map getMap() { 25 return map; 26 } 27 public void setMap(Map map) { 28 this.map = map; 29 } 30 public Property() { 31 super(); 32 } 33 34 }
1 <bean id="propertyId" class="com.spring.Property"> 2 <property name="arr"> 3 <list> 4 <value>arr刘杨</value> 5 <value>arr刘超</value> 6 <value>arr刘聪</value> 7 </list> 8 </property> 9 <property name="list"> 10 <list> 11 <value>list刘杨</value> 12 <value>list刘超</value> 13 <value>list刘聪</value> 14 </list> 15 </property> 16 <property name="map"> 17 <map> 18 <entry key="1" value="map刘杨"></entry> 19 <entry key="2" value="map刘超"></entry> 20 <entry key="3" value="map刘聪"></entry> 21 </map> 22 </property> 23 </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 xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" 5 http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here --> 9 10 <!-- 自动扫描 配置 扫描所有注解 :使用注解注入 --> 11 <context:component-scan base-package="com.spring_ioc2"></context:component-scan> 12 13 </beans>
1 package com.spring_ioc2; 2 3 import javax.annotation.Resource; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.beans.factory.annotation.Value; 7 import org.springframework.context.annotation.Lazy; 8 import org.springframework.context.annotation.Scope; 9 import org.springframework.stereotype.Component; 10 11 @Component(value="clazzId") //相当于 <bean id="clazzId" class="com.spring.clazz"></bean> 12 //@Scope(value="prototype") 13 @Lazy 14 public class Clazz { 15 16 @Value("gs02班") // <property name="" value=""> 17 private String name; 18 19 //@Autowired //根据student 的类型寻找 // <property name="" ref=""> 20 @Resource(name="student") //指定别名扫描寻找 21 private Student stu; 22 23 public String getName() { 24 return name; 25 } 26 public void setName(String name) { 27 this.name = name; 28 } 29 public Student getStu() { 30 return stu; 31 } 32 public void setStu(Student stu) { 33 this.stu = stu; 34 } 35 36 37 public Clazz(){ 38 System.out.println("=== clazz初始化完成 ==="); 39 } 40 41 42 public void clzzBegin(){ 43 44 System.out.println("=== 开始上课喽 ==="); 45 //stu.study(); 46 } 47 48 }
1 package com.spring_ioc2; 2 3 import org.springframework.beans.factory.annotation.Value; 4 import org.springframework.stereotype.Component; 5 6 @Component("student") 7 public class Student { 8 9 @Value("刘杨") 10 private String name; 11 private int age; 12 13 14 public Student() { 15 System.out.println("=== student初始化完成 ==="); 16 } 17 18 public Student(String name) { 19 this.name = name; 20 } 21 22 23 public Student(String name, int age) { 24 super(); 25 this.name = name; 26 this.age = age; 27 } 28 29 public String getName() { 30 return name; 31 } 32 public void setName(String name) { 33 this.name = name; 34 } 35 36 public int getAge() { 37 return age; 38 } 39 public void setAge(int age) { 40 this.age = age; 41 } 42 43 44 public void study(){ 45 System.out.println("Student study 正在努力学习!!!"); 46 } 47 }
三: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 xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" 5 http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here --> 9 10 <!-- 自动扫描 配置 扫描有限注解 :xml表与注解混合使用注入--> 11 <context:annotation-config></context:annotation-config> 12 13 <!--注册类 --> 14 <bean id="clazzId" class="com.spring_ioc3.Clazz"></bean> 15 <bean id="student" class="com.spring_ioc3.Student"></bean> 16 17 </beans>
1 package com.spring_ioc3; 2 3 import javax.annotation.Resource; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.beans.factory.annotation.Value; 7 import org.springframework.context.annotation.Lazy; 8 import org.springframework.context.annotation.Scope; 9 import org.springframework.stereotype.Component; 10 11 public class Clazz { 12 13 @Value("gs02班") // <property name="" value=""> 14 private String name; 15 16 //@Autowired //根据student 的类型寻找 // <property name="" ref=""> 17 @Resource(name="student") //指定别名扫描寻找 18 private Student stu; 19 20 public String getName() { 21 return name; 22 } 23 public void setName(String name) { 24 this.name = name; 25 } 26 public Student getStu() { 27 return stu; 28 } 29 public void setStu(Student stu) { 30 this.stu = stu; 31 } 32 33 34 public Clazz(){ 35 System.out.println("=== clazz初始化完成 ==="); 36 } 37 38 public void clzzBegin(){ 39 40 System.out.println("=== 开始上课喽 ==="); 41 //stu.study(); 42 } 43 44 }
1 package com.spring_ioc3; 2 3 import org.springframework.beans.factory.annotation.Value; 4 import org.springframework.stereotype.Component; 5 6 7 public class Student { 8 9 @Value("刘杨") 10 private String name; 11 private int age; 12 13 14 public Student() { 15 System.out.println("=== student初始化完成 ==="); 16 } 17 18 public Student(String name) { 19 this.name = name; 20 } 21 22 23 public Student(String name, int age) { 24 super(); 25 this.name = name; 26 this.age = age; 27 } 28 29 public String getName() { 30 return name; 31 } 32 public void setName(String name) { 33 this.name = name; 34 } 35 36 public int getAge() { 37 return age; 38 } 39 public void setAge(int age) { 40 this.age = age; 41 } 42 43 44 public void study(){ 45 System.out.println("Student study 正在努力学习!!!"); 46 } 47 }