zoukankan      html  css  js  c++  java
  • Spring笔记:AOP面向切面编程 山上下了雪

    AOP(Aspect Oriented Programming)面向切面编程,是通过预编译和运行期间动态代理的方式实现程序功能的一种技术。Spring中常用的AOP实现方式有三种:Spring原生API、自定义类和注解。我先把不同实现方式的示例中共用的代码贴出来。

    切入点,即要测试的接口和类:

    package com.yun.service;
    
    public interface UserService {
        public void add();
        public void delete();
        public void update();
        public void query();
    }
    
    package com.yun.service;
    
    public class UserServiceImpl implements UserService{
        public void add() {
            System.out.println("Add a user.");
        }
    
        public void delete() {
            System.out.println("Delete a user.");
        }
    
        public void update() {
            System.out.println("Update a user.");
        }
    
        public void query() {
            System.out.println("Query a user.");
        }
    }
    

    测试类:

    import com.yun.service.UserService;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class MyTest {
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            // 注意,由于Spring AOP的实现原理是动态代理,所以这里的对象类型必须指定为对应的接口,而不是具体的实现类
            UserService userService = (UserService)context.getBean("userService");
            userService.add();
        }
    }
    

    1. AOP实现方式一:Spring原生API

    使用Spring原生API的方式就需要实现特定的接口,具体接口见示例。

    在需要切入的方法之前执行的操作:

    package com.yun.log;
    
    import org.springframework.aop.MethodBeforeAdvice;
    
    import java.lang.reflect.Method;
    
    public class BeforeLog implements MethodBeforeAdvice {
        // method: 目标对象中要执行的方法
        // args: 执行方法对应的参数
        // target: 要执行的目标对象
        // 此方法表示在目标对象target的method方法之前执行
        public void before(Method method, Object[] args, Object target) throws Throwable {
            System.out.println("before:" + target.getClass().getName() + "." + method.getName());
        }
    }
    

    在需要切入的方法之后执行的操作:

    package com.yun.log;
    
    import org.springframework.aop.AfterReturningAdvice;
    
    import java.lang.reflect.Method;
    
    public class AfterReturnLog implements AfterReturningAdvice {
        // 此方法表示在目标对象target的method方法之后执行,这个方法可以拿到method方法的返回值
        public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
            System.out.println(target.getClass().getName() + "." + method.getName() + "的返回结果为:" + returnValue);
        }
    }
    

    xml配置:为了使用Spring AOP,需要添加额外的扩展:

    • xmlns:aop="http://www.springframework.org/schema/aop"
    • http://www.springframework.org/schema/aop
    • https://www.springframework.org/schema/aop/spring-aop.xsd
    <?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
            https://www.springframework.org/schema/aop/spring-aop.xsd">
    
        <!-- 注册bean -->
        <bean id="userService" class="com.yun.service.UserServiceImpl"/>
        <bean id="beforeLog" class="com.yun.log.BeforeLog"/>
        <bean id="afterReturnLog" class="com.yun.log.AfterReturnLog"/>
    
        <!-- aop实现方式一:使用原生Spring API接口 -->
        <aop:config>
            <!-- pointcut:表示切入点,即要切入的方法,expression属性值是一个execution表达式,该表达式其实就是一个方法定义格式:修饰符 返回值类型 方法签名,
             如果不想指定修饰符和返回值,可以直接写星号*,UserServiceImpl.*表示UserServiceImpl中的所有方法,(..)表示方法的任意参数 -->
            <aop:pointcut id="pointcut" expression="execution(* com.yun.service.UserServiceImpl.*(..))"/>
            <!-- advisor:执行环绕,即在要切入的方法执行前后或其他位置进行切入执行,advice-ref表示“环绕”的类对象(需要实现对应的方法) -->
            <aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
            <aop:advisor advice-ref="afterReturnLog" pointcut-ref="pointcut"/>
        </aop:config>
    </beans>
    

    输出:

    before:com.yun.service.UserServiceImpl.add
    Add a user.
    com.yun.service.UserServiceImpl.add的返回结果为:null
    

    2. AOP实现方式二:自定义类实现AOP

    自定义类的方式可以指定自定义的类中哪些方法在“前”执行,哪些方法在“后”执行。

    自定义类:

    package com.yun.custom;
    
    public class MyPointCut {
        public void before(){
            System.out.println("--------before-------");
        }
    
        public void after(){
            System.out.println("--------after-------");
        }
    }
    

    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
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop
            https://www.springframework.org/schema/aop/spring-aop.xsd">
    
        <!-- 注册bean -->
        <bean id="userService" class="com.yun.service.UserServiceImpl"/>
        <bean id="myPointCut" class="com.yun.custom.MyPointCut"/>
        
        <!-- aop实现方式二:使用自定义类 -->
        <aop:config>
            <!-- 配置切面,即自定义的类 -->
            <aop:aspect ref="myPointCut">
                <!-- 配置切入点,即被“包裹”的方法,也是使用execution表达式 -->
                <aop:pointcut id="pointcut" expression="execution(* com.yun.service.UserServiceImpl.*(..))"/>
                <!-- 配置在切入点之前执行的方法 -->
                <aop:before method="before" pointcut-ref="pointcut"/>
                <!-- 配置在切入点之后执行的方法 -->
                <aop:after method="after" pointcut-ref="pointcut"/>
            </aop:aspect>
        </aop:config>
    </beans>
    

    输出:

    --------before-------
    Add a user.
    --------after-------
    

    3. AOP实现方式三:注解

    注解的方式更加直观明了,而且同样会使用execution表达式,但是请注意,需要在xml中声明aop自动代理支持。

    注解使用:

    package com.yun.annotation;
    
    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    
    // 指定切面
    @Aspect
    public class AnnotationPointcut {
        // 在切入点之前执行,参数为execution表达式
        @Before("execution(* com.yun.service.UserServiceImpl.*(..))")
        public void before(){
            System.out.println("--------before-------");
        }
    
        // 在切入点之后执行,参数为execution表达式
        @After("execution(* com.yun.service.UserServiceImpl.*(..))")
        public void after(){
            System.out.println("--------after-------");
        }
    }
    

    xml配置:特别注意,需要声明使用aop自动代理。

    <?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
            https://www.springframework.org/schema/aop/spring-aop.xsd">
    
        <!-- 注册bean -->
        <bean id="userService" class="com.yun.service.UserServiceImpl"/>
        <bean id="annotationPointcut" class="com.yun.annotation.AnnotationPointcut"/>
        <!-- 声明使用aop自动代理 -->
        <aop:aspectj-autoproxy/>
    </beans>
    

    输出:

    --------before-------
    Add a user.
    --------after-------
    
  • 相关阅读:
    安装Kudu
    flume+kafka+spark streaming整合
    安装Kafka
    DataFrame格式化
    RDD/Dataset/DataFrame互转
    多个jar包合并成一个jar包的办法
    flume使用示例
    ecplise + hadoop 调试环境搭建
    web.xml文件加载顺序
    Web.xml配置参数详解
  • 原文地址:https://www.cnblogs.com/guyuyun/p/15640663.html
Copyright © 2011-2022 走看看