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>
  • 相关阅读:
    Get Date information with a datetime var
    深入浅出Eclipse Modeling Framework (EMF)
    [转]How to Create a Thumbnail Picture Library View in SharePoint 2007
    Moss2007 view 过滤 当前用户 Custom List View (Filter by User [Me])
    moss 2007 自定义NewForm.aspx 注意事项 报错Unable to display this Web Part
    [转]Hiding the Search Scopes DropDown in WSSv3/MOSS 2007
    moss2007 新建模板页 搜索框无法使用 SmallSearchInputBox
    [转]SharePoint SearchasYouType with jQuery
    [转]Moss2007 Customize the NewForm.aspx 自定义NewForm EditForm页面
    Moss2007 xslt DataFormWebPart 展示图片库所有图片(文档库所有文档)
  • 原文地址:https://www.cnblogs.com/wt7018/p/13339532.html
Copyright © 2011-2022 走看看