aop:aspect

package com.albert.spring.proxy;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

public class Interceptor
{
    public void before()
    {
        System.out.println("前置通知");
    }
    
    public void afterReturning()
    {
        System.out.println("後置通知");
    }
    
    public void beforeAdd(JoinPoint jp)
    {
        System.out.println("前置通知,獲取類名:" + jp.getTarget().getClass().getName());
        System.out.println("前置通知,獲取方法名:" + jp.getSignature().getName());
        System.out.print("前置通知,獲取參數:");
        for (Object obj : jp.getArgs())
        {
            System.out.print(obj + "\t");
        }
    }
    
    public void afterReturningRes(String result)
    {
        System.out.println("後置通知,返回結果:" + result);
    }
    
    public void after()
    {
        System.out.println("最終通知");
    }
    
    public void AfterThrowing()
    {
        System.out.println("例外通知");
    }
    
    public void catchException(Exception e)
    {
        System.out.println("獲取拋出的異常:" + e);
    }
    
    public Object around(ProceedingJoinPoint pjp)
        throws Throwable
    {
        System.out.println("進入環繞");
        // if(){ // 進行一些判斷,再執行環繞Object result = pjp.proceed();
        // }
        System.out.println("退出環繞");
        return result;
    }
}


 

<?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: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-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
	<aop:aspectj-autoproxy /><!-- 開啟切面編程功能-->
	<bean id="userDaoImp" class="com.albert.spring.proxy.aop.UserDaoImp" />
	<bean id="aspectBean" class="com.albert.spring.proxy.aop.Interceptor" />
	<aop:config>
	<!-- 聲明一個切麵類-->
		<aop:aspect id="asp" ref="aspectBean">
		<!-- 聲明一個切入點-->
			<aop:pointcut id="thecut"
				expression="execution(* com.albert.spring.proxy.aop.UserDaoImp.*(..))" />
			<aop:after-returning pointcut-ref="thecut"
				method="afterReturningRes" returning="result" />
			<aop:around pointcut-ref="thecut" method="around" />
			<aop:after-throwing pointcut-ref="thecut" method="catchException"
				throwing="e" />
			<aop:after pointcut-ref="thecut" method="after" />
			<aop:before pointcut-ref="thecut" method="before" />
			<aop:before pointcut-ref="thecut" method="beforeAdd" />
		</aop:aspect>
	</aop:config>

</beans>


 

你可能感兴趣的:(spring,AOP,bean,xml,asp)