spring面向切面AOP编程
一:AOP概念
AOP即面向切面编程,是面向对象编程(OOP)的一种补充。在OOP中只能实现父子关系的纵向的代码重用,AOP采取横向抽取,将分散在各个方法中的重复代码提取出来通过动态代理应用到各个方法中。
AOP名词理解(不需过分记忆及理解,懂AOP的实现及用法即可):
- 横切关注点:我们要关注的功能,即横向抽取分散在各个方法中的重复功能,如日志,权限等
- 切面(Aspect):横切关注点 被完整定义模块,如日志切面,即它是一个类
- 通知(Advice):切面要完成的功能,如日志功能方法,即它是类中的一个方法
- 目标对象(Target):被切面切的对象。真正的业务逻辑
- 代理(proxy):实现AOP的一种原理。对目标对象进行代理功能增强
- 连接点(Join point):能够植入切面的执行点,比如方法的前后,抛出异常时都可以是连接点
- 切入点(Pointcut):针对哪些连接点植入通知,也就是指定具体的拦截地点。
- 引入(Introduction):对目标类添加新方法及属性
- 织入(Weaving):把切面应用到目标对象来创建新的代理对象的过程。
AOP的5类通知类型:
- 前置通知:方法前通知,实现接口MethodBeforeAdvice
- 后置通知:方法后通知,实现接口AfterReturningAdvice
- 环绕通知:方法前后通知,实现接口MethodInterceptor
- 抛出异常后通知:方法抛出异常后通知,实现接口ThrowsAdvice
- 引介通知:类中增加新的方法和属性,实现接口IntroductionInterceptor
Aop应用场景:
1.权限验证
2.性能追踪
3.日志追踪
4.异常处理
5.事务 => Spring 的声明式事务管理
6.缓存
7.懒加载
二:AOP实现方式一:使用原生Spring API接口(主要SpringAPI接口实现)
引入面向切面编程的jar包:
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.9.1</version>
</dependency>
目标接口:
package com.lmy.service;
/**
* @author 86152
*/
public interface UserService {
/**
* 增加
*/
void add();
/**
* 删除
*/
void delete();
/**
* 修改
*/
void update();
/**
* 查询
*/
void select();
}
目标类:
package com.lmy.service.impl;
import com.lmy.service.UserService;
/**
* @author : liu ming yong
* @date : 2022/7/23 下午 8:27
* @description : 增删改查
*/
public class UserServiceImpl implements UserService {
public void add() {
System.out.println("增加");
}
public void delete() {
System.out.println("删除");
}
public void update() {
System.out.println("修改");
}
public void select() {
System.out.println("查询");
}
}
配置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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册bean-->
<bean id="userService" class="com.lmy.service.impl.UserServiceImpl"/>
<bean id="beforeLog" class="com.lmy.aop.BeforeLog"/>
<bean id="afterLog" class="com.lmy.aop.AfterLog"/>
<!--配置aop:需要导入aop的约束-->
<aop:config>
<!--切入点: expression:表达式,execution(要执行切入的目标对象位置 * * * * *)-->
<aop:pointcut id="pointcut" expression="execution(* com.lmy.service.impl.UserServiceImpl.*(..))"/>
<!--执行增强-->
<aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
前置通知类(切面):
package com.lmy.aop;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
/**
* @author : liu ming yong
* @date : 2022/7/23 下午 8:32
* @description : 前置增强
*/
public class BeforeLog implements MethodBeforeAdvice {
/**
*
* @param method: 要执行的目标对象的方法
* @param args:要执行的目标对象的方法的参数
* @param target:目标对象
* @throws Throwable
*/
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"执行了方法"+method.getName());
}
}
后置通知类(切面):
package com.lmy.aop;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
/**
* @author : liu ming yong
* @date : 2022/7/23 下午 8:40
* @description : 后置通知
*/
public class AfterLog implements AfterReturningAdvice {
/**
* @param returnValue:要执行的目标对象的方法的返回值
* @param method: 要执行的目标对象的方法
* @param args:要执行的目标对象的方法的参数
* @param target:目标对象
* @throws Throwable
*/
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"执行了方法"+method.getName()+",返回值为:"+returnValue);
}
}
测试类:
package com.lmy;
import com.lmy.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author : liu ming yong
* @date : 2022/7/23 下午 8:59
* @description :
*/
public class MyTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 注意,这里必须向上转型为接口UserService,而不能是实现类UserServiceImpl,因为jdk动态代理是针对接口代理
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
}
结果输出:
com.lmy.service.impl.UserServiceImpl执行了方法add
增加
com.lmy.service.impl.UserServiceImpl执行了方法add,返回值为:null
三:AOP实现方式二:自定义类实现AOP(主要是切面定义)
引入面向切面编程的jar包:
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.9.1</version>
</dependency>
目标接口:
package com.lmy.service;
/**
* @author 86152
*/
public interface UserService {
/**
* 增加
*/
void add();
/**
* 删除
*/
void delete();
/**
* 修改
*/
void update();
/**
* 查询
*/
void select();
}
目标类:
package com.lmy.service.impl;
import com.lmy.service.UserService;
/**
* @author : liu ming yong
* @date : 2022/7/23 下午 8:27
* @description : 增删改查
*/
public class UserServiceImpl implements UserService {
public void add() {
System.out.println("增加");
}
public void delete() {
System.out.println("删除");
}
public void update() {
System.out.println("修改");
}
public void select() {
System.out.println("查询");
}
}
配置文件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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册bean-->
<bean id="userService" class="com.lmy.service.impl.UserServiceImpl"/>
<bean id="log" class="com.lmy.aop.Log"/>
<aop:config>
<!--自定义切面,ref:要引用的类(切面)-->
<aop:aspect ref="log">
<!--切入点-->
<aop:pointcut id="pointcut" expression="execution(* com.lmy.service.impl.UserServiceImpl.*(..))"/>
<!--通知-->
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
</beans>
通知类(切面):
package com.lmy.aop;
/**
* @author : liu ming yong
* @date : 2022/7/24 下午 5:28
* @description : 增强类(切面)
*/
public class Log {
void before(){
System.out.println("在方法前执行");
}
void after(){
System.out.println("在方法后执行");
}
}
测试类:
package com.lmy;
import com.lmy.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author : liu ming yong
* @date : 2022/7/24 下午 5:39
* @description : AOP测试-自定义类方式
*/
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) context.getBean("userService");
userService.delete();
}
}
结果输出:
在方法前执行
删除
在方法后执行
四:AOP实现方式三:使用注解方式实现
引入面向切面编程的jar包:
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.9.1</version>
</dependency>
目标接口:
package com.lmy.service;
/**
* @author 86152
*/
public interface UserService {
/**
* 增加
*/
void add();
/**
* 删除
*/
void delete();
/**
* 修改
*/
void update();
/**
* 查询
*/
void select();
}
目标类:
package com.lmy.service.impl;
import com.lmy.service.UserService;
/**
* @author : liu ming yong
* @date : 2022/7/23 下午 8:27
* @description : 增删改查
*/
public class UserServiceImpl implements UserService {
public void add() {
System.out.println("增加");
}
public void delete() {
System.out.println("删除");
}
public void update() {
System.out.println("修改");
}
public void select() {
System.out.println("查询");
}
}
配置文件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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册bean-->
<bean id="userService" class="com.lmy.service.impl.UserServiceImpl"/>
<bean id="log" class="com.lmy.aop.Log"/>
<!--开启注解支持 默认JDK代理(proxy-target-class="false") 设置为true则为cglib代理-->
<aop:aspectj-autoproxy/>
</beans>
通知类(切面):
package com.lmy.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
/**
* @author : liu ming yong
* @date : 2022/7/24 下午 9:36
* @description : 通知类(切面)
*/
@Aspect // 标注这个类为切面
public class Log {
@Before("execution(* com.lmy.service.impl.UserServiceImpl.*(..))")
void before() {
System.out.println("在方法前执行============");
}
@After("execution(* com.lmy.service.impl.UserServiceImpl.*(..))")
void after() {
System.out.println("在方法后执行===========");
}
/**
* 环绕增强:ProceedingJoinPoint参数可获取切入点并相关处理
* @param joinPoint
* @throws Throwable
*/
@Around("execution(* com.lmy.service.impl.UserServiceImpl.*(..))")
void around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("环绕执行(前)========");
// 执行方法
Object proceed = joinPoint.proceed();
// 获取切入点方法的名字
String name = joinPoint.getSignature().getName();
System.out.println("切入点方法的名字:"+name);
// 获取目标对象
Object target = joinPoint.getTarget();
System.out.println("目标对象:"+target);
System.out.println("环绕执行(后)========");
}
}
测试类:
package com.lmy;
import com.lmy.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author : liu ming yong
* @date : 2022/7/24 下午 9:42
* @description : AOP注解方式测试
*/
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) context.getBean("userService");
userService.select();
}
}
结果输出:
环绕执行(前)========
在方法前执行============
查询
在方法后执行===========
切入点方法的名字:select
目标对象:com.lmy.service.impl.UserServiceImpl@7526515b
环绕执行(后)========