zoukankan      html  css  js  c++  java
  • Spring入门第十八课

    Spring AOP

    AspectJ:Java社区里最完整最流行的AOP框架

    在Spring2.0以上的版本中,可以使用基于AspectJ注解或者基于XML配置的AOP

    看代码:

    package logan.study.aop.impl;
    
    public interface ArithmeticCalculator {
        
        int add(int i, int j);
        int sub(int i, int j);
        int mul(int i, int j);
        int div(int i, int j);
        
    
    }
    package logan.study.aop.impl;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
    
        @Override
        public int add(int i, int j) {
            // TODO Auto-generated method stub
            int result = i + j;
            return result;
        }
    
        @Override
        public int sub(int i, int j) {
            // TODO Auto-generated method stub
            int result = i - j;
            return result;
        }
    
        @Override
        public int mul(int i, int j) {
            // TODO Auto-generated method stub
            int result = i * j;
            return result;
        }
    
        @Override
        public int div(int i, int j) {
            // TODO Auto-generated method stub
            int result = i / j;
            return result;
        }
    
    }
    package logan.study.aop.impl;
    
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.springframework.stereotype.Component;
    
    //把这个类声明为一个切面:需要把该类放入到IOC容器中,在声明为一个切面
    @Aspect
    @Component
    public class LoggingAspect {
        //声明该方法时一个前置通知:在目标方法开始之前执行
        @Before("execution(public int logan.study.aop.impl.ArithmeticCalculator.add(int, int))")
        public void beforeMethod(){
            System.out.println("The method begins");
        }
    
    }
    <?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:context="http://www.springframework.org/schema/context"
        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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
    
        <context:component-scan base-package="logan.study.aop.impl"></context:component-scan>
        <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
        
    
    </beans>
    package logan.study.aop.impl;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Main {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            //1.创建Spring的IOC容器
            ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
            //2.从IOC容器里面获取bean实例
            ArithmeticCalculator arithmeticCalculator = ctx.getBean(ArithmeticCalculator.class);
            //使用Bean
            int result = arithmeticCalculator.add(3, 6);
            System.out.println(result);
    
        }
    
    }

    输出结果:

    五月 27, 2017 4:41:38 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@49c2faae: startup date [Sat May 27 16:41:38 CST 2017]; root of context hierarchy
    五月 27, 2017 4:41:39 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from class path resource [applicationContext.xml]
    The method begins
    9

    在看代码:

    package logan.study.aop.impl;
    
    import java.util.Arrays;
    import java.util.List;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.springframework.stereotype.Component;
    
    //把这个类声明为一个切面:需要把该类放入到IOC容器中,在声明为一个切面
    @Aspect
    @Component
    public class LoggingAspect {
        //声明该方法时一个前置通知:在目标方法开始之前执行
        @Before("execution(public int logan.study.aop.impl.ArithmeticCalculator.add(int, int))")
        public void beforeMethod(JoinPoint joinPoint){
            String methodName = joinPoint.getSignature().getName();
            List<Object> args = Arrays.asList(joinPoint.getArgs());
            System.out.println("The method "+ methodName +" begins with "+args);
        }
    
    }

    输出结果:

    五月 27, 2017 5:10:24 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@49c2faae: startup date [Sat May 27 17:10:24 CST 2017]; root of context hierarchy
    五月 27, 2017 5:10:24 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from class path resource [applicationContext.xml]
    The method add begins with [3, 6]
    9

    在看代码:

    package logan.study.aop.impl;
    
    import java.util.Arrays;
    import java.util.List;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.springframework.stereotype.Component;
    
    //把这个类声明为一个切面:需要把该类放入到IOC容器中,在声明为一个切面
    @Aspect
    @Component
    public class LoggingAspect {
        //声明该方法时一个前置通知:在目标方法开始之前执行
        @Before("execution(public int logan.study.aop.impl.ArithmeticCalculator.*(int, int))")
        public void beforeMethod(JoinPoint joinPoint){
            String methodName = joinPoint.getSignature().getName();
            List<Object> args = Arrays.asList(joinPoint.getArgs());
            System.out.println("The method "+ methodName +" begins with "+args);
        }
    
    }
    package logan.study.aop.impl;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Main {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            //1.创建Spring的IOC容器
            ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
            //2.从IOC容器里面获取bean实例
            ArithmeticCalculator arithmeticCalculator = ctx.getBean(ArithmeticCalculator.class);
            //使用Bean
            int result = arithmeticCalculator.add(3, 6);
            System.out.println(result);
            
            result = arithmeticCalculator.div(3, 6);
            System.out.println(result);
    
        }
    
    }

    输出结果:

    五月 27, 2017 5:12:36 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@49c2faae: startup date [Sat May 27 17:12:36 CST 2017]; root of context hierarchy
    五月 27, 2017 5:12:36 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from class path resource [applicationContext.xml]
    The method add begins with [3, 6]
    9
    The method div begins with [3, 6]
    0

    1.SpringAOP

    加入jar包:

    spring-aop-4.3.8.RELEASE.jar
    spring-aspects-4.3.8.RELEASE.jar
    spring-beans-4.3.8.RELEASE.jar
    spring-context-4.3.8.RELEASE.jar
    spring-core-4.3.8.RELEASE.jar
    spring-expression-4.3.8.RELEASE.jar
    commons-logging-1.2.jar
    aspectjrt.jar
    aspectjweaver.jar
    aopalliance-1.0.jar
    aspectj-1.8.10.jar

    在配置文件中加入aop的命名空间

    基于注解的方式:

    在配置文件中加入如下配置:

    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

    把横切关注点的代码抽象到切面的类中

    切面首先是一个IOC中的Bean,即加入@Component注解

    切面还需要加入@Aspect注解

    在类中声明各种通知。

    AspectJ支持5中类型的通知注解:

    -@Before:前置通知,在方法执行之前执行

    -@After:后置通知,在方法执行之后通知

    -@AfterRunning:返回通知,在方法返回结果之后执行

    -@AfterThrowing:异常通知:在方法抛出异常之后

    -@Around:环绕通知,围绕着方法执行。

    声明一个方法

    在方法前加入@Before注解

    利用方法签名编写AspectJ切入点表达式

    最典型的切入点表达式时根据方法的签名来匹配各种方法:

    -execution * logan.aop.ArithmeticCalculator.*(...)匹配ArithmeticCalculator中声明的所有方法,第一个*代表任意修饰符以及任意返回值,第二个*代表任意方法,(...)匹配任意数量的参数,若目标类与接口与切面在同一个包中,可以省略包名。

    -execution public * ArithmeticCalculator.*(...):匹配ArithmeticCalculator接口的所有公有方法。

    -execution public double ArithmeticCalculator.*(...):匹配ArithmeticCalculator中返回double类型数字的方法。

    -execution public double ArithmeticCalculator.*(double,...):匹配ArithmeticCalculator中第一个参数为double类型的,返回double类型数字的方法。

    -execution public double ArithmeticCalculator.*(double, double):匹配ArithmeticCalculator中参数类型为(double,double)类型的方法。

  • 相关阅读:
    Java中一对多映射关系
    Java中多对多映射关系
    java中的匿名内部类总结
    (自己转)比较ArrayList、LinkedList、Vector
    Statement 接口的应用(存在sql语句的注入风险)
    创建Jutil (单元测试)
    自己(转)抽象类和接口联系与区别
    SQL查询语句
    数据库基础查询语句中的几个细节
    数据库查询语句
  • 原文地址:https://www.cnblogs.com/LoganChen/p/6913890.html
Copyright © 2011-2022 走看看