(四)Spring-AOP
一、AOP背景知识
1.1 相关概念
1 横切关注点:
即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点,如日志,安全,缓存,事务等等...
2 切面(ASPECT)
横切关注点,被模块化的特殊对象。即,它是一个类。比如,专注于完成日志的Log类
3 通知(Advice)
切面必须要完成的工作。即,它是类中的一个方法。比如,专注于完成日志的Log类的一个方法。
4 目标(Target)
被通知对象。
5 代理(Proxy)
6 切入点(pointcut)
切面通知执行的“地点”的定义。说白了就是在哪个地方执行,如jdk动态代理中InvocationHandler的invoke()方法
7 连接点(JointPoint)
与切入点匹配的执行点。
1.2 Spring中5种Advice
在spring中,通过advice定义横切逻辑,spring中支持5种类型的advice
通知类型 | 连接点 | 实现接口 |
---|---|---|
前置通知 | 方法前 | org.springframework.aop.MethodBeforeAdvice |
后置通知 | 方法后 | org.springframework.aop.AfterReturningAdvice |
环绕通知 | 方法前后 | org.aopalliance.intercept.MethodInterceptor |
异常抛出通知 | 方法抛出异常 | org.springframework.aop.ThrowsAdvice |
引介通知 | 类中增强新的方法属性 | org.springframework.aop.IntroductionIntercepter |
二、使用Spring实现AOP
2.1 导入相关依赖或者包
使用AOP织入,需要导入一个依赖包
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
2.2 实现AOP的3个方式
2.2.1 方式一:使用spring的AOP接口
在application-context注册对象和aop切面以及切点,spring会自动帮拦截目标方法
1 定义目标(被代理)的接口
package com.happy.service;
public interface UserService {
void add();
void delete();
void update();
void query();
}
2 编写目标类,实现接口
package com.happy.service.impl;
import com.happy.service.UserService;
public class UserServiceImpl implements UserService {
@Override
public void add() {
System.out.println("增加了一个用户");
}
@Override
public void delete() {
System.out.println("删除了一个用户");
}
@Override
public void update() {
System.out.println("更新了一个用户");
}
@Override
public void query() {
System.out.println("查询了一个用户");
}
}
3 编写切面(类)和advice(方法)
package com.happy.log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
//这个类是切面
public class BeforeLog implements MethodBeforeAdvice {
//这个方法是通知
//spring会在我们执行目标方法前自动调用
//method为要执行的目标对象的方法
//target标志目标对象
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass()+"的"+method.getName()+"开始执行前记录日志");
}
}
package com.happy.log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
//一个切面
public class AfterLog implements AfterReturningAdvice {
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass()+"的"+method.getName()+"执行结束后记录日志");
System.out.println("返回结果为->"+returnValue);
}
}
4 注册目标类(被代理类)、切面以及切点
在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"
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/context https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册bean-->
<bean id="beforeLog" class="com.happy.log.BeforeLog"></bean>
<bean id="afterLog" class="com.happy.log.AfterLog"></bean>
<bean id="userService" class="com.happy.service.impl.UserServiceImpl"></bean>
<!--方式一:使用原生的spring api接口-->
<!--配置aop,需要导入aop的约束-->
<aop:config>
<!--需要一个切入点,即在哪里执行我们spring的方法-->
<!--需要一个表达式expression:表达式。execution要执行的位置!表示会在这个地方执行-->
<aop:pointcut id="pointcut" expression="execution(* com.happy.service.impl.UserServiceImpl.*(..) )"/>
<!--执行环绕增加!-->
<aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"></aop:advisor>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"></aop:advisor>
</aop:config>
<context:annotation-config></context:annotation-config>
</beans>
5 执行目标方法
package com.happy.service;
import com.happy.service.impl.UserServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClasspathXmlApplicationContext;
public class UserServiceTest {
@Test
public void test() {
ApplicationContext context = new ClasspathXmlApplicationContext("application-context.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.add();
System.out.println("==============自己new对象=================");
//必须用容器管理的bean才能执行advice方法
UserServiceImpl userService2 = new UserServiceImpl();
userService2.add();
}
}
2.2.2 方式二:使用自定义切面
1 定义目标(被代理)的接口
同方式一
2 编写目标类
同方式一
3 编写自定义切面Aspect
package com.happy.diyAspect;
public class MyAspect {
public void before(){
System.out.println("div myApsect============方法执行前");
}
public void after(){
System.out.println("diy myApsect============方法执行后");
}
}
4 注册目标类、切面以及切点
同方式一,也在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"
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/context https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册bean-->
<bean id="beforeLog" class="com.happy.log.BeforeLog"></bean>
<bean id="afterLog" class="com.happy.log.AfterLog"></bean>
<bean id="userService" class="com.happy.service.impl.UserServiceImpl"></bean>
<!--方式一:使用原生的spring api接口-->
<!--配置aop,需要导入aop的约束-->
<!-- <aop:config>
<!–需要一个切入点,即在哪里执行我们spring的方法–>
<!–需要一个表达式expression:表达式。execution要执行的位置!表示会在这个地方执行–>
<aop:pointcut id="pointcut" expression="execution(* com.happy.service.impl.UserServiceImpl.*(..) )"/>
<!–执行环绕增加!–>
<aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"></aop:advisor>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"></aop:advisor>
</aop:config>-->
<!--方式二:自定义aspect-->
<bean id="myAspect" class="com.happy.diyAspect.MyAspect"></bean>
<aop:config>
<aop:aspect id="myAspect" ref="myAspect">
<aop:pointcut id="pointcut" expression="execution(* com.happy.service.impl.UserServiceImpl.*(..) )"/>
<aop:before method="before" pointcut-ref="pointcut"></aop:before>
<aop:after method="after" pointcut-ref="pointcut"></aop:after>
</aop:aspect>
</aop:config>
<context:annotation-config></context:annotation-config>
</beans>
5 执行目标方法
package com.happy.service;
import com.happy.service.impl.UserServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClasspathXmlApplicationContext;
public class UserServiceTest {
@Test
public void test() {
ApplicationContext context = new ClasspathXmlApplicationContext("application-context.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.add();
System.out.println("==============自己new对象=================");
//必须用容器管理的bean才能执行advice方法
UserServiceImpl userService2 = new UserServiceImpl();
userService2.add();
}
}
2.2.3 方式三:使用注解
1 编写注解模式的自定义切面
package com.happy.annotationAspect;
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;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class MyAnnotationAspect {
@Before("execution(* com.happy.service.impl.UserServiceImpl.* (..))")
public void before(){
System.out.println("=="+this.getClass().getName()+"执行before方法");
}
@After("execution(* com.happy.service.impl.UserServiceImpl.* (..))")
public void after(){
System.out.println("=="+this.getClass().getName()+"执行after方法");
}
@Around("execution(* com.happy.service.impl.UserServiceImpl.* (..))")
public void around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("===="+this.getClass().getName()+"执行环绕around方法1====");
System.out.println("signature->"+joinPoint.getSignature());
Object proceed = joinPoint.proceed();
System.out.println("===="+this.getClass().getName()+"执行环绕around方法2====");
}
}
2 开启注解模式的aop
<?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"
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/context https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册bean-->
<bean id="beforeLog" class="com.happy.log.BeforeLog"></bean>
<bean id="afterLog" class="com.happy.log.AfterLog"></bean>
<bean id="userService" class="com.happy.service.impl.UserServiceImpl"></bean>
<!--开启注解支持-->
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.happy"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
3 执行目标方法
4 spring支持的两种动态代理模式
proxy-target-class="false"-->jdk,默认!
proxy-target-class="true"-->cglib
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
区别在于cglib不要求被代理类实现接口。而jdk要求被代理类必须先定义核心业务接口,再实现核心业务接口。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。