当前位置: 代码迷 >> Web前端 >> spring中的引介加强
  详细解决方案

spring中的引介加强

热度:182   发布时间:2012-08-25 10:06:20.0
spring中的引介增强
引介增强是一种比较特殊类型的增强


public interface Monitorable {

public void setMonitorActive(boolean active);
}


public class ControllablePerformanceMonitor extends DelegatingIntroductionInterceptor implements Monitorable{

private ThreadLocal<Boolean> MonitorStatusMap = new ThreadLocal<Boolean>();
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Object obj = null;
if(MonitorStatusMap.get()!=null && MonitorStatusMap.get()){
PerformanceMonitor.begin(invocation.getClass().getName()+"."+invocation.getMethod().getName());
obj = super.invoke(invocation);
PerformanceMonitor.end();
}
else{
obj = super.invoke(invocation);
}
return obj;
}

public void setMonitorActive(boolean active) {
MonitorStatusMap.set(active);
}
}
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext-introduce.xml");
ForumService fs  = (ForumService)app.getBean("forumService");

Monitorable mi = (Monitorable) fs;
mi.setMonitorActive(true);
fs.removeForum(1);
fs.removeTopic(2);
<!-- 引介增强 -->
<bean id="forumServiceTarget" class="org.spring.proxy.Invocation.Impl.ForumServiceImpl"/>
<bean id="controllablePerformanceMonitor" class="org.spring.introduce.ControllablePerformanceMonitor"/>
<bean id="forumService" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="interfaces">
<value>org.spring.introduce.Monitorable</value>
</property>
<property name="target" ref="forumServiceTarget"/>
<property name="interceptorNames">
<list>
<idref local="controllablePerformanceMonitor"/>
</list>
</property>
<property name="proxyTargetClass" value="true"/>
</bean>
  相关解决方案