首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

sping中怎么配置拦截器

2012-10-29 
sping中如何配置拦截器sping中如何配置拦截器配置如下bean idaroundAdvice /beanbean idbeforeA

sping中如何配置拦截器
sping中如何配置拦截器
配置如下

    <bean id="aroundAdvice" ></bean>    <bean id="beforeAdvice" ></bean>       <!-- 相当于JDKProxy,不过他能代理任何类的对象 -->    <bean id="proxy" >        <!-- 以下是JDK动态代理要求的传入的接口 -->     <!-- 第一步:配置接口 -->    <property name="proxyInterfaces">    <list>    <value>com.cs.dao.UserDao</value>    </list>    </property>     <!--  这是如何配置cglib 方式 cglib方式是spring已经提供的只需要配置即可 能代理任何类  与上面的jdk配置只能二选一    <property name="proxyTargetClass" value="true" />        -->          <!-- 第二步:配置目标对象 , 及腰拦截的对象是哪个 -->    <property name="target">    <bean name="code">import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;public class AroundAdvice implements MethodInterceptor {public Object invoke(MethodInvocation arg0) throws Throwable {System.out.println("开启事务");Object obj = arg0.proceed() ; // 相当于调用真正的方法System.out.println("关闭事务");return obj;}}

beforeAdvice的代码如下:(二选一)
import org.springframework.aop.MethodBeforeAdvice;public class BeforeAdvice implements MethodBeforeAdvice {public void before(Method method, Object[] args, Object target)throws Throwable { System.out.println("日志记录:" + method.getName() + " is inteceptor");        //在方法调用之前做的事情}}
测试类:
public static void main(String[] args) {    ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml") ;        UserDao uDao = (UserDao)ctx.getBean("proxy") ; //如果配置的是cglib方式 ,则能代理任何类        uDao.addUser(new User()) ;}

热点排行