MyException - 我的异常网
当前位置:我的异常网» 互联网 » 阻截类型的拦截器

阻截类型的拦截器

www.MyException.Cn  网友分享于:2013-05-03  浏览:10次
拦截类型的拦截器

 

先给大家介绍一种拦截方式:拦截类型的拦截器。故名思议:比如我想拦截所有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,造成程序中出现其他拦截器失效的问题。

 

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有