zoukankan      html  css  js  c++  java
  • Spring AOP:@Before、@After 的 JavaConfig 写法

    网络上关于Spring AOP的范例大都是使用xml作配置文件,见此特地写一些JavaConfig的范例,既为加深理解,亦为加强记忆。如需引用或转载的同学,请注明来源。

    使用Spring AOP,要成功运行起代码,只用Spring提供给开发者的jar包是不够的,请额外上网下载两个jar包导入项目中:

    • aopalliance.jar
    • aspectjweaver.jar。

    由于我使用IDEA_U创建的spring项目,aopalliance.jar是Maven自动下载的,而 aspectjweaver.jar 则需要另外下载aspectj.jar,然后用解压软件打开,解压出 aspectjweaver.jar

    先写一个接口:

    @Component
    public interface Person {
        void say();
        void run();
    }

    再写两个实现类:

    @Component
    @Qualifier("adults")
    public class Adults implements Person {
    
        private String classname = "adults";
    
        @Override
        public void say() {
            System.out.println("I am " + classname + " , I like acid rock.");
        }
    
        @Override
        public void run() {
            System.out.println("I am " + classname + " , I like long-distance");
        }
    }
    @Component
    @Qualifier("children")
    public class Children implements Person {
    
        private String classname = "children";
    
        @Override
        public void say() {
            System.out.println("We are " + classname + " , we like nursery rhymes.");
        }
    
        @Override
        public void run() {
            System.out.println("We are " + classname + " , we like running around.");
        }
    }

    然后写一个切面,这也是一个类:

    @Component
    @Aspect //声明这是一个切面。必须的!
    public class AspectConfig {
    
        //通知和切入点的混合写法;
        //第一个 * 号表示任意返回类型,第二个 * 号表示Person的所有方法
        @Before("execution(* com.san.spring.aop.Person.*(..))")
        public void showTime1(){
            System.out.println("CurrentTime = " + System.currentTimeMillis());
        }
    
        @After("execution(* com.san.spring.aop.Person.*(..))")
        public void showTime2(){
            System.out.println("CurrentTime = " + System.currentTimeMillis());
        }
    }

    上面的切面也可以这样定义

    @Component
    @Aspect //声明这是一个切面。必须的! 
    public class AspectConfig {
    
        // 定义一个切点
        @Pointcut("execution(* com.san.spring.aop.Person.*(..))")
        public void pointcut(){}
    
        // 定义通知
        @Before("pointcut()")
        public void showTime1(){
            System.out.println("CurrentTime = " + System.currentTimeMillis());
        }
    
        // 定义通知
        @After("pointcut()")
        public void showTime2(){
            System.out.println("CurrentTime = " + System.currentTimeMillis());
        }
    }

    定义spring的JavaConfig类:

    @Configuration
    @ComponentScan
    @EnableAspectJAutoProxy //启用自动代理功能。必须的!
    public class SpringConfig {
    }

    写一个测试类:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = SpringConfig.class)
    public class AopTest {
    
        @Autowired
        @Qualifier("children")
        private Person person;
    
        @Test
        public void testMethod(){
            person.say();
            person.run();
        }
    }

    本文参考了http://www.cnblogs.com/xrq730/p/4919025.html

  • 相关阅读:
    linux(fedora) 下dvwa 建筑环境
    【ThinkingInC++】2、输入和输出流
    Caused by: java.lang.ClassNotFoundException: javax.transaction.TransactionManager
    SpringMVC注释启用
    XML wsdl soap xslt xsl ide
    一个解析RTSP 的URL函数
    PHP:header()函数
    jquery实现鼠标焦点十字效果
    拼出漂亮的表格
    Oracle中如何插入特殊字符:& 和 ' (多种解决方案)
  • 原文地址:https://www.cnblogs.com/xxdsan/p/6399711.html
Copyright © 2011-2022 走看看