前往顾页
以后地位: 主页 > 收集编程 > Jsp实例教程 >

Spring引介加强

时候:2012-10-07 23:11来源:知行网www.zhixing123.cn 编辑:麦田守望者

引介加强是一种比较特别的加强范例,它不是在目标体例四周织入加强,而是为目标类建立新的体例和属性,所以引介加强的连接点是类级别的,而非体例级别的。经由过程引介加强,我们可以为目标类增加一个接口的实现,即本来目标类未实现某个接口,经由过程引介加强可以为目标类建立实现该接口的代办代理。

 

1Monitorable.java

package springdemo.aop.introduce;

 

public interface Monitorable {

void setMonitorActive(boolean active);

}

 

2ControlablePerformanceMonitor.java

package springdemo.aop.introduce;

import org.aopalliance.intercept.MethodInvocation;

import org.springframework.aop.support.DelegatingIntroductionInterceptor;

 

public class ControlablePerformanceMonitor extends

DelegatingIntroductionInterceptor implements Monitorable{

private ThreadLocal<Boolean> MonitorStatusMap=new ThreadLocal<Boolean>();

public void setMonitorActive(boolean active)

{

MonitorStatusMap.set(active);

}

//反对体例

public Object invoke(MethodInvocation mi)throws Throwable{

Object obj=null;

// 对支撑机能监控可控代办代理,经由过程判定其状况决定是不是开启机能监控服从

if(MonitorStatusMap.get()!=null && MonitorStatusMap.get()){

PFMonitor.begin(mi.getClass().getName()+"."+mi.getMethod().getName());

obj=super.invoke(mi);

PFMonitor.end();

}else{

obj=super.invoke(mi);

}

return obj;

}

}

 

3ForumService.java

package springdemo.aop.introduce;

 

public class ForumService{

public void removeTopic(int topicid){

System.out.println("摹拟删除Topic记录:"+topicid);

try{

Thread.currentThread().sleep(1000);

}catch(Exception e){

throw new RuntimeException(e);

}

}

 

public void removeForum(int fid)

{

System.out.println("摹拟删除Topic记录:"+fid);

try{

Thread.currentThread().sleep(1000);

}catch(Exception e){

throw new RuntimeException(e);

}

}

}

 

4PFMonitor.java

package springdemo.aop.introduce;

 

public class PFMonitor {

private static ThreadLocal<MethodPerformance> pfmonitorRecord=new ThreadLocal<MethodPerformance>();

public static void begin(String method){

System.out.println("begin monitor......");

MethodPerformance mp=new MethodPerformance(method);

pfmonitorRecord.set(mp);

}

public static void end(){

System.out.println("end monitor......");

MethodPerformance mp=pfmonitorRecord.get();

mp.printPerformance();

}

}

 

5MethodPerformance.java

package springdemo.aop.introduce;

 

public class MethodPerformance {

private long begin;

private long end;

private String serviceMethod;

public MethodPerformance(String serviceMethod){

this.serviceMethod=serviceMethod;

this.begin=System.currentTimeMillis();

}

public void printPerformance(){

end=System.currentTimeMillis();

long elapse=end-begin;

System.out.println(serviceMethod+"破钞了:"+elapse+"毫秒。");

System.out.println();

}

}

 

6introduce.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" >

<beans>

<bean id="forumServiceTarget" class="springdemo.aop.introduce.ForumService" />

<bean id="pmonitor" class="springdemo.aop.introduce.ControlablePerformanceMonitor" />

<bean id="forumService" class="org.springframework.aop.framework.ProxyFactoryBean" >

<property name="interfaces">

<!—引介加强所实现的接口 -->

<value>springdemo.aop.introduce.Monitorable</value>

</property>

<property name="interceptorNames" value="pmonitor" />

<property name="target" ref="forumServiceTarget" />

<!-- 因为引介加强必然要经由过程建立子类来天生代办代理,所以需求强迫利用CGLIB,否 则会报错-->

<property name="proxyTargetClass" value="true" />

</bean>

</beans>

 

7TestIntroduce.java

package springdemo.aop.introduce;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

public class TestIntroduce {

public static void main(String [] args){

String configPath="springdemo/aop/introduce/introduce.xml";

ApplicationContext ctx=new ClassPathXmlApplicationContext(configPath);

ForumService fs=(ForumService)ctx.getBean("forumService");

//默许环境下,未开启机能监控服从

fs.removeForum(1111);

fs.removeTopic(2222);

 

//开启机能监控服从

Monitorable monitorable=(Monitorable)fs;

monitorable.setMonitorActive(true);

 

// 在机能监控服从开启环境下,再次调用业务体例

fs.removeForum(1111);

fs.removeTopic(2222);

 

System.out.println("测试结束!~~~~");

}

}

 

顶一下
(1)
100%
踩一下
(0)
0%
------分开线----------------------------
标签(Tag):Java JAVA实例教程 JAVA根本教程 Java源代码 Java技能
------分开线----------------------------
颁发评论
请自发遵循互联网相关的政策法规,严禁公布色情、暴力、革命的谈吐。
评价:
神色:
考证码:点击我更换图片
猜你感兴趣