SpringAOP 的使用(两种方式)

 

 

 使用Spring AOP 拦截 方法,给被拦截的方法加上,前置通知,后置通知,例外通知,最终通知 的两个例子,见 附件

需要 spring.jar,cglib.jar,commons-logging.jar

 

方式1. 被拦截的类没有实现任何接口

 

关键代码:

package junit.test;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.aop.support.DefaultPointcutAdvisor;

import cn.com.xinli.common.BusinessMethodInterceptor;
import cn.com.xinli.service.BusinessInterface;
import cn.com.xinli.service.impl.BusinessImpl;
/*springAOP 测试方式1 的测试用例 只需要spring.jar 支持
 * 
 * */
public class AOPTest
{
	static BusinessInterface face=null;
	@BeforeClass
	public static void setUpBeforeClass() throws Exception
	{
		BusinessImpl bi=new BusinessImpl();
		ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
		proxyFactoryBean.setTarget(bi);
		proxyFactoryBean.addAdvisor(new DefaultPointcutAdvisor(new BusinessMethodInterceptor()));
		proxyFactoryBean.setInterfaces(new Class[]{BusinessInterface.class});
		face= (BusinessInterface)proxyFactoryBean.getObject();
	}

	@Test
	public void testAdd() throws Exception
	{
		
		
		
		face.add();
	}

	@Test
	public void testDel()
	{
		face.del();
	}

	@Test
	public void testFind()
	{
		face.find();
	}

	@Test
	public void testUpdate()
	{
		face.update();
	}

}

 

 

 

方式2. 被拦截的类没有实现任何接口

 

关键代码:

 

package junit.test;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.aop.support.DefaultPointcutAdvisor;

import cn.com.xinli.common.BusinessMethodInterceptor;
import cn.com.xinli.service.impl.Business;
/*springAOP 测试方式2 的测试用例 需要spring.jar和cglib.jar 支持
 * 
 * */
public class AOPTest2
{
	static Business business =null;
	@BeforeClass
	public static void setUpBeforeClass() throws Exception
	{
				/**/
				ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
				proxyFactoryBean.addAdvisor(new DefaultPointcutAdvisor( new BusinessMethodInterceptor()));
				proxyFactoryBean.setTarget(new Business());
				business = (Business) proxyFactoryBean.getObject();
	}

	@Test
	public void testAdd() throws Exception
	{
		/*另一种调用方式*/
		Business target = new Business();
        ProxyFactory pf = new ProxyFactory();
        pf.addAdvice(new BusinessMethodInterceptor()); 
        pf.setTarget(target);
        Business proxy = (Business) pf.getProxy();
        //输出代理对象调用的信息
         proxy.add();    
	}

	@Test
	public void testDel()
	{
		business.del();
	}

	@Test
	public void testFind()
	{
		business.find();
	}

	@Test
	public void testUpdate()
	{
		business.update();
	}

}

 

参考:

 

http://blog.csdn.net/Jyeafee/archive/2006/01/11/575972.aspx  

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值