<?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">

 

 

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

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

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

 

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

<!-- 告訴Spring去找AspectJannotation -->

 

 

</beans>



package model;

 

import org.aspectj.lang.JoinPoint;

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.After;

import org.aspectj.lang.annotation.AfterReturning;

import org.aspectj.lang.annotation.AfterThrowing;

import org.aspectj.lang.annotation.Around;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.aspectj.lang.annotation.Pointcut;

@Aspect //用以做AOP宣告

public class LogAdvice {

   

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

    @Pointcut(value="execution(* model.DemoService.method*(..))")

    public void pointcut(){}//呼叫這個方法,啥都不做只是註冊記號而已,主要是value指定何時啟動這個pointcut

   

    @Before(value="pointcut()")

    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 結束,真正被呼叫的方法開始:");

    }

    @Around(value="pointcut()")

    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;

    }

    @AfterReturning(value="pointcut()",returning="result")//returning="result"為必填

    public void logAfter(JoinPoint point,Object result ) {

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

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

    }

   

    @AfterThrowing(value="pointcut()" ,throwing="exc")//throwing="exc"為必填

    public void logThrow(JoinPoint point, Throwable exc){

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

    }

}



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

    }

}



六月 13, 2016 8:19:01 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh

資訊: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@193b845: startup date [Mon Jun 13 20:19:01 CST 2016]; root of context hierarchy

六月 13, 2016 8:19:01 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

資訊: Loading XML bean definitions from class path resource [beans.config.xml]

Aroundat class model.DemoService

calling method1

using 參數一

Beforeat class model.DemoService

calling method1

using 參數一 參數二

before 結束,真正被呼叫的方法開始:

------------------------method1已被呼叫--------------------

Aroundresult=參數一:1465820342022

Afterat class model.DemoService

Afterresult=參數一:1465820342022

參數一:1465820342022

六月 13, 2016 8:19:02 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose

資訊: Closing org.springframework.context.support.ClassPathXmlApplicationContext@193b845: startup date [Mon Jun 13 20:19:01 CST 2016]; root of context hierarchy

 

創作者介紹

學習筆記專區

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