Spring基于Schema配置切面的例子

       如果项目不能使用JDK1.5以上,无法使用@AspectJ进行注解,Spring提供了基于Schema配置的方法,代替基于@AspectJ注解要面的方式。

1.XML配置

使用<aop:advisor>或<aop:aspect>进行配置

<?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-3.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
	<aop:config proxy-target-class="true">
	<!--  
	    <aop:advisor advice-ref="testAdvice"  pointcut="execution(* com..*.Waiter.greetTo(..))"/>  
	    -->
		<aop:aspect ref="adviceMethods">
			<aop:before method="preGreeting"
				pointcut="target(com.baobaotao.NaiveWaiter) and args(name)"
				arg-names="name" />
			<aop:after-returning method="afterReturning"
				pointcut="target(com.baobaotao.SmartSeller)" returning="retVal" />
			<aop:around method="aroundMethod"
				pointcut="execution(* serveTo(..)) and within(com.baobaotao.Waiter)" />
			<aop:after-throwing method="afterThrowingMethod"
				pointcut="target(com.baobaotao.SmartSeller) and execution(* checkBill(..))"
				throwing="iae" />
						<!--
			<aop:after method="afterMethod"
				pointcut="execution(* com..*.Waiter.greetTo(..))" />
		
			<aop:declare-parents
				implement-interface="com.baobaotao.Seller"
				default-impl="com.baobaotao.SmartSeller"
				types-matching="com.baobaotao.Waiter+" />
				-->
            <aop:before method="bindParams" 
                   pointcut="target(com.baobaotao.NaiveWaiter) and args(name,num,..)"/>
		</aop:aspect>
	</aop:config>
    <bean id="testAdvice" class="com.baobaotao.schema.TestBeforeAdvice"/>
	<bean id="adviceMethods" class="com.baobaotao.schema.AdviceMethods" />
	<bean id="naiveWaiter" class="com.baobaotao.NaiveWaiter" />
	<bean id="naughtyWaiter" class="com.baobaotao.NaughtyWaiter" />
	<bean id="seller" class="com.baobaotao.SmartSeller" />
</beans>

2.相关的类和接口

package com.baobaotao.schema;

import org.aspectj.lang.ProceedingJoinPoint;


public class AdviceMethods {
	public void preGreeting(String name) {
		System.out.println("--how are you!--");
		System.out.println(name);
	}

    //后置增强对应方法
	public void afterReturning(int retVal){
	   System.out.println("----afterReturning()----");
	   System.out.println("returnValue:"+retVal);
	   System.out.println("----afterReturning()----");
	}

    //环绕增强对应方法
	public void aroundMethod(ProceedingJoinPoint pjp){
	   System.out.println("----aroundMethod()----");
	   System.out.println("args[0]:"+pjp.getArgs()[0]);
	   System.out.println("----aroundMethod()----");
	}

    //抛出异常增强
	public void afterThrowingMethod(IllegalArgumentException iae){
	   System.out.println("----afterThrowingMethod()----");
	   System.out.println("exception msg:"+iae.getMessage());
	   System.out.println("----afterThrowingMethod()----");
	}

    //final增强
	public void afterMethod(){
	   System.out.println("----afterMethod()----");
	}
	

	
      //------------绑定连接点参数----------//
	public void bindParams(int num,String name){
	   System.out.println("----bindParams()----");
	   System.out.println("name:"+name);
	   System.out.println("num:"+num);
	   System.out.println("----bindParams()----");
	}
}


public class TestBeforeAdvice implements MethodBeforeAdvice {

	public void before(Method method, Object[] args, Object target)
			throws Throwable {
        System.out.println("------TestBeforeAdvice------");
        System.out.println("clientName:"+args[0]);
        System.out.println("------TestBeforeAdvice------");
	}
}

public interface Waiter {
	@NeedTest
	public void greetTo(String clientName);	
	public void serveTo(String clientName);
}


@Monitorable
public class NaiveWaiter implements Waiter {
	@NeedTest
	public void greetTo(String clientName) {
		System.out.println("NaiveWaiter:greet to "+clientName+"...");
	}	
	@NeedTest
	public void serveTo(String clientName){
		System.out.println("NaiveWaiter:serving "+clientName+"...");
	}
	public void smile(String clientName,int times){
		System.out.println("NaiveWaiter:smile to  "+clientName+ times+"times...");
	}	
}


public class NaughtyWaiter implements Waiter {
	public void greetTo(String clientName) {
		System.out.println("NaughtyWaiter:greet to "+clientName+"...");
	}	
	public void serveTo(String clientName){
		System.out.println("NaughtyWaiter:serving "+clientName+"...");
	}
	public void joke(String clientName,int times){
        	System.out.println("NaughtyWaiter:play "+times+" jokes to "+clientName+"...");
	}
}


@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Monitorable {

}

3.测试

package com.baobaotao.schema;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.baobaotao.NaiveWaiter;
import com.baobaotao.Seller;
import com.baobaotao.SmartSeller;
import com.baobaotao.Waiter;

public class SchemaAspectTest {
	public static void main(String[] args) {
		String configPath = "com/baobaotao/schema/beans.xml";
		ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath);
		Waiter naiveWaiter = (Waiter) ctx.getBean("naiveWaiter");
		Waiter naughtyWaiter = (Waiter) ctx.getBean("naughtyWaiter");	
		Seller seller = (Seller) ctx.getBean("seller");
		naiveWaiter.greetTo("John");
		naughtyWaiter.greetTo("Tom");
		
		//后置增强
		((SmartSeller)seller).sell("Beer","John");
		
		//环境增强
//		naiveWaiter.serveTo("John");
		
		//抛出异常增强
//		((SmartSeller)seller).checkBill(1);
		
		//final增强
//		naiveWaiter.greetTo("John");
		
		//引入增强
//		((Seller)naiveWaiter).sell("Beer","John");
//		((NaiveWaiter)naiveWaiter).smile("John", 2);
		
		//advisor
		//naiveWaiter.greetTo("John");
		
	}
}

猜你喜欢

转载自szlxh002.iteye.com/blog/2264694