运用pring的aop机制可以做到对某些具体方法的拦截和过滤,这里举一个简单的例子,通过运用spring的aop机制实现对非法字符的过滤。
什么是aop呢?简单的用一句话概括,就是通过proxy,根据你所定义的aspect,把advice根据指定的pointcut织入到你proxy所代理的target中,这就是aop。所以aop基本包含这5个术语。好了,不多说了,我们现在就用具体的例子来实现aop的拦截。
首先我们定义一个接口,具体代码如下所示
package org.whatisjava.test.aop;
/**
* 首先定义一个接口
* @author ij
*
*/
public interface SomeService {
public String saySome(String msg);
}
接着对这个接口做一个实现,具体代码如下所示,这个就是要代理的target对象
package org.whatisjava.test.aop;
/**
*
* @author ij
*
*/
public class SomeServiceImpl implements SomeService {
public String saySome(String msg) {
return msg; //简单处理,将字符串返回即可
}
}
接下来定义一个专门用于拦截非法字符的advice,具体代码如下
package org.whatisjava.test.aop;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
/**
* 能够过滤某些字符的advice
* @author Lan
*
*/
public class InterceptorAdvice implements MethodInterceptor {
public Object invoke(MethodInvocation mi) throws Throwable {
Object[] args = mi.getArguments(); //通过MethodInvocation可以将调用方法的参数取出
String str = (String)args[0];
System.out.println("---------" + str);
if("傻X".equalsIgnoreCase(str)) {
return "请不要说脏话";
}
return mi.proceed();
}
}
这个类要实现MethodInterceptor 这个接口,重写invoke方法,专门对方法中的参数进行拦截,MethodInterceptor 此接口在spring.jar中,如果感兴趣的话可以关注一下源码。
接下来就是最关键的spring的配置文件了,具体内容如下,很好理解
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- Advice-->
<bean id="interceptorAdvice" class="org.whatisjava.test.aop.InterceptorAdvice"/>
<!-- Target,要代理的目标对象 -->
<bean id="someService" class="org.whatisjava.test.aop.SomeServiceImpl"/>
<!-- Pointcut,对指定的方法名称进行匹配-->
<bean id="somePointcut" class="org.springframework.aop.support.NameMatchMethodPointcut">
<property name="mappedName">
<value>saySome</value>
</property>
</bean>
<!-- Aspect(Advisor)包括Advice和Pointcut -->
<bean id="someAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="advice">
<ref bean="interceptorAdvice"/>
</property>
<property name="pointcut">
<ref bean="somePointcut"/>
</property>
</bean>
<!-- Proxty包括Target和Aspect(Advisor) -->
<bean id="someServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref bean="someService"/>
</property>
<property name="interceptorNames">
<list>
<value>someAdvisor</value>
</list>
</property>
</bean>
</beans>
接下来进行测试就可以了,具体代码如下所示
package junit.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.whatisjava.test.aop.SomeService;
public class InterceptorTest {
private static ApplicationContext ctx;
static {
ctx = new ClassPathXmlApplicationContext("org/whatisjava/test/aop/aopContext.xml");
}
@Test
public void tstInterceptor() {
SomeService service = (SomeService)ctx.getBean("someServiceProxy");
String str = service.saySome("傻X");
System.out.println(str);
}
}
运行测试,输入结果“请不要说脏话”,证明拦截成功。
换一个词,比如“你好”,则正常输入。说明此小小的拦截功能实现成功。
分享到:
- 2009-10-19 11:49
- 浏览 2447
- 评论(8)
- 论坛回复 / 浏览 (8 / 5984)
- 查看更多
相关推荐
springaop拦截controller日志
Spring AOP 拦截器 Advisor Spring AOP 拦截器 Advisor
spring aop切面拦截指定类和方法实现流程日志跟踪 一般情况下,在不侵入业务代码的情况下,需要做流程日志跟踪是比较合理的 采用springaop切面思想
项目中含有一整个springboot实现aop的功能,在拦截的方法形式上有两种一种是通过切点设置为拦截某个包路径下面的类中的所有方法;还有一种是基于某个自定义注解的.
springboot spring aop 拦截器 注解方式实现脱敏(涉及到:pom.xml -->application.properties --->启动类-->拦截器)
spring aop 拦截实例,下载下来直接就可以用了,很好很方便。
Spring AOP的实现机制中文版,动态代理及原理,自定义类加载器
spring aop 拦截系统操作日志和异常日志,存储到数据库中的全过程
spring aop 拦截 实例spring aop 拦截 实例spring aop 拦截 实例
死磕Spring之AOP篇 - Spring AOP两种代理对象的拦截处理(csdn)————程序
本篇文章主要介绍了详解Spring AOP 拦截器的基本实现,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
用spring写的小示例程序,拦截某一层的所有方法。可以在调用之前、之后、抛出异常拦截调用,日志打印。
spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop
spring aop jar 包
Spring Mvc AOP通过注解方式拦截controller等实现日志管理
内含有mybatis 拦截器实现的分页代码,spring 的事务和aop 测试、和反射工具类
spring-aop-1.1.1.jar spring-aop-1.2.6.jar spring-aop-1.2.9.jar spring-aop-2.0.2.jar spring-aop-2.0.6.jar spring-aop-2.0.7.jar spring-aop-2.0.8.jar spring-aop-2.0.jar spring-aop-2.5.1.jar spring-aop-...
描述一下Spring AOP? 在Spring AOP中关注点(concern)和横切关注点(cross-cutting concern)有什么不同? AOP有哪些可用的实现? Spring中有哪些不同的通知类型(advice types)? Spring AOP 代理是什么? 引介...
ssh2登陆+spring aop做拦截
Spring MVC AOP通过自定义注解方式拦截Controller等实现日志管理, springMVC里做添加AOP拦截,用于捕获异常。