spring(七)AOP
7、AOP
7.1 什么是AOP
7.2 AOP在spring中的作用
提供生命事务:允许用户自定义切面
-
横切关注点:跨越应用程序多个模块的方法或功能。即与我们的业务逻辑无关的,但是我们需要关注的部分,就是
-
横切关注点。如日志,安全,缓存,事务等等。。。
-
切面(ASPECT):横切关注点 被模块化的特殊对象。即 它是一个类
-
通知(Advice):切面必须要完成的工作,即 他是类中的一个方法
-
目标(target):被通知的对象
-
代理(Proxy):向目标对象应用通知之后创建的对象
-
切入点(PointCut):切面通知 执行的"地点"的定义
-
连接点(jointPoint):与切入点匹配的执行点
SpringAop中,通过Advice定义横切逻辑,Spring中支持的5种类型的Advice
7.3 使用spring实现AOP
【重点】使用AOP织入,需要依赖包
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
在spring中有三种方式实现AOP
环境:
eg:在执行UserService实现类的所有方法时,增加日志功能
UserService接口
public interface UserService {
void add();
void update();
void delete();
void select();
}
UserService实现类
public class UserServiceImpl implements UserService{
public void add() {
System.out.println("增加了一个用户");
}
public void update() {
System.out.println("更新了一个用户");
}
public void delete() {
System.out.println("删除了一个用户");
}
public void select() {
System.out.println("检索了一个用户");
}
}
测试
@Test
public void test(){
ApplicationContext con = new ClassPathXmlApplicationContext("applicationContext.xml");
// 动态代理代理的时接口
UserService service = con.getBean("userService", UserService.class);
service.add();
}
方式一:使用spring的API接口
Log类
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class Log implements MethodBeforeAdvice, AfterReturningAdvice {
//method:要执行的目标对象的方法(method being invoked)
//object:参数(args: arguments to the method)
//o:目标对象 (target:target of the method invocation)
@Override
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println(o.getClass().getName()+"的"+method.getName()+"被执行了");
}
@Override
public void afterReturning(Object returnValue, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("执行了" + method.getName() + "方法,返回值为" + returnValue);
}
}
配置文件:
<?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.wen.service.UserServiceImpl"/>
<bean id="log" class="com.wen.log.Log"/>
<!--配置AOP:需要导入aop头文件命名空间约束-->
<aop:config>
<!--切入点:expression:表达式,execution(*(修饰词) *(返回值) *(类名) *(方法名) *(参数)) ..任意参数-->
<aop:pointcut id="pointcut" expression="execution(* com.wen.service.UserServiceImpl.*(..))"/>
<!--执行环绕增加-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
</aop:config>
</beans>
方式二:自定义来实现AOP【主要是切面定义】
自定义类
public class DIYPointCut {
public void beforeMethod () {
System.out.println("方法执行之前");
}
public void afterMethod(){
System.out.println("方法执行后");
}
}
配置文件
<?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.wen.service.UserServiceImpl"/>
<bean id="diy" class="com.wen.DIYPointCut"/>
<aop:config>
<!--<aop:aspect ref="diy"> : 标注这个类为切面-->
<aop:aspect ref="diy">
<!--切入点-->
<aop:pointcut id="point" expression="execution(* com.wen.service.UserServiceImpl.*(..))"/>
<!--通知-->
<aop:before method="beforeMethod" pointcut-ref="point"/>
<aop:after method="afterMethod" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
</beans>
方式三:使用注解实现AOP
减少xml使用,更多使用java实现
注解实现代码
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect // 标注这个类是一个切面
public class AnnotationPointCut {
@Before("execution(* com.wen.service.UserServiceImpl.*(..))")
public void before () {
System.out.println("====方法执行前====");
}
@After("execution(* com.wen.service.UserServiceImpl.*(..))")
public void after () {
System.out.println("====方法执行后====");
}
//在环绕增强中,我们可以给定一个参数,代表我们要获取处理切入的点
@Around("execution(* com.wen.service.UserServiceImpl.*(..))")
public void around (ProceedingJoinPoint pjp) throws Throwable {
System.out.println("环绕前");
Signature signature = pjp.getSignature();//获得签名
System.out.println("signature" + signature);
Object proceed = pjp.proceed();//执行方法
System.out.println("环绕后");
}
}
配置文件
<?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.wen.service.UserServiceImpl"/>
<bean id="anno" class="com.wen.AnnotationPointCut"/>
<!--开启注解支持 JDK(默认proxy-target-class="false")cglib默认proxy-target-class="true")-->
<aop:aspectj-autoproxy proxy-target-class="false"/>
<bean id="annotationPointCut" class="com.wen.AnnotationPointCut"/>
</beans>