spring拦截器.学习.

本文通过一个具体的示例展示了如何使用 Spring AOP 进行方法拦截。包括定义接口和服务实现类,创建 AOP 拦截器来改变方法的行为,并在调用方法时输出拦截前后的结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

接口类
package com.test.TestSpring3;

public interface UserService // 被拦截的接口
{
    
public void printUser(String user);
}


实现类

package com.test.TestSpring3;

public class UserServiceImp implements UserService // 实现UserService接口
{
    
public void printUser(String user) {
        System.
out.println("printUser user:" + user);// 显示user
    }

}


 AOP方法拦截器
package com.test.TestSpring3;

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

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

    
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.test.TestSpring3;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class TestInterceptor {

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

    }

}

 

配置文件如下:

<?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.test.TestSpring3.UserServiceImp" />

    
<bean id="userInterceptor" class="com.test.TestSpring3.UserInterceptor" />

    
<bean id="userService"
        class
="org.springframework.aop.framework.ProxyFactoryBean">
        
<property name="proxyInterfaces">
            
<value>com.test.TestSpring3.UserService</value>
        
</property>
        
<property name="target">
            
<ref local="userServiceImp" />
        
</property>
        
<property name="interceptorNames">
            
<list>
                
<value>userInterceptor</value>
            
</list>
        
</property>
    
</bean>

</beans>

 

输出:

 user:shawn
printUser---!
printUser user:hello!      调用方法的时候 传入的值被拦截修改了.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值