package model;

 

public class DemoService {

    public String method1(String name ,String name2) {

//     int a=1/0;

       long time = System.currentTimeMillis();

       System.out.println("------------------------method1已被呼叫--------------------");

       return name + ":" + time;

    }

}



package model;

 

import org.aspectj.lang.JoinPoint;

import org.aspectj.lang.ProceedingJoinPoint;

 

public class LogAdvice {

    public void logBefore(JoinPoint point) {

       System.out.println("Beforeat "+point.getTarget().getClass());

       System.out.println("calling "+point.getSignature().getName());

       System.out.println("using "+point.getArgs()[0]+" "+point.getArgs()[1]);

       System.out.println("before 結束,真正被呼叫的方法開始:");

    }

   

    public Object logAround(ProceedingJoinPoint point) throws Throwable {

       System.out.println("Aroundat "+point.getTarget().getClass());

       System.out.println("calling "+point.getSignature().getName());

       System.out.println("using "+point.getArgs()[0]+" ");

       Object result=point.proceed();

       System.out.println("Aroundresult="+result);

       return result;

    }

    public void logAfter(JoinPoint point,Object result ) {//

       System.out.println("Afterat "+point.getTarget().getClass());

       System.out.println("Afterresult="+result);

    }

    public void logThrow(JoinPoint point, Throwable exc){

       System.out.println("exception000000"+exc);

    }

}



<?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: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/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

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

 

    <bean id="demoService" class="model.DemoService"></bean>

    <bean id="logAdvice" class='model.LogAdvice'></bean>

    <!-- 先把類別LogAdvice註冊好 -->

 

    <aop:config>

       <aop:aspect id="logAspect" ref="logAdvice">

       <aop:pointcut expression="execution(* model.DemoService.method*(..))" id="pointcut1"/>

       <aop:before method="logBefore" pointcut-ref="pointcut1"/>

       <aop:after-returning method="logAfter" pointcut-ref="pointcut1" returning="result"/><!--  -->

       <aop:around method="logAround" pointcut-ref="pointcut1"/>

       <aop:after-throwing method="logThrow" pointcut-ref="pointcut1" throwing="exc"/>

       </aop:aspect>

    </aop:config>

 

 

</beans>



package Test;

 

import org.springframework.context.ApplicationContext;

import org.springframework.context.ConfigurableApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

import model.DemoService;

 

public class Test {

    public static void main(String[] args) {

      

       ApplicationContext context =

              new ClassPathXmlApplicationContext("beans.config.xml");

       DemoService demoService = (DemoService) context.getBean("demoService");

       String message = demoService.method1("參數一","參數二");

       System.out.println(message);

 

       ((ConfigurableApplicationContext) context).close();

    }

}

 

創作者介紹

學習筆記專區

乙方 發表在 痞客邦 PIXNET 留言(0) 人氣()