`
gigi_112
  • 浏览: 113218 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

拦截类型的拦截器

阅读更多

 

先给大家介绍一种拦截方式:拦截类型的拦截器。故名思议:比如我想拦截所有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类型转换 拦截器 校验的例子

    这是一个关于struts2的类型转换 以及 拦截器 还有校验的例子

    Spring3_AOP四种创建通知(拦截器)类型

    Spring_AOP四种创建通知(拦截器)类型,介绍的比较详细,有实例

    java拦截器_类型转换_国际化

    java 拦截器_类型转换_国际化 原代码

    MyBatis拦截器:给参数对象属性赋值的实例

    下面小编就为大家带来一篇MyBatis拦截器:给参数对象属性赋值的实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    统计时间的拦截器

    编写struts2的拦截器 统计页面跳转时间

    Spring AOP四种创建通知(拦截器)类型

    Spring AOP四种创建通知(拦截器)类型

    360弹窗拦截器 v6独立版-流氓软件弹窗广告拦截神器.zip

    当然我们也可以选择“强力拦截”,“一般拦截”和“强力拦截”的区别就在于“一般拦截”只拦截广告,而“强力拦截”只要是弹窗不管是不是广告都会拦截,根据你的需要自己选择拦截类型 当然,我们也可以手动添加...

    struts2自定义类型转换、拦截器实例练习

    myeclipse平台下使用struts2.3框架搭建实例,理解struts2工作原理,了解struts2自定义类型转换,自定义拦截器,掌握struts2的mvc框架应用

    struts2中拦截器的使用

    Struts2在com.opensymphony.xwork2.interceptor.annotations包中定义了3个拦截器注解类型。

    拦截封包工具.zip

    提供3种可选拦截类型 1.双击监听列表显示数据 2.监听域名的意思是只监听关键域名或关键词 (只拦截此域名的网址) 3.IP地址是默认本机IPv4 可设置安卓的代理IP (安卓封包拦截) 4.端口写入需重启生效 (默认为8888)...

    Struts2 国际化字符串 拦截器

    下面让我们来做一个Velocity模板输出的例子,首先在classes/struts.xml中新建一个Action映射(Mapping),将其result类型设为velocity,如以下代码所示: &lt;result type="velocity"&gt;/HelloWorld.vm 例3 classes/...

    解决Vue在封装了Axios后手动刷新页面拦截器无效的问题

    就在一个夜黑风高的晚上,我头晕眼花的按F5手动刷新了一下页面,惊奇的发现拦截器失效了,并没有拦截外面的Coode Json代码层,而是直接把服务器的全部json数据劈头盖脸的砸在了我的脸上。一脸懵逼的我以为是接口的...

    java-grpc-prometheus:Java拦截器,可用于使用Prometheus监视Grpc服务

    对于每个RPC,拦截器都会增加以下Prometheus指标,并按方法类型,服务名称,方法名称和响应代码进行细分: 服务器 grpc_server_started_total :在服务器上启动的RPC总数。 grpc_server_handled_total :无论成功...

    struts dom4j json gson xml 拦截器 类型转换

    NULL 博文链接:https://hi-beijing.iteye.com/blog/1173801

    使用Annotation和拦截器实现访问控制 (2010年)

    访问控制是Web应用系统中的主要问题之一。基于数据库和基于配置文件两种...Interceptor(拦截器)是Struts2的一个强有力的工具,用于在某个方法或字段被访问之前进行拦截,然后在之前或之后加入某些操作,它是AOP(Aspect-O

    防止非法攻击拦截器

    web应用安全验证,防止非法攻击。防客户端攻击类型:内容欺骗,认证不充分,命令执SQL注入;跨站脚本攻击、链接注入。

    SSH2及模型驱动,拦截器注入

    很适合初学者学习! 自定义默认栈 Struts2的跳转Result的类型 在Interceptor中注入Spring管理的对象

    Spring拦截器

    Spring boot Interceptor工作原理使用场景常用拦截器接口HandlerInterceptor例子工作流程AsyncHandlerInterceptor例子工作流程HandlerInterceptorAdapter例子工作流程WebRequestInterceptor例子工作流程配置拦截器 ...

    play框架手册

    但是我们可以在一个完全不同的类里定义一些拦截器,然后在任何控制器里使用@With注释来链接他们。 - 42 - Session和Flash作用域 - 42 - 04.模板引擎 - 43 - 模板语法 - 43 - Expressions: ${…} - 44 - Template ...

Global site tag (gtag.js) - Google Analytics