一、springIOC简介
ApplicationContext是Spring IoC容器实现的代表,它负责实例化,配置和组装Bean
二、环境准备
- 创建maven项目
- 添加依赖
<dependency><groupId>org.springframeworkgroupId><artifactId>spring-contextartifactId><version>5.2.6.RELEASEversion>dependency>
- 创建实体类
public class Student{private String name;private int age;private String hobby;
}
public class Person {private int id;private String realName;private String name;private Student student;private Date birthday;private List<String> hobbies;private Map<String,String> course;
}
- 在resources文件下创建XML文件springTest.xml
三、实例解析
1.实现依赖注入的两种方式
- 基于set方法进行依赖注入
1.注入的属性需添加set方法
2.name值时set方法名字来的
<bean class&#61;"com.tedu.liyu.entry.Student" id&#61;"student"><property name&#61;"age" value&#61;"12">property><property name&#61;"hobby" value&#61;"王者荣耀">property><property name&#61;"name" value&#61;"李白">property>bean>
创建测试类&#xff1a;
public class TestIOC {ClassPathXmlApplicationContext ioc;&#64;Beforepublic void before(){ioc &#61; new ClassPathXmlApplicationContext("springTest.xml");}&#64;Testpublic void test1(){Student student &#61; ioc.getBean("student",Student.class);System.out.println(student);}
}
- 基于构造函数的依赖注入
1.注入的实体类需添加有参的构造方法
2.name值为构造方法的入参变量
public Student(String name, int age, String hobby) {this.name &#61; name;this.age &#61; age;this.hobby &#61; hobby;
}
<bean class&#61;"com.tedu.liyu.entry.Student" id&#61;"student"><constructor-arg name&#61;"name" value&#61;"十三中">constructor-arg><constructor-arg name&#61;"age" value&#61;"18">constructor-arg><constructor-arg name&#61;"hobby" value&#61;"数学">constructor-arg>bean>
2.复杂类型的注入
下面采用的是set注入&#xff0c;所以实体类中需要添加相关的set方法
<bean class&#61;"com.tedu.liyu.entry.Person" id&#61;"person"><property name&#61;"id" value&#61;"1">property><property name&#61;"name"><null>null>property><property name&#61;"realName" value&#61;"">property><property name&#61;"birthday" value&#61;"2020/05/20">property><property name&#61;"student"><bean class&#61;"com.tedu.liyu.entry.Student"><property name&#61;"name" value&#61;"张三">property><property name&#61;"hobby" value&#61;"打篮球">property><property name&#61;"age" value&#61;"18">property>bean>property><property name&#61;"hobbies"><list><value>唱歌value><value>跳舞value>list>property><property name&#61;"course"><map><entry key&#61;"1" value&#61;"Java">entry><entry key&#61;"2" value&#61;"Js">entry>map>property>
bean>
3.自动注入
<bean class&#61;"com.tedu.liyu.entry.Student" id&#61;"student"><constructor-arg name&#61;"name" value&#61;"十三中">constructor-arg><constructor-arg name&#61;"age" value&#61;"18">constructor-arg><constructor-arg name&#61;"hobby" value&#61;"数学">constructor-arg>
bean>
<bean class&#61;"com.tedu.liyu.entry.Person" id&#61;"person2" autowire&#61;"byName"><property name&#61;"id" value&#61;"999">property><property name&#61;"name" value&#61;"666">property>
bean>
当person对象中需要引用另一个student对象时&#xff0c;在自动注入时&#xff0c;spring会在IOC容器中自动找到匹配的进行注入。
byName&#xff1a;按照名字进行装配&#xff0c;以属性名作为id去容器中查找组件&#xff0c;进行赋值&#xff0c;如果找不到则装配null
byType&#xff1a;按照类型进行装配,以属性的类型作为查找依据去容器中找到这个组件&#xff0c;如果有多个类型相同的bean对象&#xff0c;那么会报异常&#xff0c;如果找不到则装配null
4.Bean的作用域
singleton&#xff1a;单例&#xff0c;只会在IOC容器中创建一次
prototype:多例&#xff0c;每次获取IOC容器都会new一个bean
<bean class&#61;"com.tedu.liyu.entry.Student" id&#61;"student3" scope&#61;"singleton">
bean>
5.生命周期回调
两种方式&#xff1a;
1.使用接口实现的方式实现生命周期的回调初始化方法&#xff1a;
实现接口InitializingBean&#xff0c;重afterPropertiesSet方法销毁方法&#xff1a;
实现接口DisposableBean&#xff0c;重写destroy方法
<bean class&#61;"com.tedu.liyu.entry.Student" id&#61;"student">bean>
public class Student implements InitializingBean , DisposableBean {public void afterPropertiesSet() throws Exception {System.out.println("生命周期回调---初始化");}public void destroy() throws Exception {System.out.println("生命周期回调---销毁");}
}
2.使用对应bean里创建的方法实现生命周期的回调
<bean class&#61;"com.tedu.liyu.entry.Student" id&#61;"student" init-method&#61;"init" destroy-method&#61;"destroy">bean>
public class Student{public void init(){System.out.println("生命周期回调---初始化");}public void destroy(){System.out.println("生命周期回调---销毁");}
}
示例如上&#xff1a;init-method的值为实体类中自定义初始化的方法名&#xff0c;destroy-method的值为实体类中自定义销毁的方法名
6.懒加载
<bean class&#61;"com.tedu.liyu.entry.Student" id&#61;"student" lazy-init&#61;"false">bean>
7.加载外部配置文件
1.先在resources下添加springContext.properties配置文件
student.name&#61;123
student.age&#61;10
student.hobby&#61;中国
<context:property-placeholder location&#61;"springContext.properties" file-encoding&#61;"UTF-8"/>
<bean class&#61;"com.tedu.liyu.entry.Student" id&#61;"student"><property name&#61;"name" value&#61;"${student.name}">property><property name&#61;"age" value&#61;"${student.age}">property><property name&#61;"hobby" value&#61;"${student.hobby}">property>
bean>
示例如上&#xff1a;需要注意的是&#xff0c;需要添加file-encoding&#61;“UTF-8”&#xff0c;不然配置文件中有中文时&#xff0c;引用可能会乱码。
8.SpEL的使用
<bean class&#61;"com.tedu.liyu.entry.Student" id&#61;"student"><constructor-arg name&#61;"name" value&#61;"十三中">constructor-arg><constructor-arg name&#61;"age" value&#61;"18">constructor-arg><constructor-arg name&#61;"hobby" value&#61;"数学">constructor-arg>
bean>
<bean class&#61;"com.tedu.liyu.entry.Person" id&#61;"person"><property name&#61;"name" value&#61;"#{student.name}">property><property name&#61;"student" value&#61;"#{student}">property><property name&#61;"birthday" value&#61;"2020/05/20">property>
bean>
SpEL:Spring Expression Language,spring的表达式语言&#xff0c;支持运行时查询操作对象