SpringMVC源码分析--容器初始化(三)HttpServletBean

本文为转载,地址:http://blog.csdn.net/qq924862077/

在上一篇博客 springMVC源码分析--容器初始化(二)DispatcherServlet中,我们队SpringMVC整体生命周期有一个简单的说明,并没有进行详细的源码分析,接下来我们会根据博客中提供的springMVC的生命周期图来详细的对SpringMVC的相关源码进行分析。

在上一篇博客中我们了解到,SpringMVC初始化配置是在父类HttpServletBean的init方法中,其实HttServletBean是一个比较简单的类,在这个类中并没有太复杂的功能,主要的函数是init函数中,源码如下:

主要工作就是设置我们在web.xml中配置的contextConfigLocation属性,当然这个属性是springMVC文件的配置文件地址,当然也可以不用配置,使用默认名称加载。

initBeanWrapper 函数并没有具体实现

initServletBean()是模板方法,是在子类FrameworkServlet中实现的,接下来我们会详细分析,这样HttpServletBean的主体功能就介绍完了。

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1.       public final void init() throws ServletException {  
  2. if (logger.isDebugEnabled()) {  
  3.     logger.debug("Initializing servlet '" + getServletName() + "'");  
  4. }  
  5.   
  6. // Set bean properties from init parameters.  
  7. try {  
  8.     //获得web.xml中的contextConfigLocation配置属性,就是spring MVC的配置文件  
  9.     PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);  
  10.     BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);  
  11.     //获取服务器的各种信息  
  12.     ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());  
  13.     bw.registerCustomEditor(Resource.classnew ResourceEditor(resourceLoader, getEnvironment()));  
  14.     //模板方法,可以在子类中调用,做一些初始化工作,bw代表DispatcherServelt  
  15.     initBeanWrapper(bw);  
  16.     //将配置的初始化值设置到DispatcherServlet中  
  17.     bw.setPropertyValues(pvs, true);  
  18. }  
  19. catch (BeansException ex) {  
  20.     logger.error("Failed to set bean properties on servlet '" + getServletName() + "'", ex);  
  21.     throw ex;  
  22. }  
  23.   
  24. // Let subclasses do whatever initialization they like.  
  25. //模板方法,子类初始化的入口方法  
  26. initServletBean();  
  27.   
  28. if (logger.isDebugEnabled()) {  
  29.     logger.debug("Servlet '" + getServletName() + "' configured successfully");  
  30. }  
当然HttpServletBean还是提供了一些其他方法的,其实都是一些比较简单的get和set方法,就不做过多介绍了,HttpServletBean的完整源码如下:

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  *HttpServlet的一个简单扩展类 
  3.  */  
  4. @SuppressWarnings("serial")  
  5. public abstract class HttpServletBean extends HttpServlet  
  6.         implements EnvironmentCapable, EnvironmentAware {  
  7.   
  8.     protected final Log logger = LogFactory.getLog(getClass());  
  9.   
  10.     private final Set<String> requiredProperties = new HashSet<String>();  
  11.   
  12.     private ConfigurableEnvironment environment;  
  13.   
  14.     protected final void addRequiredProperty(String property) {  
  15.         this.requiredProperties.add(property);  
  16.     }  
  17.     @Override  
  18.     public final void init() throws ServletException {  
  19.         if (logger.isDebugEnabled()) {  
  20.             logger.debug("Initializing servlet '" + getServletName() + "'");  
  21.         }  
  22.   
  23.         // Set bean properties from init parameters.  
  24.         try {  
  25.             //获得web.xml中的contextConfigLocation配置属性,就是spring MVC的配置文件  
  26.             PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);  
  27.             BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);  
  28.             //获取服务器的各种信息  
  29.             ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());  
  30.             bw.registerCustomEditor(Resource.classnew ResourceEditor(resourceLoader, getEnvironment()));  
  31.             //模板方法,可以在子类中调用,做一些初始化工作,bw代表DispatcherServelt  
  32.             initBeanWrapper(bw);  
  33.             //将配置的初始化值设置到DispatcherServlet中  
  34.             bw.setPropertyValues(pvs, true);  
  35.         }  
  36.         catch (BeansException ex) {  
  37.             logger.error("Failed to set bean properties on servlet '" + getServletName() + "'", ex);  
  38.             throw ex;  
  39.         }  
  40.   
  41.         // Let subclasses do whatever initialization they like.  
  42.         //模板方法,子类初始化的入口方法  
  43.         initServletBean();  
  44.   
  45.         if (logger.isDebugEnabled()) {  
  46.             logger.debug("Servlet '" + getServletName() + "' configured successfully");  
  47.         }  
  48.     }  
  49.   
  50.     /** 
  51.      * Initialize the BeanWrapper for this HttpServletBean, 
  52.      * possibly with custom editors. 
  53.      * <p>This default implementation is empty. 
  54.      * @param bw the BeanWrapper to initialize 
  55.      * @throws BeansException if thrown by BeanWrapper methods 
  56.      * @see org.springframework.beans.BeanWrapper#registerCustomEditor 
  57.      */  
  58.     protected void initBeanWrapper(BeanWrapper bw) throws BeansException {  
  59.     }  
  60.       
  61.     //获取servletName  
  62.     @Override  
  63.     public final String getServletName() {  
  64.         return (getServletConfig() != null ? getServletConfig().getServletName() : null);  
  65.     }  
  66.       
  67.     //获取ServletContext  
  68.     @Override  
  69.     public final ServletContext getServletContext() {  
  70.         return (getServletConfig() != null ? getServletConfig().getServletContext() : null);  
  71.     }  
  72.   
  73.   
  74.       
  75.     protected void initServletBean() throws ServletException {  
  76.     }  
  77.   
  78.   
  79.     @Override  
  80.     public void setEnvironment(Environment environment) {  
  81.         Assert.isInstanceOf(ConfigurableEnvironment.class, environment);  
  82.         this.environment = (ConfigurableEnvironment) environment;  
  83.     }  
  84.   
  85.     @Override  
  86.     public ConfigurableEnvironment getEnvironment() {  
  87.         if (this.environment == null) {  
  88.             this.environment = this.createEnvironment();  
  89.         }  
  90.         return this.environment;  
  91.     }  
  92.   
  93.   
  94.     protected ConfigurableEnvironment createEnvironment() {  
  95.         return new StandardServletEnvironment();  
  96.     }  
  97.   
  98.     private static class ServletConfigPropertyValues extends MutablePropertyValues {  
  99.   
  100.         public ServletConfigPropertyValues(ServletConfig config, Set<String> requiredProperties)  
  101.             throws ServletException {  
  102.   
  103.             Set<String> missingProps = (requiredProperties != null && !requiredProperties.isEmpty()) ?  
  104.                     new HashSet<String>(requiredProperties) : null;  
  105.   
  106.             Enumeration<String> en = config.getInitParameterNames();  
  107.             while (en.hasMoreElements()) {  
  108.                 String property = en.nextElement();  
  109.                 Object value = config.getInitParameter(property);  
  110.                 addPropertyValue(new PropertyValue(property, value));  
  111.                 if (missingProps != null) {  
  112.                     missingProps.remove(property);  
  113.                 }  
  114.             }  
  115.             // Fail if we are still missing properties.  
  116.             if (missingProps != null && missingProps.size() > 0) {  
  117.                 throw new ServletException(  
  118.                     "Initialization from ServletConfig for servlet '" + config.getServletName() +  
  119.                     "' failed; the following required properties were missing: " +  
  120.                     StringUtils.collectionToDelimitedString(missingProps, ", "));  
  121.             }  
  122.         }  
  123.     }  
  124.   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值