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

    结果是对滴 .^_^.

  • 相关阅读:
    KMP总结
    条件概率的计算
    EL和JSTL
    tomcat部署web项目
    web
    面试1
    面试可能会问的题
    简介
    建一个springboot项目
    刷题之牛客网
  • 原文地址:https://www.cnblogs.com/kongieg/p/11051177.html
Copyright © 2011-2022 走看看