zoukankan      html  css  js  c++  java
  • Spring aop练手

    applicationContext.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns="http://www.springframework.org/schema/beans" 
           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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">
    <context:component-scan base-package="com.kong.bean"></context:component-scan>
    <bean name="myPerform" class="com.kong.service.MyPerform"></bean>
    <bean name="myAdvice" class="com.kong.service.MyAdvice"></bean>
    <aop:config>
        <aop:aspect  ref="myAdvice">
        <aop:pointcut expression="execution(* com.kong.service.MyPerform.persist(..))" id="peisistCut"/>
            <aop:before pointcut-ref="peisistCut" method="doBefore"/>
            <aop:after pointcut-ref="peisistCut" method="doAfterRegardlessIsOk"/>
            <aop:after-returning pointcut-ref="peisistCut" method="doAfter"/>
            <aop:after-throwing pointcut-ref="peisistCut" method="doAfterThrowing"/>
            <aop:around pointcut-ref="peisistCut" method="doSurround"/>
        </aop:aspect>
    </aop:config>
    </beans>

    切点类:MyPerform.java

    package com.kong.service;
    
    public class MyPerform {
        public void persist() {
            System.out.println("do persist() method!!!");
        }
        
        public void delete() {
            System.out.println("do delete() method!!!");
        }
        
        public void modify() {
            System.out.println("do modify() method!!!");
        }
        
        public void find() {
            System.out.println("do find() method!!!");
        }
    
    }

    切面:

    package com.kong.service;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    
    public class MyAdvice {
        public void doBefore() {
            System.out.println("myAdvice_method:doBefore()!!");
        }
        public void doAfter() {
            System.out.println("myAdvice_method:doAfter()!!");
        }
        public void doAfterRegardlessIsOk() {
            System.out.println("myAdvice_method:doAfterRegardlessIsOk()!!");
        }
        public void doAfterThrowing() {
            System.out.println("myAdvice_method:doAfterThrowing;if program comes exception,this message will show!!!");
        }
        
        public void doSurround(ProceedingJoinPoint pj) throws InterruptedException {
            try {
                System.out.println("method doSurround() begin,hehehhe!");
                System.out.println("begin drive time niddle");
                Long start = System.currentTimeMillis();
                Thread.sleep(2000);
                pj.proceed();
                Long end = System.currentTimeMillis();
                System.out.println("method doSurround() end,use time "+(end-start)+"mills");
            } catch (Throwable e) {
                // TODO Auto-generated catch block
                System.out.println("Exception ocurred");
                e.printStackTrace();
            }
            
        }
    
    }

    测试:

    package com.kong.test;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;import com.kong.service.MyPerform;
    public class MyTest {
    
        @Test
        public  void test_01() {
        
             ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
             MyPerform myPerform = (MyPerform) ac.getBean("myPerform");
             myPerform.persist();
        }
        
    }

    结果是对滴 .^_^.

  • 相关阅读:
    和时间做朋友:你一定要学的高效时间管理术
    助推:如何做出有关健康、财富与幸福的最佳决策(2017年诺贝尔经济学奖获得者理查德·泰勒作品)
    看透 : 解密身体语言隐藏的密码
    成为独角兽:海盗、梦想家、创新者如何开创并主宰新品类
    极简法则:从苹果到优步的深层简化工具
    高效15法则:谷歌、苹果都在用的深度工作发
    成功与运气:好运与精英社会的神话
    2星|《成长企业的法则》:尝试总结成功企业的模式,但是洞察力不够。
    3星|《OKR:源于英特热和谷歌的目标管理利器》:OKR原理、实施手册、实施过的公司的访谈
    gedit如何使用代码片段
  • 原文地址:https://www.cnblogs.com/kongieg/p/11051177.html
Copyright © 2011-2022 走看看