Spring07:AOP
AOP:面向切面编程
Aop 在 不改变原有代码的情况下 , 去增加新的功能
通过 Spring API 实现AOP
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
</dependency>
</dependencies>
public interface UserService {
public void add();
public void delete();
public void update();
public void search();
}
目标业务类
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 search() {
System.out.println("查询用户");
}
}
public class Log implements MethodBeforeAdvice {
//method : 要执行的目标对象的方法
//objects : 被调用的方法的参数
//Object : 目标对象
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println(o.getClass().getName()+"的"+method.getName()+"方法被执行了");
}
}
public class AfterLog implements AfterReturningAdvice {
//returnValue 返回值
//method被调用的方法
//args 被调用的方法的对象的参数
//target 被调用的目标对象
public void afterReturning(Object returnValue, Method method, Object[] objects, Object target) throws Throwable {
System.out.println("执行了"+target.getClass().getName()+"的"+method.getName()+"方法,返回值:"+returnValue);
}
}
beans.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.wgg.service.UserServiceImpl"/>
<bean id="log" class="com.wgg.log.Log"/>
<bean id="afterLog" class="com.wgg.log.AfterLog"/>
<!--aop的配置-->
<aop:config>
<!--切入点,*后跟空格-->
<aop:pointcut id="pointcut" expression="execution(* com.wgg.service.UserServiceImpl.*(..))"/>
<!--切入点执行-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
测试
public class MyTest {
@Test
public void test(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserService userService = (UserService) context.getBean("userService");
userService.update();
}
}
自定义类来实现Aop
自定义切入类
public class DiyPointcut {
public void before(){
System.out.println("----------方法执行前---------------");
}
public void after(){
System.out.println("-----------方法执行后----------------");
}
}
<!--注册bean-->
<bean id="userService" class="com.wgg.service.UserServiceImpl"/>
<bean id="diy" class="com.wgg.config.DiyPointcut"/>
<!--aop的配置-->
<aop:config>
<aop:aspect ref="diy">
<aop:pointcut id="diyPointcut" expression="execution(* com.wgg.service.UserServiceImpl.*(..))"/>
<aop:before pointcut-ref="diyPointcut" method="before"/>
<aop:after pointcut-ref="diyPointcut" method="after"/>
</aop:aspect>
</aop:config>
测试
public class MyTest {
@Test
public void test(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
}
使用注解实现
使用注解实现的增强类
@Aspect
public class AnnotationPointcut {
@Before("execution(* com.wgg.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("-------方法执行前-----------");
}
@After("execution(* com.wgg.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("-----------方法执行后-----------");
}
@Around("execution(* com.wgg.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="userService" class="com.wgg.service.UserServiceImpl"/>
<bean id="annotationPointcut" class="com.wgg.config.AnnotationPointcut"/>
<aop:aspectj-autoproxy/>
测试
@Test
public void test(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserService userService = (UserService) context.getBean("userService");
userService.add();
}