spring 拦截器中拦截/和拦截/*的区别

Spring中拦截/和拦截/*的区别_百度知道
Spring中拦截/和拦截/*的区别
提问者采纳
最简单也最实用;*;user&#47、拦截&#47、拦截/user&#47,js.htm.do、拦截*;add1,不能访问到jsp。3.do这是最传统的方式:&#47,事情就是麻烦一些, 例如;:会导致静态文件(jpg。很多互联网类型的应用很喜欢这种风格的URL,例如:&#47,css)被拦截,但转到jsp时再次被拦截;add可以实现现在很流行的REST风格,css)被拦截后不能正常显示。想实现REST风格,请求可以走到Action中。不会导致静态文件(jpg,这是一个错误的方式、*,js。弊端。2。后面有解决办法还算简单
其他类似问题
spring的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁Spring中拦截/和拦截/*的区别_百度知道
Spring中拦截/和拦截/*的区别
提问者采纳
2,不能访问到jsp1,css)被拦截,js:&#47.htm,例如。弊端。3,事情就是麻烦一些, 例如:/add.do。后面有解决办法还算简单.do这是最传统的方式,这是一个错误的方式,最简单也最实用。很多互联网类型的应用很喜欢这种风格的URL。想实现REST风格。不会导致静态文件(jpg、*:会导致静态文件(;user/add可以实现现在很流行的REST风格,js,css)被拦截后不能正常显示、拦截/*,请求可以走到Action中,但转到jsp时再次被拦截、拦截*、拦截/user&#47
其他类似问题
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁AOP是一种将通用逻辑与具体业务分离的技术,能够弥补OO在横向代码复用不足的问题,很好的实现separation of concerns (SoC)。缓存是改善系统性能的一种常用技术,采取以空间换时间的策略。缓存就是与具体业务无关的,如果我们设计一个缓存框架,那么应该是可插拔的,对系统业务代码无侵入的,这很符合AOP的适用场景。我们的项目采用了Ehcache缓存框架作为底层支撑,采用Spring框架的AOP进行方法拦截,将耗时方法的返回结果,进行缓存。现在我们使用spring的aop模拟这种实现。
首先假设我们有一个已经编写好的,比较耗时的类
package net.aty.
public class DaoImpl
public String query(int id)
// 模拟耗时的数据库查询操作
System.out.println(&query begin...&);
Thread.sleep(100 * id);
System.out.println(&query over...&);
} catch (InterruptedException e)
e.printStackTrace();
return &result:& + id * 10;
下面是我们编写的缓存实现类
package net.aty.
import java.util.HashM
import java.util.M
import org.aspectj.lang.ProceedingJoinP
import org.aspectj.lang.S
public class CacheQueryResult
private Map&String, Object& buffer = new HashMap&String, Object&();
public Object around(ProceedingJoinPoint point) throws Throwable
String key = uniqueKey(point);
Object returnValue = buffer.get(key);
if(returnValue != null)
return returnV
Object object = point.proceed();
buffer.put(key, object);
private String uniqueKey(ProceedingJoinPoint point)
Object target = point.getTarget();
Signature signature = point.getSignature();
String methodSignature = signature.toString();
String key = target.hashCode() + methodS
现在我们在spring.xml中进行配置,实现缓存切面对目标对象的拦截
&?xml version=&1.0& encoding=&UTF-8&?&
&beans xmlns=&http://www.springframework.org/schema/beans&
xmlns:xsi=&http://www.w3.org/2001/XMLSchema-instance& xmlns:context=&http://www.springframework.org/schema/context&
xmlns:aop=&http://www.springframework.org/schema/aop&
xsi:schemaLocation=&http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd&&
&!-- 需要被拦截的目标对象 --&
&bean id=&dao& class=&net.aty.dao.DaoImpl&&&/bean&
&!-- 方法拦截器和环绕增强--&
&bean id=&cacheInterceptor& class=&net.aty.cache.CacheQueryResult&&&/bean&
&aop:config proxy-target-class=&true&&
&aop:pointcut id=&cachePointcut& expression=&execution(public * net.aty.dao.DaoImpl.*(..))& /&
&aop:aspect id=&cacheAspect& ref=&cacheInterceptor&&
&aop:around method=&around& pointcut-ref=&cachePointcut& /&
&/aop:aspect&
&/aop:config&
测试类如下:
package net.
import net.aty.dao.DaoI
import org.springframework.context.support.ClassPathXmlApplicationC
public class TestMain
private static ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
&spring.xml&);
public static void main(String[] args)
for (int i = 0; i & 3; i++)
testDao();
public static void testDao()
DaoImpl dao = (DaoImpl) context.getBean(&dao&);
long begin = System.currentTimeMillis();
String result = dao.query(2);
long end = System.currentTimeMillis();
System.out.println(result + &,cost time:& + (end - begin));
通过执行结果发现,后面2次调用是直接从缓存中取的数据,这样就达到了我们模拟缓存的效果。
最后附上该工程的结构图和依赖的spring3.1.2的jar
至此我们实现采用aspect的方式,达到了方法拦截的效果。接下来使用MethodInteceptor和advisor完成相同的效果。
package net.aty.
import org.aopalliance.intercept.MethodI
import org.aopalliance.intercept.MethodI
public class CacheMethodInterceptor implements MethodInterceptor
public Object invoke(MethodInvocation invocation) throws Throwable
System.out.println(&begin MethodInterceptor&);
Object result = invocation.proceed();
System.out.println(&end MethodInterceptor&);
对应的xml配置如下:
&bean id=&cacheMethodInterrupter& class=&net.aty.cache.CacheMethodInterceptor& /&
&aop:config proxy-target-class=&true&&
&aop:pointcut id=&servicePointcut& expression=&execution(public * net.aty.service.WeatherService.*(..))& /&
&aop:advisor advice-ref=&cacheMethodInterrupter& pointcut-ref=&servicePointcut& /&
&/aop:config&spring aop的过滤器aspect和拦截器Interceptor有什么区别和联系呢?
[问题点数:20分,结帖人OnlyOneLove]
spring aop的过滤器aspect和拦截器Interceptor有什么区别和联系呢?
[问题点数:20分,结帖人OnlyOneLove]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
相关帖子推荐:
本帖子已过去太久远了,不再提供回复功能。

我要回帖

更多关于 spring mvc 拦截器 的文章

 

随机推荐