记录学习--GenericFilterBean类解析

1.类

public abstract class GenericFilterBean implements Filter, BeanNameAware, EnvironmentAware, EnvironmentCapable, ServletContextAware, InitializingBean, DisposableBean

Filter:过滤器接口,定义init、doFilter、destroy三个方法。
BeanNameAware, EnvironmentAware, EnvironmentCapable, ServletContextAware:aware和Capable后缀的,可以获取到前面的这些BeanName、Environment、ServletContext。
InitializingBean, DisposableBean:Bean生命周期里面有,会执行afterPropertiesSet、destroy方法。
FilterConfig:Tomcat每次创建Filter的时候,也会同时创建一个FilterConfig类,这里包含了Filter配置文件的配置信息。

2.初始化做了哪些事

	public final void init(FilterConfig filterConfig) throws ServletException {
        Assert.notNull(filterConfig, "FilterConfig must not be null");
        this.filterConfig = filterConfig;
        PropertyValues pvs = new FilterConfigPropertyValues(filterConfig, this.requiredProperties);
        if (!pvs.isEmpty()) {
            try {
                BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
                ResourceLoader resourceLoader = new ServletContextResourceLoader(filterConfig.getServletContext());
                Environment env = this.environment;
                if (env == null) {
                    env = new StandardServletEnvironment();
                }

                bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, (PropertyResolver)env));
                this.initBeanWrapper(bw);
                bw.setPropertyValues(pvs, true);
            } catch (BeansException var6) {
                String msg = "Failed to set bean properties on filter '" + filterConfig.getFilterName() + "': " + var6.getMessage();
                this.logger.error(msg, var6);
                throw new NestedServletException(msg, var6);
            }
        }

        this.initFilterBean();
        if (this.logger.isDebugEnabled()) {
            this.logger.debug("Filter '" + filterConfig.getFilterName() + "' configured for use");
        }

    }

这一步将FilterConfig的初始化参数InitParameter设置到Bean的属性上。如果为空,初始化就完了,什么事也没做。
①PropertyValues pvs = new FilterConfigPropertyValues(filterConfig, this.requiredProperties);获取参数。
②BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);使用BeanWrapper修改对象属性。
③bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, (PropertyResolver)env));注册自定义属性编辑器,遇到 Resource 类型的属性将会使用 ResourceEditor 进行解析。
④bw.setPropertyValues(pvs, true);初始化参数。

3.执行顺序

需不需要考虑顺序,无序有没有影响。
考虑顺序的情况下:。
(未完待续~)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值