spring简单前置通知、后置处理。

package com.ghr;

public class VegetalbeService 
{
	public  void food() 
	{
		System.out.print("切菜-->炒菜");
	}

}



package com.ghr;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class ServiceDecorator implements MethodInterceptor
{
	public Object invoke(MethodInvocation invocation)throws Throwable
	{
		System.out.print("买菜-->洗菜-->");
		Object obj=invocation.proceed();
		System.out.print("-->上菜");
		return obj;
		
	}

}


package com.ghr;

import org.springframework.aop.framework.ProxyFactory;

public class Vegetable 
{

	public static void main(String[] args) 
	{
		VegetalbeService target=new VegetalbeService();
		ProxyFactory factory=new ProxyFactory();
		factory.addAdvice(new ServiceDecorator());
		factory.setTarget(target);
		VegetalbeService proxy=(VegetalbeService)factory.getProxy()	;
		proxy.food();
		
	}
	
}

附有所需要的几个jar包,运行结果:
买菜-->洗菜-->切菜-->炒菜-->上菜

你可能感兴趣的:(java,spring,AOP)