IOC 简介
IOC 概念和原理
什么是 IOC
IOC 是 Inversion of Control 的缩写,就是控制反转的意思,把对象创建和对象之间的调用过程都交给 Spring 进行管理。
使用 IOC 目的是:降低耦合度。
IOC 底层原理
主要有三个:
- xml 解析
- 工厂模式
- 反射
IOC 的两个主要接口
1、 IOC 思想基于 IOC 容器完成, IOC 容器底层就是对象工厂。
2、 Spring 提供 IOC 容器实现两种方式:(两个接口)
BeanFactory
接口- IOC 容器基本实现,是 Spring 内部的使用接口,不提供开发人员进行使用。
- 在加载配置文件时候不会创建对象,在获取对象(使用)时才去创建对象。
ApplicationContext
接口(常用)- 它是 BeanFactory 接口的子接口,提供更多更强大的功能,一般由开发人员进行使用。
- 在加载配置文件时候就会把在配置文件对象进行创建。
3、 ApplicationContext
接口有实现类
FileSystemXmlApplicationContext
对应系统盘路径(绝对路径)ClassPathXmlApplicationContext
对应类路径(相对路径)
IOC 操作 Bean 管理
Bean 管理指的是两个操作:
- Spring 创建对象
- Spirng 注入属性
IOC 操作 Bean 管理有两种实现方式:
- 基于 xml 配置文件方式实现
- 基于注解方式实现
基于 xml 方式操作 Bean
在 spring 配置文件中,使用 bean 标签,标签里面添加对应属性,就可以实现对象创建。
在 bean 标签有很多属性,介绍常用的属性
- id 属性:唯一标识
- class 属性:类全路径(包类路径)
创建对象时候,默认执行无参数构造方法完成对象创建。(因此,需要提供无参构造器)
注入属性的两种方式
- 使用 set 方法进行注入
- 创建类,定义属性和对应的 set 方法
- 在 spring 配置文件中,使用
property
完成属性的注入
- 使用有参数构造进行注入
- 创建类,定义属性,创建属性对应的有参构造器
- 在 spring 配置文件中,使用
constructor-arg
完成属性的注入
示例代码(使用 set 方法注入)
①创建Book类,设置属性和set方法
public class Book {
private String bookName;
private String author;
// 通过set方法注入属性
public void setBookName(String bookName) {
this.bookName = bookName;
}
public void setAuthor(String author) {
this.author = author;
}
public void testDemo() {
System.out.println(bookName + "::" + author);
}
}
②配置spring xml文件
<!--配置 Book 对象的创建,使用set方法注入属性-->
<bean id="book" class="com.demo.Book">
<!--
使用property完成属性的注入
name是类里面属性的名称
value是向属性注入的值
-->
<property name="bookName" value="易筋经"></property>
<property name="author" value="达摩老祖"></property>
</bean>
③测试类:
@Test
public void test1() {
// 1、加载 Spring 配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
// 2、获取配置创建的对象
Book book = context.getBean("book", Book.class);
System.out.println(book);
book.testDemo();
}
示例代码(使用有参构造注入)
①创建Orders类,设置属性和有参构造器
public class Orders {
private String orderName;
private String address;
//使用有参构造注入
public Orders(String orderName, String address) {
this.orderName = orderName;
this.address = address;
}
}
②配置spring xml文件
<!--配置 Orders 对象的创建,使用有参构造注入属性-->
<bean id="orders" class="com.demo.Orders">
<!--
使用constructor-arg完成属性的注入
name是类里面属性的名称
value是向属性注入的值
-->
<constructor-arg name="orderName" value="1234567890"></constructor-arg>
<constructor-arg name="address" value="China"></constructor-arg>
</bean>
③测试类
@Test
public void test2() {
// 1、加载 Spring 配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
// 2、获取配置创建的对象
Orders orders = context.getBean("orders", Orders.class);
System.out.println(orders);
}
注入其它字面量
1、注入 null 值
<!--注入null 值-->
<property name="address">
<null/>
</property>
2、属性值包含特殊符号
- 可以使用转移字符,比如
<>
用< >
来表示 - 也可以使用
![CDATA[含有特殊字符的内容]]
<!--属性值包含特殊符号 1 把<>进行转义:< > 2 把带特殊符号内容写到 CDATA --> <property name="address"> <value><![CDATA[<南京>]]></value> </property>
注入外部 bean
set方法和有参构造都行,下面以set方法为例
①创建两个类 service 类和 dao 类,在 service 调用 dao 里面的方法
public class UserDaoImpl implements UserDao {
@Override
public void update() {
System.out.println("update......");
}
}
public class UserService {
// 创建UserDao类型属性,生成set方法
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add() {
System.out.println("service......");
userDao.update();
}
}
②在 spring 配置文件中进行配置
<!--service和dao对象创建-->
<bean id="userService" class="com.service.UserService">
<!--注入userDao对象
name属性:类里面属性名称
ref属性:创建userDao对象bean标签id值
-->
<property name="userDao" ref="userDaoImpl"></property>
</bean>
<bean id="userDaoImpl" class="com.dao.UserDaoImpl"></bean>
③测试类
@Test
public void test3() {
// 1、加载 Spring 配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
// 2、获取配置创建的对象
UserService userService = context.getBean("userService", UserService.class);
userService.add();
}
注入内部 bean
一对多关系:一个部门有多个员工,一个员工属于一个部门。在实体类之间表示一对多关系,员工表示所属部门,使用对象类型属性进行表示。
① 创建bean包,创建 Dept 和 Emp 两个类
public class Dept {
private String dname;
public void setDname(String dname) {
this.dname = dname;
}
@Override
public String toString() {
return "Dept{" +
"dname='" + dname + '\'' +
'}';
}
}
public class Emp {
private String ename;
private String gender;
//员工属于某一个部门,使用对象形式表示
private Dept dept;
public void setEname(String ename) {
this.ename = ename;
}
public void setGender(String gender) {
this.gender = gender;
}
//生成dept的get方法
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public void add() {
System.out.println(ename + "::" + gender + "::" + dept);
}
}
②在 spring 配置文件中进行配置
<!--内部bean-->
<bean id="emp" class="com.bean.Emp">
<!--设置两个普通属性-->
<property name="ename" value="lucy"></property>
<property name="gender" value="female"></property>
<!--设置对象类型属性-->
<property name="dept">
<bean id="dept" class="com.bean.Dept">
<property name="dname" value="安保部"></property>
</bean>
</property>
</bean>
③测试类
@Test
public void test4() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
Emp emp = context.getBean("emp", Emp.class);
emp.add();
}
注入属性之级联赋值
在 spring 配置文件中进行配置(两种写法)
<bean id="emp" class="com.bean.Emp">
<property name="ename" value="lucy"></property>
<property name="gender" value="female"></property>
<!-- 级联赋值的第一种写法 -->
<property name="dept" ref="dept"></property>
<!-- 级联赋值的第二种写法,Emp必须提供dept的get方法 -->
<property name="dept.dname" value="技术部"></property>
</bean>
<bean id="dept" class="com.bean.Dept">
<property name="dname" value="财务部"></property>
</bean>
测试类
@Test
public void test5() {
// 1、加载 Spring 配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");
// 2、获取配置创建的对象
Emp emp = context.getBean("emp", Emp.class);
emp.add();
}
注入集合属性
- 注入数组类型属性
- 注入 List 集合类型属性
- 注入 Map 集合类型属性
- 注入 Set 集合类型属性
- 在集合里面设置对象类型值
①创建 Student 类,定义数组、 list、 map、 set 类型属性,生成对应 set 方法
public class Student {
// 数组类型的属性
private String[] courses;
// list集合类型的属性
private List<String> list;
// map集合类型的属性
private Map<String, String> maps;
// set集合类型的属性
private Set<String> sets;
// 学生所学多门课程
private List<Course> courseList;
public void setCourses(String[] courses) {
this.courses = courses;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMaps(Map<String, String> maps) {
this.maps = maps;
}
public void setSets(Set<String> sets) {
this.sets = sets;
}
public void setCourseList(List<Course> courseList) {
this.courseList = courseList;
}
public void test() {
System.out.println(Arrays.toString(courses));
System.out.println(list);
System.out.println(maps);
System.out.println(sets);
System.out.println(courseList);
}
}
②在 spring 配置文件中进行配置
<?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="student" class="com.collection.Student">
<!--数组类型属性注入-->
<property name="courses">
<array>
<value>java课程</value>
<value>数据库课程</value>
</array>
</property>
<!--list类型属性注入-->
<property name="list">
<list>
<value>张三</value>
<value>李四</value>
</list>
</property>
<!--map类型属性注入-->
<property name="maps">
<map>
<entry key="JAVA" value="java"></entry>
<entry key="CPP" value="c++"></entry>
</map>
</property>
<!--set类型属性注入-->
<property name="sets">
<set>
<value>MySQL</value>
<value>Redis</value>
</set>
</property>
<!--注入list集合类型,值是对象-->
<property name="courseList">
<list>
<ref bean="course1"></ref>
<ref bean="course2"></ref>
</list>
</property>
</bean>
<!--创建多个course对象-->
<bean id="course1" class="com.collection.Course">
<property name="cname" value="Spring5课程"></property>
</bean>
<bean id="course2" class="com.collection.Course">
<property name="cname" value="MyBatis框架"></property>
</bean>
</beans>
③测试类
@Test
public void test6() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean5.xml");
Student student = context.getBean("student", Student.class);
student.test();
}
把集合注入部分提取出来
①创建 Car 类
public class Car {
private List<String> list;
public void setList(List<String> list) {
this.list = list;
}
public void test() {
System.out.println(list);
}
}
②在 spring 配置文件中进行配置
注意:需要先设置 util 名称空间
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<!--1 提取list集合类型属性注入-->
<util:list id="carList">
<value>宝马</value>
<value>奔驰</value>
<value>奥迪</value>
</util:list>
<!--2 提取list集合类型属性注入使用-->
<bean id="car" class="com.collection.Car">
<property name="list" ref="carList"></property>
</bean>
</beans>
③测试类
@Test
public void test7() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean6.xml");
Car car = context.getBean("car", Car.class);
car.test();
}
FactoryBean
Spring 有两种类型 bean,一种是普通 bean,也就是前面用到的,还有另外一种是工厂 bean(FactoryBean)
- 普通 bean:在配置文件中定义 bean 类型就是返回类型
- 工厂 bean:在配置文件定义 bean 类型可以和返回类型不一样
创建工厂bean的步骤
- 创建类,让这个类作为工厂 bean,实现接口
FactoryBean
- 实现接口里面的方法,在实现的方法中定义返回的 bean 类型
示例代码
创建MyBean
类,实现FactoryBean<>
接口,重写相应的方法:
public class MyBean implements FactoryBean<Course> {
@Override
public boolean isSingleton() {
return false;
}
// 定义返回bean
@Override
public Course getObject() throws Exception {
Course course = new Course();
course.setCname("abc");
return course;
}
@Override
public Class<?> getObjectType() {
return null;
}
}
配置spring xml :
<bean id="myBean" class="com.factorybean.MyBean"></bean>
测试类:
@Test
public void test8() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean7.xml");
Course course = context.getBean("myBean", Course.class);
System.out.println(course);
}
Bean 的作用域
spring 里面,默认情况下创建的bean是单实例对象,修改前面的代码如下所示
@Test
public void test7() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean6.xml");
Car car1 = context.getBean("car", Car.class);
Car car2 = context.getBean("car", Car.class);
// car.test();
System.out.println(car1);
System.out.println(car2);
}
控制台输出:
在spring配置文件的bean
标签里面,有属性scope
,有两个值可以选择:
singletom
- 是默认值,表示单实例对象
- 加载spring配置文件时,就会创建单实例对象
prototype
- 表示多实例对象
- 不是在加载spring配置文件时创建对象,而是在调用getBean方法时创建多实例对象
前述bean文件只需修改以下scope属性即可
重新运行上面的程序,控制台输出:
Bean 的生命周期
Bean 的生命周期是指从对象创建到对象销毁的全过程。
加上 bean 的后置处理器, bean 的生命周期有七步:
- 通过构造器创建 bean 实例(无参数构造)
- 为 bean 的属性设置值和对其他 bean 引用(调用 set 方法)
- 把 bean 实例传递 bean 后置处理器的方法
postProcessBeforeInitialization
- 调用 bean 的初始化的方法(需要进行配置初始化的方法)
- 把 bean 实例传递 bean 后置处理器的方法
postProcessAfterInitialization
- bean 可以使用了(对象获取到了)
- 当容器关闭时候,调用 bean 的销毁的方法(需要进行配置销毁的方法)
创建Person类
public class Person {
private String name;
// 无参数构造
public Person() {
System.out.println("第一步,执行无参数构造创建bean实例");
}
public void setName(String name) {
this.name = name;
System.out.println("第二步,调用set方法设置属性值");
}
// 创建执行的初始化的方法
public void initMethod() {
System.out.println("第四步,执行初始化的方法");
}
// 创建执行的销毁的方法
public void destroyMethod() {
System.out.println("第七步,执行销毁的方法");
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
'}';
}
}
创建 MyBeanPost
类,实现 BeanPostProcessor
接口,重写 postProcessBeforeInitialization
,和 postProcessAfterInitialization
方法
public class MyBeanPost implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("第三步,在初始化之前执行的bean后置处理器的方法");
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("第五步,在初始化之后执行的bean后置处理器的方法");
return bean;
}
}
配置 spring xml 文件
<!--bean生命周期-->
<bean id="person" class="com.bean.Person" init-method="initMethod" destroy-method="destroyMethod">
<property name="name" value="jack"></property>
</bean>
<!--配置后置处理器-->
<bean id="myBeanPost" class="com.bean.MyBeanPost"></bean>
测试类:
@Test
public void test9() {
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("bean8.xml");
Person person = context.getBean("person", Person.class);
System.out.println("第六步,获取创建的bean实例对象:" + person);
}
控制台输出:
自动装配
自动装配就是根据指定装配规则(属性名称或者属性类型), Spring 自动将匹配的属性值进行注入。
bean标签属性autowire
配置自动装配,常用两个值:
byName
根据属性名称注入 ,注入值 bean 的 id 值和类属性名称一样。byType
根据属性类型注入。
在 autowrite 包下建立 Animal 类和 Tiger 类
public class Animal {
private Tiger tiger;
public void setTiger(Tiger tiger) {
this.tiger = tiger;
}
@Override
public String toString() {
return "Animal{" +
"tiger=" + tiger +
'}';
}
}
public class Tiger {
@Override
public String toString() {
return "Tiger{}";
}
}
配置 spring xml 文件
<!--
bean标签属性autowire,配置自动装配
autowire属性常用两个值:
byName根据属性名称注入 ,注入值bean的id值和类属性名称一样
byType根据属性类型注入
-->
<bean id="animal" class="com.autowrite.Animal" autowire="byName"></bean>
<bean id="tiger" class="com.autowrite.Tiger"></bean>
测试类
@Test
public void test10() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean9.xml");
Animal animal = context.getBean("animal", Animal.class);
System.out.println(animal);
}
引入外部属性文件
1、创建外部属性文件, properties 格式文件,写数据库信息
prop.driverClass=com.mysql.jdbc.Driver
prop.url=jdbc:mysql://localhost:3306/userDb
prop.userName=root
prop.password=abc123
2、把外部 properties 属性文件引入到 spring 配置文件中,注意要先引入 context 名称空间
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 引入外部属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${prop.driverClass}"></property>
<property name="url" value="${prop.url}"></property>
<property name="username" value="${prop.userName}"></property>
<property name="password" value="${prop.password}"></property>
</bean>
</beans>
基于注解的方式操作 Bean(重点)
创建对象
注解的基本概念
- 注解是代码特殊标记,格式: @注解名称(属性名称=属性值, 属性名称=属性值…….)
- 注解可以作用在类上面、方法上面、属性上面
- 使用注解目的:简化 xml 配置
Spring 针对 Bean 管理中创建对象提供注解
@Component
:最普通的组件,可以使用在pojo上@Service
:使用在业务层,即service层@Controller
:使用在控制层,即controller层@Repository
:使用在持久层,即dao层
上面四个注解功能是一样的,都可以用来创建 bean 实例。
基于注解方式实现对象的创建
- 引入依赖:spring-aop-5.3.2.jar
- 开启组件扫描,可以设置扫描的细节
- 创建类,在类的上面添加创建对象注解
示例代码
配置 spring xml 文件,注意要先设置 context 空间
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--
开启组件扫描
如果扫描多个包,多个包之间使用,隔开
也可以填写上层目录,涵盖下面的所有包
-->
<context:component-scan base-package="com"></context:component-scan>
<!--
示例 1
use-default-filters="false" 表示现在不使用默认的filter,而是自己配置filter
context:include-filter,设置扫描哪些内容
-->
<context:component-scan base-package="com" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--
示例 2
采用默认的filter,扫描包内所有内容
context:exclude-filter,设置哪些内容不进行扫描
-->
<context:component-scan base-package="com">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
创建 UserService 类
/**
* 在注解里面的value属性值可以省略不写,默认值是类名称,其中首字母小写
* 相当于 <bean id="userService" class=".."/>
*/
// @Component(value="userService")
// @Service
// @Repository
@Controller
public class UserService {
public void add() {
System.out.println("service....");
}
}
测试类
@Test
public void test1() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
UserService userService = context.getBean("userService", UserService.class);
System.out.println(userService);
userService.add();
}
注入属性
四种注解:
@Autowired
:根据属性类型进行自动装配。@Qualifier
:根据名称进行注入,这个@Qualifier
注解的使用需要和上面@Autowired
一起使用。@Resource
:既可以根据类型注入,又可以根据名称注入。(注意这个注解是javax.annotation
包下的,其它三个都是spring框架里面的)@Value
:注入普通类型属性。
示例代码
把 service 和 dao 对象创建
public interface UserDao {
public void add();
}
@Repository(value = "userDaoImpl1")
public class UserDaoImpl implements UserDao {
@Override
public void add() {
System.out.println("dao add.....");
}
}
在 service 注入 dao 对象,在 service 类添加 dao 类型属性,在属性上面使用注解
@Service
public class UserService {
@Value(value = "jerry") // 注入普通类型属性
private String name;
/**
* 定义dao类型属性,不需要添加set方法,添加注入属性注解
*/
// @Autowired // 根据类型注入
// @Qualifier(value = "userDaoImpl1") //根据名称进行注入,必须配合 @Autowired 一起使用
// private UserDao userDao;
// @Resource // 根据类型注入
@Resource(name = "userDaoImpl1") // 根据名称进行注入
private UserDao userDao;
public void add() {
System.out.println("service...." + name);
userDao.add();
}
}
spring xml 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com"></context:component-scan>
</beans>
测试类
@Test
public void test1() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
UserService userService = context.getBean("userService", UserService.class);
System.out.println(userService);
userService.add();
}
控制台输出:
完全注解开发
上面使用注解注入属性依然用到 xml 文件,如果想要完全注解开发,可以创建配置类,替代 xml 配置文件。
编写配置类:
@Configuration //作为配置类,替代 xml 配置文件
@ComponentScan(basePackages = {"com"}) // 扫描包
public class SpringConfig {
}
测试类:
@Test
public void test2() {
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
UserService userService = context.getBean("userService", UserService.class);
System.out.println(userService);
userService.add();
}
控制台输出: