AOP

时间:2021-06-28 18:15:24   收藏:0   阅读:0

AOP

使用Spring实现AOP【重点】

使用AOP,需要导入一个依赖包

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.6</version>
</dependency>

AOP方式一: Spring的API接口实现

接口

public interface UserService {
    void add();
    void delete();
    void update();
    void select();
}

接口实现类

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 select() {
        System.out.println("查询了一个用户!");
    }
}

日志工具类
方法前

public class Log implements MethodBeforeAdvice {

    /*
        method:要执行的目标对象的方法
        args: 参数
        target: 目标对象
     */
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName()+"执行了: "+method.getName()+"方法,参数为: "+args);
    }
}

方法后

public class AfterLog implements AfterReturningAdvice {
    /*
        returnValue: 返回的结果
        method:要执行的目标对象的方法
        args: 参数
        target: 目标对象
     */
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) 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
                           https://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.saxon.service.UserServiceImpl"/>
    <bean id="log" class="com.saxon.log.Log"/>
    <bean id="afterLog" class="com.saxon.log.AfterLog"/>
    <!--
方式一:使用Spring API接口
配置aop需要:导入aop的约束
  xmlns:aop="http://www.springframework.org/schema/aop"
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop.xsd
 -->
    <aop:config>
        <!--
        切入点:pointcut
        要执行的位置: expression()表达式 * * * * * 修饰词 返回值 类名 方法名 参数
        execution(* com.dada.service.UserServiceImpl.*(..)  UserServiceImpl类的任意方法,任意的参数
        -->
        <aop:pointcut id="piontcut" expression="execution(* com.saxon.service.UserServiceImpl.*(..) )"/>
        <!--执行环绕增强!-->
        <aop:advisor advice-ref="log" pointcut-ref="piontcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="piontcut"/>
    </aop:config>

    
</beans>

测试代码

@Test
public void test(){
    ApplicationContext context =
        new ClassPathXmlApplicationContext("applicationContext.xml");
    //动态代理代理的是接口
    UserService userService = context.getBean("userService", UserService.class);
    userService.add();
}

//测试结果
com.saxon.service.UserServiceImpl执行了: add方法,参数为: [Ljava.lang.Object;@50a638b5
增加了一个用户!
执行了: add方法,返回了: null

AOP方式二: 自定义切面

自定义类

public class DiyPointCut {
    public void before(){
        System.out.println("=====方法执行前=====");
    }
    public void after(){
        System.out.println("=====方法执行后=====");
    }
}

配置xml

<!--方式二 自定义类-->
<bean id="diy" class="com.saxon.diy.DiyPointCut"/>
<aop:config>
    <!--自定义切面,ref引用的类-->
    <aop:aspect ref="diy">
        <!--切入点-->
        <aop:pointcut id="pointcut" expression="execution(* com.saxon.service.UserServiceImpl.*(..))"/>
        <!--通知-->
        <aop:before method="before" pointcut-ref="pointcut"/>
        <aop:after method="after" pointcut-ref="pointcut"/>
    </aop:aspect>
</aop:config>

测试

@Test
public void test(){
    ApplicationContext context =
        new ClassPathXmlApplicationContext("applicationContext.xml");
    //动态代理代理的是接口
    UserService userService = context.getBean("userService", UserService.class);
    userService.add();
}

AOP方式三:注解实现

@Aspect //标记这个类为切面
@Component
public class AnnotationPointCut {
    @Before("execution(* com.saxon.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("=====方法执行前=====");
    }
    @After("execution(* com.saxon.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("=====方法执行后=====");
    }
    //在环绕增强中,我们可以给定一个参数,代表我们要获取处理切入的点
    @Around("execution(* com.saxon.service.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("=========环绕前========");
        //执行方法
        Object proceed = jp.proceed();
        System.out.println("=========环绕后========");
    }
}

配置xml

<!--方式三-->
<bean id="annotatinPointCut" class="com.saxon.diy.AnnotationPointCut" />
<!--开启注解支持 JDK(默认 proxy-target-class="false") cglib(proxy-target-class="true")-->
<aop:aspectj-autoproxy proxy-target-class="true"/>

或者

 <!--方式三: 注解-->
    <!--开启注解支持-->
    <aop:aspectj-autoproxy/>

注意

<bean id="annotatinPointCut" class="com.saxon.diy.AnnotationPointCut" />
与 @Component  相同
评论(0)
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!