spring 实现AOP

Posted 市丸银

tags:

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

三种方式

第二个比较好

导入包

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

使用API接口实现AOP

过程

1、接口类

2、继承接口类

3、添加日志

package com.wt.log;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class BeforeLog  implements MethodBeforeAdvice {

    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(method.getName());
    }
}

package com.wt.log;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class AfterLog implements AfterReturningAdvice {
    public void afterReturning(Object returnValue, Method method, Object[] args, Object object) 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
        https://www.springframework.org/schema/aop/spring-aop.xsd">
    <bean id="userService" class="com.wt.service.UserServiceImpl"></bean>
    <bean id="afterLog" class="com.wt.log.AfterLog"></bean>
    <bean id="beforeLog" class="com.wt.log.BeforeLog"></bean>

    <aop:config>
<!--        切入点 -->
       <aop:pointcut id="pointcut" expression="execution(* com.wt.service.UserServiceImpl.*(..))"/>
<!--        执行环绕增强-->
        <aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
    </aop:config>
</beans>

测试

import com.wt.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void userTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = context.getBean("userService", UserService.class);
        userService.add();
    }
}

 自定义实现AOP

自定义一个类用于打印日志

package com.wt.diy;

public class UserLog {
    void before(){
        System.out.println("===========before===========");
    }

    void after(){
        System.out.println("===========after============");
    }
}
<?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
        https://www.springframework.org/schema/aop/spring-aop.xsd">
    <bean id="userService" class="com.wt.service.UserServiceImpl"></bean>
    <bean id="userLog" class="com.wt.diy.UserLog"/>

    <aop:config>
        <!--切面-->
        <aop:aspect ref="userLog">
            <!--切点-->
            <!--execution 执行方法 (..) 不限定参数-->
            <aop:pointcut id="point" expression="execution(* com.wt.service.UserServiceImpl.*(..))"/>
            <aop:before method="before" pointcut-ref="point"/>
            <aop:after method="after" pointcut-ref="point"/>
        </aop:aspect>
    </aop:config>

</beans>

使用注解实现AOP

package com.wt.diy;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

// 切面
@Aspect
public class TestLog {
    
    @Before("execution(* com.wt.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("=====方法执行前=====");
    }
    
    @After("execution(* com.wt.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("=====方法执行后======");
    }
}

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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">
    <bean id="userService" class="com.wt.service.UserServiceImpl"/>
    <bean id="testLog" class="com.wt.diy.TestLog"/>

    <aop:aspectj-autoproxy />

</beans>

 

以上是关于spring 实现AOP的主要内容,如果未能解决你的问题,请参考以下文章

spring aop配置及实现

Spring AOP 应用:三种配置及实现方式

使用Spring配置文件实现AOP

JAVA之AOP

spring-AOP原理

Spring_AOP的实现机制-动态代理