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.执行顺序
需不需要考虑顺序,无序有没有影响。
考虑顺序的情况下:。
(未完待续~)