AOP
面向切面编程
在Spring中,提供声明式事务,允许自定义切面
- 横切关注点:跨越app多个模块的方法或功能,与业务逻辑无关(日志、安全、缓存、事务等等)
- 切面:横切关注点被模块化的特殊对象,类
- 通知:切面必须完成的工作,类中的方法
- 日志:被通知的对象
- 代理:向目标对象应用通知之后创建的对象
- 切入点:切面通知执行的“地点”的定义
- 连接点:与切入点匹配的执行点
使用Spring 实现 AOP
导包
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.9.1</version>
</dependency>
方式一:使用Spring的接口
SpringAPI接口实现
-
接口 public interface UserService{
public void add();
public void delete();
public void update();
public void query();
}
-
实现类 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 query() {
System.out.println("查找商品");
}
}
-
切入日志功能 import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class Log implements MethodBeforeAdvice {
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName() + "的" + method.getName() + "被执行了");
}
}
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了" + method.getName() + "方法,返回结果为:" + returnValue);
}
}
-
配置 applicationContext.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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userService" class="com.yl.service.UserServiceImpl"/>
<bean id="log" class="com.yl.log.Log"/>
<bean id="afterLog" class="com.yl.log.AfterLog"/>
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.yl.service.UserServiceImpl.*(..))"/>
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
-
测试 import com.yl.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ApplicationTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) context.getBean("userService");
userService.add();
userService.query();
}
}
aop --> 动态代理
方式二:自定义实现AOP
切面定义
public class DiyPointCut {
public void before(){
System.out.println("方法执行前...");
}
public void after(){
System.out.println("方法执行后...");
}
}
<bean id="diy" class="com.yl.diy.DiyPointCut"/>
<aop:config>
<aop:aspect ref="diy">
<aop:pointcut id="point" expression="execution(* com.yl.service.UserServiceImpl.*(..))"/>
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
import com.yl.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ApplicationTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) context.getBean("userService");
userService.add();
userService.query();
}
}
方式三:使用注解实现
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.yl.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("方法执行前...");
}
@After("execution(* com.yl.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("方法执行后...");
}
@Around("execution(* com.yl.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("环绕前");
Signature signature = joinPoint.getSignature();
System.out.println("signature:" + signature);
Object proceed = joinPoint.proceed();
System.out.println("环绕后");
}
}
<bean id="annotationPointCut" class="com.yl.diy.AnnotationPointCut"/>
<aop:aspectj-autoproxy/>
public class ApplicationTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
}
在不影响原来业务类的情况下,实现动态增强
|