码迷,mamicode.com
首页 > 其他好文 > 详细

Aop练手

时间:2019-06-19 14:55:56      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:exe   try   ogr   before   sys   ram   row   art   lap   

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();
    }
    
}

结果是对滴 .^_^.

Aop练手

标签:exe   try   ogr   before   sys   ram   row   art   lap   

原文地址:https://www.cnblogs.com/kongieg/p/11051177.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!