Spring aop 拦截器(即面向切面编程)

package com.saic.grape;

public interface UserService {
    public void printUser(String user);
}



package com.saic.grape;

public class UserServiceImp implements UserService{

    @Override
    public void printUser(String user) {
        // TODO Auto-generated method stub
         System.out.println("printUser user:" + user);// 显示user  
    }

}

package com.saic.grape;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;


public class UserInterceptor implements MethodInterceptor{
// AOP方法拦截器

   

    @Override
    public Object invoke(MethodInvocation arg0) throws Throwable {
         try{
                if (arg0.getMethod().getName().equals("printUser"))
                // 拦截方法是否是UserService接口的printUser方法
                {
                    Object[] args = arg0.getArguments();// 被拦截的参数
                    System.out.println("user:" + args[0]);
                    arg0.getArguments()[0] = "hello!";// 修改被拦截的参数
                }
                System.out.println(arg0.getMethod().getName() + "---!");
                return arg0.proceed();// 运行UserService接口的printUser方法
            } catch (Exception e) {
                throw e;
            }
    }
}


package com.saic.grape;


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

public class TestInterceptor{

    public static void main(String[] args){
        ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath:aop.xml");
        //ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");    
        UserService us = (UserService) ctx.getBean("userService");
        us.printUser("ssss");

    }
}

<?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="userServiceImp"  class="com.saic.grape.UserServiceImp" />
    <!-- AOP拦截器(面向切面编程) -->
    <bean id="userInterceptor" class="com.saic.grape.UserInterceptor" />

    <bean id="userService" class="org.springframework.aop.framework.ProxyFactoryBean">
      <!-- 代理接口 -->
        <property name="proxyInterfaces">
            <value>com.saic.grape.UserService</value>
        </property>
       <!-- 目标实现类 -->
        <property name="target">
            <ref local="userServiceImp" />
      </property>
        <!-- 拦截器 -->
        <property name="interceptorNames">
            <list>
                <value>userInterceptor</value>
            </list>
        </property>
    </bean>

</beans>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值