spring-aop 三种实现切面的方式

1、概念

  • AOP (Aspect Oriented Programming)意为∶面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。
  • AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。
  • 利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

2、实现方式

使用之前需要导入aspectj织入包

 <dependency>
     <groupId>org.aspectj</groupId>
     <artifactId>aspectjweaver</artifactId>
     <version>1.9.7.M3</version>
 </dependency>

2.1 使用spring提供的接口

前置通知:MethodBeforeAdvice
后置通知:AfterReturningAdvice

  • 搭建环境

业务接口

package cn.burcher.service;

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

业务实现类

package cn.burcher.service;

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("查询了一个用户");
    }
}

前置日志类

package cn.burcher.log;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class LogBefore implements MethodBeforeAdvice {
    
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getSimpleName()+"的"+method.getName()+"方法执行了");
    }
}

后置日志类

package cn.burcher.log;

import org.springframework.aop.AfterAdvice;
import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class LogAfter implements AfterReturningAdvice {

    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getSimpleName()+"的"+method.getName()+"方法执行了");
    }
}

  • 在beans.xml注册(注意:使用aop需要导入配置)
<bean id="logBefore" class="cn.burcher.log.LogBefore"/>
<bean id="logAfter" class="cn.burcher.log.LogAfter"/>

<bean id="userService" class="cn.burcher.service.UserServiceImpl"/>

<!--aop配置类-->
<aop:config>
	<!--定义一个切入点-->
    <aop:pointcut id="point" expression="execution(* cn.burcher.service.UserServiceImpl.*(..))"/>
    <!--对切入点 执行 -->
    <aop:advisor advice-ref="logAfter" pointcut-ref="point" />
    <aop:advisor advice-ref="logBefore" pointcut-ref="point" />
</aop:config>
  • 测试
public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        UserService userService = context.getBean("userService", UserService.class);
        userService.add();
    }
}
结果:
UserServiceImpl的add方法执行了
添加了一个用户
UserServiceImpl的add方法执行了

2.3 使用自定义切面类

编写自定义切面类

package cn.burcher.pointcut;

public class Log {
    public void before(){
        System.out.println("方法执行前");
    }

    public void after(){
        System.out.println("方法执行后");
    }
}

在beans.xml中配置

<bean id="log" class="cn.burcher.pointcut.Log"/>
<aop:config>
    <aop:pointcut id="point" expression="execution(* cn.burcher.service.UserServiceImpl.*(..))"/>
    <aop:aspect ref="log">
        <aop:after method="after" pointcut-ref="point"/>
        <aop:before method="before" pointcut-ref="point"/>
    </aop:aspect>
</aop:config>

测试类不变,再次执行

结果:
方法执行前
添加了一个用户
方法执行后
  • 如果使用接口和自定义切面一起使用会怎么样?
UserServiceImpl的add方法执行了
方法执行前
添加了一个用户
方法执行后
UserServiceImpl的add方法执行了

可以看出来,spring接口实现的aop优先级是比较高的

3、使用注解实现切面

@Aspect 相当于<aop:config>
@Before相当于<aop:before>

package cn.burcher.pointcut;

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(* cn.burcher.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("========方法执行前=========");
    }


    @After("execution(* cn.burcher.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("========方法执行后=========");
    }

    @Around("execution(* cn.burcher.service.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("---------环绕前---------");
        pjp.proceed();
        System.out.println("---------环绕后---------");
    }
}

我们需要到beans.xml中开启切面注解支持

<bean class="cn.burcher.pointcut.AnnotationPointcut"/>

<!--开启支持-->
<aop:aspectj-autoproxy/>

也可以用注解的方式@Component

测试类不改,再次执行


---------环绕前---------
========方法执行前=========
添加了一个用户
========方法执行后=========
---------环绕后---------

我们再试试,三种方式同时用:

UserServiceImpl的add方法执行了
方法执行前
---------环绕前---------
========方法执行前=========
添加了一个用户
========方法执行后=========
---------环绕后---------
方法执行后
UserServiceImpl的add方法执行了

可以看到注解的方式是优先级最低的

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值