zoukankan      html  css  js  c++  java
  • Spring_AOP

    使用Spring实现Aop

      <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>1.9.4</version>    
            </dependency>

    方式一:使用Spring的Api接口 【主要是SPringle Api接口实现】

    1、接口

    package com.ly.service;
    
    public interface UserService {
        public void add();
        public void del();
        public void update();
        public void select();
    }

    2、配置文件

    <?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
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd
    ">
    
        <bean id="userService" class="com.ly.service.UserServiceImpl"/>
        <bean id="log" class="com.ly.log.Log"/>
        <bean id="afterLog" class="com.ly.log.AfterLog"/>
    
        <!--方式一、使用原生的Spring Api接口-->
        <!--配置aop:需要导入aop的约束-->
        <aop:config>
            <!--切入点,expression:表达式,execution(要执行的位置)-->
            <aop:pointcut id="pointcut" expression="execution(* com.ly.service.UserServiceImpl.*(..))"/>
            <!--执行环绕增加-->
            <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
            <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
        </aop:config>
    </beans>

    3、真实对象

    package com.ly.service;
    
    public class UserServiceImpl implements UserService {
        public void add() {
            System.out.println("增加了一个用户");
        }
    
        public void del() {
            System.out.println("删除了一个用户");
        }
    
        public void update() {
            System.out.println("修改了一个用户");
        }
    
        public void select() {
            System.out.println("选择了一个用户");
        }
    
    }

    4、前置日志

    package com.ly.log;
    
    import org.springframework.aop.MethodBeforeAdvice;
    
    import java.lang.reflect.Method;
    
    public class Log  implements MethodBeforeAdvice {
        //method:要执行的目标对象的方法
        //args:参数
        //target“目标对象
        public void before(Method method, Object[] args, Object target) throws Throwable {
            System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
    
        }
    }

    5、后置日志

    package com.ly.log;
    
    import org.springframework.aop.AfterReturningAdvice;
    
    import java.lang.reflect.Method;
    
    public class AfterLog implements AfterReturningAdvice {
       //returnvalue:返回值
        public void afterReturning(Object returnvalue, Method method, Object[] args, Object target) throws Throwable {
            System.out.println("执行了"+method.getName()+"返回的结果为"+returnvalue);
        }
    }

    6、测试

    import com.ly.service.UserService;
    import com.ly.service.UserServiceImpl;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class MyTest {
        public static void main(String[] args) {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
            //**********动态代理代理的是一类接口*************
            UserService userService = (UserService) context.getBean("userService");
            userService.add();
            userService.del();
        }
    }

    方式二:使用自定义类来实现Aop 【主要是切面定义】

    1、自定义类

    package com.ly.diy;
    
    public class DiyPointCut {
        public void before(){
            System.out.println("---执行前---");
        }
        public void after(){
            System.out.println("---执行后---");
        }
    }

    2、配置文件

    <?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
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd
    ">
    
        <bean id="userService" class="com.ly.service.UserServiceImpl"/>
        <bean id="log" class="com.ly.log.Log"/>
        <bean id="afterLog" class="com.ly.log.AfterLog"/>
    
        <!--方式一、使用原生的Spring Api接口-->
        <!--配置aop:需要导入aop的约束-->
        <!--<aop:config>
            &lt;!&ndash;切入点,expression:表达式,execution(要执行的位置)&ndash;&gt;
            <aop:pointcut id="pointcut" expression="execution(* com.ly.service.UserServiceImpl.*(..))"/>
            &lt;!&ndash;执行环绕增加&ndash;&gt;
            <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
            <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
        </aop:config>-->
    
    
        <!--方式二:自定义类-->
        <bean id="diy" class="com.ly.diy.DiyPointCut"/>
            <aop:config>
                <!--自定义切面,ref要引用类-->
                <aop:aspect ref="diy">
                    <!--切入点-->
                    <aop:pointcut id="point" expression="execution(* com.ly.service.UserServiceImpl.*(..))"/>
    
                    <!--通知-->
                    <aop:before method="before" pointcut-ref="point"/>
                    <aop:after method="after" pointcut-ref="point"/>
                </aop:aspect>
            </aop:config>
    
    </beans>

    方式三:使用注解实现

    1、自定义类

    package com.ly.diy;
    
    //方式三 使用注解实现AOP
    
    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(* com.ly.service.UserServiceImpl.*(..))")
        public void before(){
            System.out.println("方法执行之前");
        }
        @After("execution(* com.ly.service.UserServiceImpl.*(..))")
        public void after(){
            System.out.println("方法执行之后");
        }
        //在环绕增强中,我们可以给定一个参数,代表我们要获取处理切入的点
        @Around("execution(* com.ly.service.UserServiceImpl.*(..))")
        public void around(ProceedingJoinPoint jp) throws Throwable {
            System.out.println("环绕前");
    
            //执行方法
            Object proceed = jp.proceed();
    
            System.out.println("环绕后");
        }
    
    }

    2、配置

    <bean id="annotationPointCut" class="com.ly.diy.AnnotationPointCut"/>
    <aop:aspectj-autoproxy/>
  • 相关阅读:
    【java基础】方法2
    【Java基础】方法
    [java基础]数组
    [Java基础]循环结构3
    [java基础]循环结构2
    [java基础]循环结构1
    [java基础]分支结构(2)
    mybatis-config.xml简单笔记
    applicationContext.xml简单笔记
    spring-servlet.xml简单示例
  • 原文地址:https://www.cnblogs.com/moxihuishou/p/15195314.html
Copyright © 2011-2022 走看看