Chinaunix

标题: Spring自动代理 [打印本页]

作者: northwolf    时间: 2008-04-23 09:00
标题: Spring自动代理

                                接触Srping已经好长时间了,没有学精它,用到哪就看哪,不求甚解难以精通一项技术了,惭愧!项目中有个需求要在每个线程停止自动检查线程所使用资源,所以我想拦截interrupt方法就可实现,经测试没有成功,因为
Aop拦截的类必须统一实现一个接口,interrupt方法又是Thread自带的,项目中线程没有统一定义接口,所以失败但还是测试了一下Spring的自动代理,感觉还是挺好用的,在这个新技术新框架层出的年代,自己真的有点过时了,前一段时间听说Spring的创始人已经投奔微软了,我想对java还是会有一定影响的。
下面是测试代码,很简单自己记录一下,以备后用。
//业务类
package com.xujj.spring;
/**
* IHelloWorld接口
*
* @author xujj
*/
public  interface IHelloWorld {
    public abstract  String test(String a);
}
package com.xujj.spring;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* IHelloWorld接口实现
*
* @author xujj
*/
public class HelloWorld implements IHelloWorld {
    private static final Log log = LogFactory.getLog(HelloWorld.class);
    public String test(String a) {
        log.info(a);
        return a;
    }
}
//通知,拦截方法
/**
*
*/
package com.xujj.spring;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
/**
* @author xujj
*
*/
public class TestAdvice implements MethodInterceptor {
    public Object invoke(MethodInvocation arg0) throws Throwable {
        System.out.println("~~~~~~~~~~~~~~~~before~~~~~~~~~~~~");
        String value = String.valueOf(arg0.getArguments()[0]);
        if (value.startsWith("hello"))
            arg0.proceed();
        System.out.println("~~~~~~~~~~~~~~~after~~~~~~~");
        return null;
    }
}
//客户端:
package com.xujj.spring;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
/**
* HelloWorld客户应用
*
* @author xujj
*/
public class HelloClient {
    protected static final Log log = LogFactory.getLog(HelloClient.class);
    public static void main(String[] args) {
        ApplicationContext ctx=new FileSystemXmlApplicationContext("src/appcontext.xml");
       IHelloWorld hw = (IHelloWorld) ctx.getBean("hello");
//通过代理获取实例,只能转换成接口类型,不能转换成具体类
       hw.test("ahello world!!");
    }
}
//配置:
?xml version="1.0" encoding="UTF-8"?>
!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
   "http://www.springframework.org/dtd/spring-beans.dtd">
beans>
    bean id="advisor"
        class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        property name="advice">
            ref bean="TestAdvice" />
        /property>
        property name="pattern">
            value>.*test.*/value>
        /property>
    /bean>
    bean id="TestAdvice" class="com.xujj.spring.TestAdvice">/bean>
    bean id="autoProxyCreator"
        class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />
    bean id="hello" class="com.xujj.spring.HelloWorld" />
/beans>
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/40588/showart_573445.html




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2