先给大家介绍一种拦截方式:拦截类型的拦截器。故名思议:比如我想拦截所有com.xx.xx.ConsumerBean,要是单独拦截某些beanId会显的很笨重,那么提供一种拦截器,拦截类型。代码如下:
/** * Created by IntelliJ IDEA. * User: guzhen.cg * Date: 13-4-10 * Time: 下午3:15 * To change this template use File | Settings | File Templates. */ import java.lang.reflect.AccessibleObject; import java.lang.reflect.Field; import java.util.*; import java.util.concurrent.ConcurrentHashMap; import org.springframework.aop.TargetSource; import org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator; import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.util.Assert; import org.springframework.util.PatternMatchUtils; import org.springframework.web.context.ContextLoader; import org.springframework.web.context.ContextLoaderListener; /** * 类BeanTypeAutoProxyCreator.java的实现描述:使用配置类型代替Springframework中配置名称的实现 * * @author guzhen.cg 2013-04-18 晚上11:40 */ public class BeanTypeAutoProxyCreator extends BeanNameAutoProxyCreator implements InitializingBean, ApplicationContextAware { private static final long serialVersionUID = -9094985530794052264L; private Class<?> targetBeanType; private ApplicationContext context; /** * @param targetClass the targetClass to set */ public void setTargetBeanType(Class<?> targetClass) { this.targetBeanType = targetClass; } public void setApplicationContext(ApplicationContext context) { this.context = context; } /** * Return if the given bean name matches the mapped name. * <p> * The default implementation checks for "xxx*", "*xxx" and "*xxx*" matches, as well as direct equality. Can be * overridden in subclasses. * * @param beanName the bean name to check * @param mappedName the name in the configured list of names * @return if the names match * @see org.springframework.util.PatternMatchUtils#simpleMatch(String, String) */ protected boolean isMatch(String beanName, String mappedName) { return PatternMatchUtils.simpleMatch(mappedName, beanName); } public void afterPropertiesSet() throws Exception { Assert.notNull(targetBeanType, "targetType cannot be null"); String[] beanNames =context.getBeanNamesForType(targetBeanType, true, false); // String[] beanNames = context.getBeanNamesForType(targetBeanType); //太坑爹,不要初始化bean if (beanNames == null) { logger.warn("can not found suit bean by " + targetBeanType ); return ; } String[] result = new String[beanNames.length]; for (int i = 0; i < beanNames.length; i++) { result[i] = beanNames[i].substring(1); //截取第一个字符& } super.setBeanNames(result); } }
这个拦截器很简单,继承beanname自动拦截器,实现了ApplicationContextAware接口,获得程序中的context。这时走了一个很大的弯路,通过context获取需要拦截的beanID时,如果第三个参数allowEagerInit(渴望初始化)用默认值:true,则此时会去初始化部分bean,造成程序中出现其他拦截器失效的问题。
相关推荐
网页封包拦截器是网页封包拦截的利器,是一款完全免费的绿色国产软件,能让您事半功倍,而且是为数不多的几款支持Win7的拦截软件。 网页封包拦截器支持: 2000/XP/2003/Vista/2008/Win7 全系列系统 网页封包拦截器...
这是一个关于struts2的类型转换 以及 拦截器 还有校验的例子
Spring_AOP四种创建通知(拦截器)类型,介绍的比较详细,有实例
java 拦截器_类型转换_国际化 原代码
下面小编就为大家带来一篇MyBatis拦截器:给参数对象属性赋值的实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
编写struts2的拦截器 统计页面跳转时间
Spring AOP四种创建通知(拦截器)类型
当然我们也可以选择“强力拦截”,“一般拦截”和“强力拦截”的区别就在于“一般拦截”只拦截广告,而“强力拦截”只要是弹窗不管是不是广告都会拦截,根据你的需要自己选择拦截类型 当然,我们也可以手动添加...
myeclipse平台下使用struts2.3框架搭建实例,理解struts2工作原理,了解struts2自定义类型转换,自定义拦截器,掌握struts2的mvc框架应用
Struts2在com.opensymphony.xwork2.interceptor.annotations包中定义了3个拦截器注解类型。
提供3种可选拦截类型 1.双击监听列表显示数据 2.监听域名的意思是只监听关键域名或关键词 (只拦截此域名的网址) 3.IP地址是默认本机IPv4 可设置安卓的代理IP (安卓封包拦截) 4.端口写入需重启生效 (默认为8888)...
下面让我们来做一个Velocity模板输出的例子,首先在classes/struts.xml中新建一个Action映射(Mapping),将其result类型设为velocity,如以下代码所示: <result type="velocity">/HelloWorld.vm 例3 classes/...
就在一个夜黑风高的晚上,我头晕眼花的按F5手动刷新了一下页面,惊奇的发现拦截器失效了,并没有拦截外面的Coode Json代码层,而是直接把服务器的全部json数据劈头盖脸的砸在了我的脸上。一脸懵逼的我以为是接口的...
对于每个RPC,拦截器都会增加以下Prometheus指标,并按方法类型,服务名称,方法名称和响应代码进行细分: 服务器 grpc_server_started_total :在服务器上启动的RPC总数。 grpc_server_handled_total :无论成功...
NULL 博文链接:https://hi-beijing.iteye.com/blog/1173801
访问控制是Web应用系统中的主要问题之一。基于数据库和基于配置文件两种...Interceptor(拦截器)是Struts2的一个强有力的工具,用于在某个方法或字段被访问之前进行拦截,然后在之前或之后加入某些操作,它是AOP(Aspect-O
web应用安全验证,防止非法攻击。防客户端攻击类型:内容欺骗,认证不充分,命令执SQL注入;跨站脚本攻击、链接注入。
很适合初学者学习! 自定义默认栈 Struts2的跳转Result的类型 在Interceptor中注入Spring管理的对象
Spring boot Interceptor工作原理使用场景常用拦截器接口HandlerInterceptor例子工作流程AsyncHandlerInterceptor例子工作流程HandlerInterceptorAdapter例子工作流程WebRequestInterceptor例子工作流程配置拦截器 ...
但是我们可以在一个完全不同的类里定义一些拦截器,然后在任何控制器里使用@With注释来链接他们。 - 42 - Session和Flash作用域 - 42 - 04.模板引擎 - 43 - 模板语法 - 43 - Expressions: ${…} - 44 - Template ...