SpringAOP之引介增强 IntroductionInterceptor

SpringAOP常见的有前置、后置、环绕、异常处理等技术,今天介绍AOP中的引介增强。这个技术是类级别的。


定义Monitor


package com.augmentum.introductionInterceptor;

public interface Monitor {
	void setMonitorActive(boolean bool);
}


实现类继承了 IntroductionInterceptor


package com.augmentum.introductionInterceptor;

public class PerformanceMonitor {
	private static ThreadLocal<MethodPerformace> performaceRecord = new ThreadLocal<MethodPerformace>();
	public static void begin(String method) {
		System.out.println("begin monitor...");
		MethodPerformace mp = performaceRecord.get();
		if(mp == null){
			mp = new MethodPerformace(method);
			performaceRecord.set(mp);
		}else{
		    mp.reset(method);	
		}
	}
	public static void end() {
		System.out.println("end monitor...");
		MethodPerformace mp = performaceRecord.get();
		mp.printPerformace();
	}
}



业务类


package com.augmentum.introductionInterceptor;

public class ForumService {

	public void removeTopic(int topicId) {
		System.out.println("模拟删除Topic记录:"+topicId);
		try {
			Thread.currentThread().sleep(20);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}		

	}

	public void removeForum(int forumId) {
		System.out.println("模拟删除Forum记录:"+forumId);
		try {
			Thread.currentThread().sleep(40);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}		
	}
}


测试类


package com.augmentum.introductionInterceptor;

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

public class TestIntroduce {
	public static void main(String[] args) {
		String path = "conf/conf-advice-introduce.xml";
		ApplicationContext ac = new ClassPathXmlApplicationContext(path);
		ForumService forumService = (ForumService) ac.getBean("forumService");
		forumService.removeForum(100);
		
		Monitor monitor = (Monitor)forumService;
		monitor.setMonitorActive(true);
		
		forumService.removeForum(100);
		forumService.removeTopic(1000);
	}
}


配置文件


<?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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<bean id="monitor" class="com.augmentum.introductionInterceptor.ControMonitor"/>
	<bean id="target" class="com.augmentum.introductionInterceptor.ForumService"/>
	
	<bean id="forumService" class="org.springframework.aop.framework.ProxyFactoryBean"
		p:interfaces="com.augmentum.introductionInterceptor.Monitor" 
		p:target-ref="target"
		p:interceptorNames="monitor" p:proxyTargetClass="true" />
</beans>




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值