AOP意为面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护
在SpringAOP中,通过通知定义横切逻辑,可以在不改变原有代码的情况下 , 去增加新的功能
导入相关Maven依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.13</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.8.RC3</version>
</dependency>
</dependencies>
通过 Spring API 实现
业务接口和实现类
package com.dream.service;
public interface UserService {
public void add();
public void delete();
public void update();
public void search();
}
package com.dream.service;
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 search() {
System.out.println("查询用户");
}
}
前置增强和后置增强
package com.dream.log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class Log implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println( target.getClass().getName() + "的" + method.getName() + "方法被执行了");
}
}
package com.dream.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().getName()
+"的"+method.getName()+"方法,"
+"返回值:"+returnValue);
}
}
注册bean和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: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 id="userService" class="com.dream.service.UserServiceImpl"/>
<bean id="log" class="com.dream.log.Log"/>
<bean id="afterLog" class="com.dream.log.AfterLog"/>
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.dream.service.UserServiceImpl.*(..))"/>
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
测试
import com.dream.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserService userService = (UserService) context.getBean("userService");
userService.search();
}
}
自定义类来实现
自定义织入类
package com.dream.diy;
public class DiyPointcut {
public void before(){
System.out.println("---------方法执行前---------");
}
public void after(){
System.out.println("---------方法执行后---------");
}
}
<bean id="diy" class="com.dream.diy.DiyPointcut"/>
<aop:aspect ref="diy">
<aop:pointcut id="diyPonitcut" expression="execution(* com.dream.service.UserServiceImpl.*(..))"/>
<aop:before pointcut-ref="diyPonitcut" method="before"/>
<aop:after pointcut-ref="diyPonitcut" method="after"/>
</aop:aspect>
使用注解实现
package com.dream.diy;
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;
@Aspect
public class AnnotationPointcut {
@Before("execution(* com.dream.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("---------注解实现方法执行前---------");
}
@After("execution(* com.dream.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("---------注解实现方法执行后---------");
}
@Around("execution(* com.dream.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint jp) throws Throwable {
System.out.println("环绕前");
System.out.println("签名:"+jp.getSignature());
Object proceed = jp.proceed();
System.out.println("环绕后");
System.out.println(proceed);
}
}
注册bean并增加对注解的支持
<bean id="annotationPointcut" class="com.dream.diy.AnnotationPointcut"/>
<aop:aspectj-autoproxy/>