Spring——AOP注解使用

Posted tractors

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring——AOP注解使用相关的知识,希望对你有一定的参考价值。

一、导入依赖:

技术图片
        <!-- SpringIOC -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <!-- SpringAOP -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>aopalliance</groupId>
            <artifactId>aopalliance</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.7</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.2</version>
        </dependency>
依赖

二、通知类:

@Component
@Aspect
public class Log 
    @Pointcut("execution( * com.qf.aop..*(..))")
    public void logPointCut()  

    @Before("logPointCut()")
    public void before() 
        System.out.println("前置通知");
    
    @AfterReturning("logPointCut()")
    public void afterReturning() 
        System.out.println("后置通知(方法不出现异常)");
    
    @AfterThrowing("logPointCut()")
    public void afterException() 
        System.out.println("异常通知");
    
    @After("logPointCut()")
    public void after() 
        System.out.println("最终通知(方法不出现异常)");
    

    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable 
        System.out.println("这是环绕通知之前的部分");
        Object proceed=null;
        try 
            proceed = point.proceed();
            System.out.println("这是环绕通知之后的部分");
        catch (Exception e)
            System.out.println(e.getMessage());
            System.out.println("这是环绕通知异常的部分");
        finally 
            System.out.println("这是最终通知");
        
        return proceed;
    

三、被增强类:

@Component
public class UserManager 
    public void addStudent(User user) 
        //System.out.println(3/0);
        System.out.println("增加用户");
    

四、application-context.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" xmlns:bean="http://www.springframework.org/schema/context"
       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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 开启spring-IOC注解扫描-->
    <bean:component-scan base-package="com.qf" />
    <!-- 开启spring-aop注解-->
    <aop:aspectj-autoproxy ></aop:aspectj-autoproxy>
</beans>

五、测试:

public class T 
    public static void main(String[] args) 
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
        UserManager userManager = context.getBean("userManager",UserManager.class);
        userManager.addStudent(new User());
    

六、注意点:

  使用注解配置:最终通知会再后置通知之前,但是环绕通知还是照常;

 

以上是关于Spring——AOP注解使用的主要内容,如果未能解决你的问题,请参考以下文章

spring中aop全注解时配置类怎么写

使用Spring的注解方式实现AOP

使用Spring的注解方式实现AOP

使用Spring的注解方式实现AOP

spring注解开发及AOP

Spring的注解方式实现AOP