zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    Java的静态块与实例块(转)
    Programming Ability Test学习 1031. Hello World for U (20)
    Programming Ability Test学习 1011. World Cup Betting (20)
    Programming Ability Test学习 1027. Colors in Mars (20)
    Programming Ability Test学习 1064. Complete Binary Search Tree (30)
    Programming Ability Test学习 1008. Elevator (20)
    【maven详解-生命周期】Maven的生命周期和插件
    【maven详解-插件】maven插件学习之源码插件Source Xref
    $(document).ready(){}、$(fucntion(){})、(function(){})(jQuery)onload()的区别
    你还没真的努力过,就轻易输给了懒惰
  • 原文地址:https://www.cnblogs.com/wt7018/p/13339532.html
Copyright © 2011-2022 走看看