Spring框架
Spring框架概述
Spring 有两个核心部分:Ioc 和 Aop
(1) Ioc:控制反转,把创建对象过程交给 Spring 进行管理
(2) Aop:面向切面,不修改源代码进行功能增强
Spring入门程序
导入依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
创建一个类
public class User {
public void add() {
System.out.println("add...");
}
}
编写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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="com.liao.entity.User"></bean>
</beans>
测试
@Test
public void testUserBean() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("UserBean.xml");
User user = context.getBean("user", User.class);
System.out.println(user);
user.add();
}
输出结果
Ioc容器
Ioc概念和原理
1.什么是 Ioc
(1) 控制反转,把对象创建和对象之间的调用过程,交给 Spring 进行管理
(2) 使用 Ioc 目的:为了耦合度降低
(3) 做入门案例就是 Ioc 实现
2.Ioc底层原理
xml 解析、工厂模式、反射
3.Ioc底层执行原理
Ioc过程
第一步,创建 xml 配置文件,配置创建的对象
<bean id="dao" class="com.liao.dao.UserDao"></bean>
第二步,有 service 类和 dao 类,创建工厂类
class UserFactory {
public static UserDao getDao() {
//1.解析 xml 文件,读取类的属性值
String classValue = class属性值;
//2.通过反射创建对象
Class classN = Class.forName(classValue);
//3.返回 UserDao 实例对象
return (UserDao) classN.newInstance();
}
}
Ioc接口(BeanFactory)
1.Ioc 思想基于 Ioc 容器完成,Ioc 容器底层就是对象工厂
2.Spring 提供两种 Ioc 容器的实现方式(两个接口):
(1) BeanFactory:Ioc 容器的基本实现,是 Spring 内部的使用接口,一般不进行使用
* 加载配置文件时不会创建对象,在获取(使用)对象时才会创建对象
(2) ApplicationContext:BeanFactory 接口的子接口,提供更多更强大的功能,一般都使用这个
* 加载配置文件时就会把在配置文件中的对象进行创建
(3) ApplicationContext 接口常用的实现类 ClassPathXmlApplicationContext
Ioc操作Bean管理
什么是 Bean 管理
* Bean 管理指的是两个操作:Spring 创建对象、Spring 注入属性
基于xml配置文件方式实现
基于xml方式创建对象
<!--配置User对象创建-->
<bean id="user" class="com.liao.entity.User"></bean>
1.在 Spring 配置文件中,使用 bean 标签,标签里面添加对应属性,就可以实现对象创建
2.在 bean 标签有很多属性,介绍常用的属性
* id 属性:唯一标识
* class 属性:类全路径(包+类路径)
3.创建对象的时候,默认执行无参数构造方法完成对象创建
基于xml方式注入属性
DI:依赖注入,也就是注入属性,在创建对象的基础上才能注入属性。有两种方法进行注入
1.使用 set 方法进行注入
(1) 创建类,定义属性和对应的 set 方法
public class Book {
private String name;
private String author;
public void setName(String name) {
this.name = name;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
'}';
}
}
(2) 在 Spring 配置文件配置对象创建,配置属性注入
<bean id="book" class="com.liao.entity.Book">
<property name="name" value="易筋经"></property>
<property name="author" value="达摩老祖"></property>
</bean>
(3) 测试
@Test
public void testBook() {
ApplicationContext context = new ClassPathXmlApplicationContext("UserBean.xml");
Book book = context.getBean("book", Book.class);
System.out.println(book);
}
(4) 输出结果
2.使用有参构造方法进行注入
(1) 创建类,定义属性,创建属性对应的有参构造方法
public class Orders {
private String name;
private String address;
public Orders(String name, String address) {
this.name = name;
this.address = address;
}
@Override
public String toString() {
return "Orders{" +
"name='" + name + '\'' +
", address='" + address + '\'' +
'}';
}
}
(2) 在 Spring 配置文件中进行配置
<bean id="orders" class="com.liao.entity.Orders">
<constructor-arg name="name" value="X-001"></constructor-arg>
<constructor-arg name="address" value="china"></constructor-arg>
</bean>
(3) 测试
@Test
public void testOrders() {
ApplicationContext context = new ClassPathXmlApplicationContext("UserBean.xml");
Orders orders = context.getBean("orders", Orders.class);
System.out.println(orders);
}
(4) 测试结果
3.p名称空间注入(简化基于xml配置中的set方式)
(1) 添加 p 名称空间在配置文件头中
xmlns:p="http://www.springframework.org/schema/p"
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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">
(2) 进行属性注入,在 bean 标签里面进行操作
<bean id="book" class="com.liao.entity.Book" p:name="九阳神功" p:author="斗酒神僧"></bean>
基于xml注入其他类型属性
1.字面量
(1) null值
<bean id="book" class="com.liao.entity.Book">
<property name="name" value="易筋经"></property>
<property name="author">
<null/>
</property>
</bean>
(2) 属性值包含特殊符号
<bean id="book" class="com.liao.entity.Book">
<property name="name" value="易筋经"></property>
<!--
属性值包含特殊符号
1.把<>进行转义 < >
2.把带特殊符号内容写到CDATA
-->
<property name="author">
<value><![CDATA[<<达摩老祖>>]]></value>
</property>
</bean>
2.注入属性-外部 bean
(1) 创建 service 和 dao 两个类
public interface CatDao {
void run();
}
public class CatDaoImpl implements CatDao {
@Override
public void run() {
System.out.println("CatDao is running ...");
}
}
public interface CatService {
void run();
}
(2) 在 service 中调用 dao 里面的方法
public class CatServiceImpl implements CatService {
private CatDao catDao;
public void setCatDao(CatDao catDao) {
this.catDao = catDao;
}
@Override
public void run() {
System.out.println("CatService is running ...");
catDao.run();
}
}
(3) 在 Spring配置文件 中进行配置
<bean id="catDao" class="com.liao.dao.impl.CatDaoImpl"></bean>
<bean id="catService" class="com.liao.service.impl.CatServiceImpl">
<property name="catDao" ref="catDao"></property>
</bean>
(4) 测试
@Test
public void testCatBean() {
ApplicationContext context = new ClassPathXmlApplicationContext("CatBean.xml");
CatService catService = context.getBean("catService", CatService.class);
catService.run();
}
(5) 输出
3.注入属性-内部 bean
(1) 一对多关系:部门和员工,一个部门有多个员工,一个员工属于一个部门,部门是一,员工是多
(2) 在实体类之间表示一对多关系
public class Dept {
private String name;
private String number;
public void setName(String name) {
this.name = name;
}
public void setNumber(String number) {
this.number = number;
}
@Override
public String toString() {
return "Dept{" +
"name='" + name + '\'' +
", number='" + number + '\'' +
'}';
}
}
public class Emp {
private String name;
private String gender;
private Dept dept;
public void setName(String name) {
this.name = name;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setDept(Dept dept) {
this.dept = dept;
}
@Override
public String toString() {
return "Emp{" +
"name='" + name + '\'' +
", gender='" + gender + '\'' +
", dept=" + dept +
'}';
}
}
(3) 在Spring中进行配置
<bean id="emp" class="com.liao.entity.Emp">
<property name="name" value="张三"></property>
<property name="gender" value="男"></property>
<property name="dept">
<bean id="dept" class="com.liao.entity.Dept">
<property name="name" value="研发部"></property>
<property name="number" value="10010"></property>
</bean>
</property>
</bean>
(4) 测试
@Test
public void testDeptEmpBean() {
ApplicationContext context = new ClassPathXmlApplicationContext("DeptEmpBean.xml");
Emp emp = context.getBean("emp", Emp.class);
System.out.println(emp);
}
(5) 输出结果
4.注入属性-级联赋值
(1) 第一种写法
<bean id="emp" class="com.liao.entity.Emp">
<property name="name" value="张三"></property>
<property name="gender" value="男"></property>
<property name="dept" ref="dept"></property>
</bean>
<bean id="dept" class="com.liao.entity.Dept">
<property name="name" value="研发部"></property>
<property name="number" value="10010"></property>
</bean>
(2) 第二种写法
首先在Emp.class中添加get方法
public Dept getDept() {
return dept;
}
然后修改Spring配置文件
<bean id="emp" class="com.liao.entity.Emp">
<property name="name" value="张三"></property>
<property name="gender" value="男"></property>
<property name="dept" ref="dept"></property>
<property name="dept.name" value="测试部"></property>
<property name="dept.number" value="10000"></property>
</bean>
<bean id="dept" class="com.liao.entity.Dept"></bean>
5.xml注入集合属性
* 注入 数组、List、Set、Map 类型属性
(1) 创建类,定义属性,生成对应 set 方法
public class Collection {
private String[] array;
private List<String> list;
private Set<String> set;
private Map<String,String> map;
public void setArray(String[] array) {
this.array = array;
}
public void setList(List<String> list) {
this.list = list;
}
public void setSet(Set<String> set) {
this.set = set;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
@Override
public String toString() {
return "Collection{" +
"array=" + Arrays.toString(array) +
", list=" + list +
", set=" + set +
", map=" + map +
'}';
}
}
(2) 编写Spring配置类
<bean id="collection" class="com.liao.entity.Collection">
<property name="array">
<array>
<value>张三</value>
<value>李四</value>
</array>
</property>
<property name="list">
<list>
<value>JAVA</value>
<value>PHP</value>
</list>
</property>
<property name="set">
<set>
<value>MySQL</value>
<value>Redis</value>
</set>
</property>
<property name="map">
<map>
<entry key="Tom" value="猫"></entry>
<entry key="Jerry" value="鼠"></entry>
</map>
</property>
</bean>
(3) 测试
@Test
public void testCollection() {
ApplicationContext context = new ClassPathXmlApplicationContext("CollectionBean.xml");
Collection collection = context.getBean("collection", Collection.class);
System.out.println(collection);
}
(4) 输出结果
6.在集合里面设置对象类型值
(1) 创建实体类
public class Teacher {
private String name;
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Teacher{" +
"name='" + name + '\'' +
'}';
}
}
public class Student {
private List<Teacher> teachers;
public void setTeachers(List<Teacher> teachers) {
this.teachers = teachers;
}
@Override
public String toString() {
return "Student{" +
"teachers=" + teachers +
'}';
}
}
(2) 编写Spring配置文件
<bean id="student" class="com.liao.entity.Student">
<property name="teachers">
<list>
<ref bean="teacher1"></ref>
<ref bean="teacher2"></ref>
</list>
</property>
</bean>
<bean id="teacher1" class="com.liao.entity.Teacher">
<property name="name" value="张三"></property>
</bean>
<bean id="teacher2" class="com.liao.entity.Teacher">
<property name="name" value="李四"></property>
</bean>
(3) 测试
@Test
public void testStudentBean() {
ApplicationContext context = new ClassPathXmlApplicationContext("StudentBean.xml");
Student student = context.getBean("student", Student.class);
System.out.println(student);
}
(4) 输出结果
7.把集合注入部分提取出来
(1) 在 Spring 配置文件中引入名称空间 util
* xmlns:util="http://www.springframework.org/schema/util"
* Index of /schema/util http://www.springframework.org/schema/util/spring-util.xsd
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
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
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
(2) 使用 util 标签完成 list 集合注入提取
<bean id="student" class="com.liao.entity.Student">
<property name="teachers" ref="teacherList"></property>
</bean>
<bean id="teacher1" class="com.liao.entity.Teacher">
<property name="name" value="张三"></property>
</bean>
<bean id="teacher2" class="com.liao.entity.Teacher">
<property name="name" value="李四"></property>
</bean>
<util:list id="teacherList">
<ref bean="teacher1"></ref>
<ref bean="teacher2"></ref>
</util:list>
FactoryBean
1.Spring有两种类型bean,一种是普通bean,另外一种是工厂bean(FactoryBean)
2.普通bean:在配置文件中定义bean的类型就是返回的类型
3.工厂bean:在配置文件中定义bean的类型可以和返回的类型不一样
* 第一步,创建类,让这个类作为工厂bean,实现接口FactoryBean
* 第二步,实现接口里面的方法,在实现的方法中定义返回的bean类型
public class MyBean implements FactoryBean<Teacher> {
@Override
public Teacher getObject() throws Exception {
Teacher teacher = new Teacher();
teacher.setName("张三");
return teacher;
}
@Override
public Class<?> getObjectType() {
return null;
}
@Override
public boolean isSingleton() {
return FactoryBean.super.isSingleton();
}
}
<bean id="myBean" class="com.liao.entity.MyBean"></bean>
@Test
public void testMyBean() {
ApplicationContext context = new ClassPathXmlApplicationContext("StudentBean.xml");
Teacher teacher = context.getBean("myBean", Teacher.class);
System.out.println(teacher);
}
Bean作用域
1.在Spring里面,可以设置创建的bean实例时单实例还是多实例
2.在Spring里面,默认情况下,bean是单实例对象
3.如何设置是单实例还是多实例
<bean id="user" class="com.liao.entity.User" scope="singleton"></bean>
* 在Spring配置文件bean标签里面有属性scope用于设置是单实例还是多实例
scope常用值:“默认值-单实例-singleton”、“多实例-prototype”
4.scope值的区别
* singleton:单实例,加载Spring配置文件的时候就会创建单实例对象
* prototype:多实例,在调用getBean方法的时候才会创建多实例对象
* request:创建的实例会放在request中
* session:创建的实例会放在session中
Bean生命周期
*生命周期:从对象创建到对象销毁的过程
1.bean生命周期
(1) 通过构造器创建bean实例(无参构造器)
(2) 为bean的属性设置值和对其它bean引用(调用set方法)
(3) 调用bean的初始化的方法(需要进行配置初始化的方法)
(4) 进行bean的使用(对象获取成功)
(5) 当容器关闭的时候,调用bean的销毁的方法(需要进行配置销毁的方法)
public class Orders {
private String name;
public Orders() {
System.out.println("1.通过构造器创建bean实例(无参构造器)");
}
public void setName(String name) {
this.name = name;
System.out.println("2.为bean的属性设置值和对其它bean引用(调用set方法)");
}
//创建一个执行初始化的方法
public void initMethod() {
System.out.println("3.调用bean的初始化的方法(需要进行配置初始化的方法)");
}
//创建一个执行销毁的方法
public void destroyMethod() {
System.out.println("5.当容器关闭的时候,调用bean的销毁的方法(需要进行配置销毁的方法)");
}
}
<bean id="orders" class="com.liao.bean.Orders" init-method="initMethod" destroy-method="destroyMethod"></bean>
@Test
public void testOrdersBean() {
ApplicationContext context = new ClassPathXmlApplicationContext("OrdersBean.xml");
com.liao.bean.Orders orders = context.getBean("orders", com.liao.bean.Orders.class);
System.out.println("4.进行bean的使用(对象获取成功)");
System.out.println(orders);
//调用ClassPathXmlApplicationContext的close方法关闭容器
((ClassPathXmlApplicationContext) context).close();
}
2.bean的后置处理器
*配置bean的后置处理器后,bean的生命周期有7步
(1) 通过构造器创建bean实例(无参构造器)
(2) 为bean的属性设置值和对其它bean引用(调用set方法)
(3) 把bean实例传递bean后置处理器的方法 postProcessBeforeInitialization
(4) 调用bean的初始化的方法(需要进行配置初始化的方法)
(5) 把bean实例传递bean后置处理器的方法 postProcessAfterInitialization
(6) 进行bean的使用(对象获取成功)
(7) 当容器关闭的时候,调用bean的销毁的方法(需要进行配置销毁的方法)
*演示添加后置处理器的效果(创建类,实现接口BeanPostProcessor,创建后置处理器,并且将该继承接口的bean交给Spring管理)
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("add.在bean初始化之前执行的方法");
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("add.在bean初始化之后执行的方法");
return bean;
}
}
<bean id="orders" class="com.liao.bean.Orders" init-method="initMethod" destroy-method="destroyMethod"></bean>
<bean id="myBeanPostProcessor" class="com.liao.bean.MyBeanPostProcessor"></bean>
@Test
public void testOrdersBean() {
ApplicationContext context = new ClassPathXmlApplicationContext("OrdersBean.xml");
com.liao.bean.Orders orders = context.getBean("orders", com.liao.bean.Orders.class);
System.out.println("4.进行bean的使用(对象获取成功)");
System.out.println(orders);
//调用ClassPathXmlApplicationContext的close方法关闭容器
((ClassPathXmlApplicationContext) context).close();
}
xml自动装配
1.自动装配:根据指定装配规则(属性名称或者属性类型),Spring自动将匹配的属性值进行注入
2.bean标签中的属性autowire,用于配置自动装配
*autowire属性常用两个值:
byName根据属性名称注入,注入值bean的id值和类属性名称一样
byType根据属性类型注入(同一个类型的bean不能配置多个)
<bean id="dept" class="com.liao.autowire.Dept"></bean>
<bean id="emp" class="com.liao.autowire.Emp" autowire="byName"></bean>
外部属性文件
*以配置druid数据库连接池为例
1.直接配置数据库信息
<!--直接配置连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/db_user"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
2.引入外部属性文件配置数据库连接池
(1) 创建外部属性文件,properties格式文件,写数据库信息
prop.driverClassName=com.mysql.jdbc.Driver
prop.url=jdbc:mysql://localhost:3306/db_user
prop.username=root
prop.password=123456
(2) 在 Spring 配置文件中引入名称空间 context
xmlns:context="http://www.springframework.org/schema/context"
Index of /schema/context http://www.springframework.org/schema/context/spring-context.xsd
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
(3) 在 Spring 配置文件中引入外部属性文件,配置bean信息
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${prop.driverClassName}"></property>
<property name="url" value="${prop.url}"></property>
<property name="username" value="${prop.username}"></property>
<property name="password" value="${prop.password}"></property>
</bean>
基于注解方式实现
1.什么是注解
(1) 注解是代码特殊标记,格式:@注解名称(属性名称=属性值,属性名称=属性值,...)
(2) 使用注解,注解可以作用在 类、方法、属性 上面
(3) 使用注解目的:简化xml配置
2.Spring针对Bean管理中创建对象提供注解
* @Component 普通组件、@Repository 持久层、@Service 业务层、@Controller 表现层
* 上面四个注解功能是一样的,都可以用来创建bean实例
3.基于注解方式实现对象创建
(1) 引入依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.3.6</version>
</dependency>
(2) 开启组件扫描
① 在Spring配置文件中引入名称空间context
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
② 在Spring配置文件中开启组件扫描配置
<!--
开启组件扫描,如果有多个包,有两种写法
1.多个包之间用逗号隔开
2.写多个扫描包的上层包,比如写com.liao
-->
<context:component-scan base-package="com.liao.dao,com.liao.service"></context:component-scan>
③ 创建类,在类上面添加创建对象的注解
//在注解里面value属性值可以省略不写
//value默认值是类名称,首字母小写
//UserService -> userService
@Component(value = "userService") //<bean id="userService" class="com.liao.service.UserService"/>
public class UserService {
public void add() {
System.out.println("UserService add ...");
}
}
④ 测试
@Test
public void testAutowire() {
ApplicationContext context = new ClassPathXmlApplicationContext("Bean.xml");
UserService userService = context.getBean("userService", UserService.class);
System.out.println(userService);
userService.add();
}
⑤ 输出结果
4.开启组件扫描细节配置
<!--
【示例】
component-scan base-package="com.liao": 默认会扫描包下所有的类
use-default-filters="false": 表示不使用默认的filter,而是使用自己配置的filter
context:include-filter: 设置扫描哪些内容
type="annotation" expression="org.springframework.stereotype.Controller":
表示只扫描带@Controller注解的类
-->
<context:component-scan base-package="com.liao" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--
【示例】
context:exclude-filter: 设置哪些内容不进行扫描
下面配置扫描com.liao包中除了带有@Controller注解类之外的所有类
-->
<context:component-scan base-package="com.liao">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
基于注解方式实现属性注入
① @Autowired:根据属性类型进行自动装配,spring推荐开发者使用@Autowired
@Autowired //根据类型进行注入
private UserDao userDao;
② @Qualifier:根据属性名称进行注入(@Qualifier要和@Autowired一起使用,因为一个接口可能有多个实现类)
@Autowired //根据类型进行注入
@Qualifier(value = "userDaoImpl") //根据名称进行注入
private UserDao userDao;
③ @Resource:可以根据类型注入,也可以根据名称注入,@Resource不是spring提供的,而是javax提供的
@Resource //根据类型进行注入
private UserDao userDao;
@Resource(name = "userDaoImpl") //根据名称进行注入
private UserDao userDao;
④ @Value:注入普通类型属性
@Value(value = "张三")
private String name;
纯注解开发
1.创建配置类,替代xml配置文件
@Configuration //作为配置类,替代xml配置文件
@ComponentScan(basePackages = {"com.liao.dao","com.liao.service"})
public class SpringConfig {
}
2.测试
@Repository
public class UserDao {
public void add() {
System.out.println("UserDao add ...");
}
}
@Service
public class UserService {
@Autowired
private UserDao userDao;
public void add() {
System.out.println("UserService add ...");
userDao.add();
}
}
@org.junit.Test
public void test() {
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
UserService userService = context.getBean("userService", UserService.class);
userService.add();
}
3.输出结果
Aop面向切面编程
不通过修改源代码的方式,增强或修改功能,降低耦合度
Aop底层原理
Aop底层使用动态代理,有两种情况动态代理:
① 有接口情况,使用JDK动态代理(创建接口实现类的代理对象,增强类的方法)
② 没有接口情况,使用CGLIB动态代理(创建子类的代理对象,增强类的方法)
JDK动态代理
1.使用Proxy类里面的newProxyInstance方法创建代理对象
//返回指定接口的代理类的实例,该接口将方法调用分派给指定的调用处理程序
static Object newProxyInstance(ClassLoader loader, 类<?> interfaces, InvocationHandler h)
该方法有三个参数:
① 类加载器
② 增强方法所在的类实现的接口,支持多个接口
③ 实现InvocationHandler接口,创建代理对象,编写增强的方法
2.编写JDK动态代理代码
① 创建接口,定义方法
public interface UserDao {
int add(int a, int b);
String update(String id);
}
② 创建接口实现类,实现方法
public class UserDaoImpl implements UserDao {
@Override
public int add(int a, int b) {
System.out.println("UserDaoImpl add ...");
return a + b;
}
@Override
public String update(String id) {
System.out.println("UserDaoImpl update ...");
return "模板_" + id;
}
}
③ 使用Proxy类创建接口代理对象
public class JdkProxy {
public void test() {
Class[] interfaces = {UserDao.class};
UserDao dao = new UserDaoImpl();
UserDao userDao = (UserDao) Proxy.newProxyInstance(JdkProxy.class.getClassLoader(), interfaces, new UserDaoProxy(dao));
int add = userDao.add(10, 20);
System.out.println("add:" + add);
String update = userDao.update("001");
System.out.println("update:" + update);
System.out.println(userDao);
}
//创建代理对象代码
class UserDaoProxy implements InvocationHandler {
//创建的是谁的代理对象,把谁传递过来
//有参数构造传递
private Object obj;
public UserDaoProxy(Object obj) {
this.obj = obj;
}
//增强的逻辑
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//方法执行之前
System.out.println("Start: method->"+method.getName()+",args->"+ Arrays.toString(args));
//被增强的方法执行
Object result = method.invoke(obj, args);
//方法之后
System.out.println("End: obj->"+obj);
return result;
}
}
}
④ 输出结果
Aop术语
1.连接点
类里面哪些方法可以被增强,这些方法称为连接点
2.切入点
实际被真正增强的方法,称为切入点
3.通知(增强)
实际增强的逻辑部分称为通知(增强)
通知有多种类型:
① 前置通知
② 后置通知
③ 环绕通知
④ 异常通知
⑤ 最终通知:不管有没有异常都会执行
4.切面
是个动作,把通知应用到切入点的过程
Aop操作 (准备)
1.Spring框架一般都是基于AspectJ实现Aop操作
AspectJ不是Spring的组成部分,而是一个独立的Aop框架,不过一般都把AspectJ和Spring框架一起使用,进行Aop操作
2.基于AspectJ实现Aop操作
① 基于xml配置文件实现
② 基于注解方式实现(推荐)
3.在项目工程里引入Aop相关依赖
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
</dependency>
切入点表达式
作用:知道对哪个类里面的哪个方法进行增强
语法结构:execution([权限修饰符] [返回类型] [类全路径] [方法名称] [参数列表])
//1.对com.liao.dao.BookDao类里面的add进行增强
execution(* com.liao.dao.BookDao.add(..))
//2.对com.liao.dao.BookDao类里面所有的方法进行增强
execution(* com.liao.dao.BookDao.*(..))
//3.对com.liao.dao包里面的所有的类,类里面所有的方法进行增强
execution(* com.liao.dao.*.*(..))
AspectJ注解
1.创建类,在类里面定义方法
//被增强的类
public class User {
public void add() {
System.out.println("User add in running ...");
}
}
2.创建增强类(编写增强逻辑)
在增强类里面,创建方法,让不同方法代表不同通知类型
//增强的类
public class UserProxy {
public void before() {
System.out.println("UserProxy before in running ...");
}
}
3.进行通知的配置
① 在Spring配置文件中,开启注解扫描
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.liao.aop"></context:component-scan>
</beans>
② 使用注解创建User和UserProxy对象(分别加上@Component注解)
③ 在增强类上面添加注解@Aspect
④ 在Spring配置文件中开启生成代理对象
<!--开启Aspect生成代理对象-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
4.配置不同类型的通知
在增强类的里面,在作为通知的方法上面添加通知类型注解,使用切入点表达式配置
@Component
@Aspect //生成代理对象
public class UserProxy {
//前置通知
//@Before注解表示作为前置通知
@Before(value = "execution(* com.liao.aop.User.add(..))")
public void before() {
System.out.println("UserProxy before in running ...");
}
}
5.测试
@org.junit.Test
public void testAop() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
User user = context.getBean("user", User.class);
user.add();
}
6.输出结果
5种通知演示
@Component
@Aspect //生成代理对象
public class UserProxy {
//前置通知
//@Before注解表示作为前置通知
@Before(value = "execution(* com.liao.aop.User.add(..))")
public void before() {
System.out.println("before ...");
}
//后置通知(返回通知),有异常时不会执行
@AfterReturning(value = "execution(* com.liao.aop.User.add(..))")
public void afterReturning() {
System.out.println("afterReturning ...");
}
//异常通知
@AfterThrowing(value = "execution(* com.liao.aop.User.add(..))")
public void afterThrowing() {
System.out.println("afterThrowing ...");
}
//环绕通知
@Around(value = "execution(* com.liao.aop.User.add(..))")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("around before ...");
//被增强的方法执行
proceedingJoinPoint.proceed();
System.out.println("around after ...");
}
//最终通知,不管有没有异常都会执行
@After(value = "execution(* com.liao.aop.User.add(..))")
public void after() {
System.out.println("after ...");
}
}
正常输出结果:
出现异常后的输出结果:
抽取相同的切入点
//相同切入点抽取
@Pointcut(value = "execution(* com.liao.aop.User.add(..))")
public void pointCutDemo() {
}
//前置通知
//@Before注解表示作为前置通知
@Before(value = "pointCutDemo()")
public void before() {
System.out.println("before ...");
}
有多个增强类对同一个方法进行增强,设置增强类优先级
在增强类上面添加注解@Order(数字类型值),数字类型值越小优先级越高
@Component
@Aspect //生成代理对象
@Order(1) //设置增强类优先级
public class UserProxy {
...
}
AspectJ配置文件
1.创建两个类,增强类和被增强类,创建方法
public class Book {
public void buy() {
System.out.println("buy ...");
}
}
public class BookProxy {
public void before() {
System.out.println("before ...");
}
}
2.在Spring配置文件中创建两个类的对象
<bean id="book" class="com.liao.xml.Book"></bean>
<bean id="bookProxy" class="com.liao.xml.BookProxy"></bean>
3.在Spring配置文件中配置切入点
<!--配置aop增强-->
<aop:config>
<!--切入点-->
<aop:pointcut id="p" expression="execution(* com.liao.xml.Book.buy(..))"/>
<!--配置切面-->
<aop:aspect ref="bookProxy">
<!--增强作用在具体的方法上-->
<aop:before method="before" pointcut-ref="p"></aop:before>
</aop:aspect>
</aop:config>
4.测试
@org.junit.Test
public void testXml() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
Book book = context.getBean("book", Book.class);
book.buy();
}
5.输出结果
纯注解开发
@Configuration
@ComponentScan(basePackages = {"com.liao.aop"})
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class AopConfig {
}