热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

springweb.xml难点配置总结

springweb.xml难点配置总结【转】web.xmlweb.xml是所有web项目的根源,没有它,任何web项目都启动不了,所以有必要了解相关的配置.

spring web.xml 难点配置总结【转】

web.xml

web.xml是所有web项目的根源,没有它,任何web项目都启动不了,所以有必要了解相关的配置.

 

ContextLoderListener,ContextLoaderServlet,DispatcherServlet 区别

本段引用自 : http://blog.csdn.net/feiyu8607/article/details/6532397

web.xml中可以有三种方式来配置xml去加载Bean:

org.springframework.web.context.ContextLoaderListener
org.springframework.web.context.ContextLoaderServlet
org.springframework.web.servlet.DispatcherServlet

  1. ContextLoaderListener 和 ContextLoaderServlet : 本质上是等同的,都是调用ContextLoader来加载web程序的上下文,加载完成以后,都是在ServletContext中,只不过listener需要Servlet2.3及以上支持。
  2. ContextLoaderListene与DispatcherServlet : 用DispatcherServlet载入的Bean是隶属于此Servlet的(所以spring可以配置多个分别拥有各自环境的DispatcherServlet),因此其他servlet无法获取到该Context。这一现象在buffalo配置时曾经出现(无法找到服务bean)。分析了buffalo和spring的源码后,将xml在ContextLoaderListener配置才得以解决。   所以web.xml文件中若只使用了一个dispatcherservlet来进行分发,则使用dispatcherservlet contextloaderlistener 来加载bean实例是等效的。

 各元素初始化过程

本段引用自: http://blog.csdn.net/fupengyao/article/details/50605954

初始化过程:

  1. 在启动Web项目时,容器(比如Tomcat)会读web.xml配置文件中的两个节点
  2. 接着容器会创建一个ServletContext(上下文),应用范围内即整个WEB项目都能使用这个上下文。
  3. 接着容器会将读取到转化为键值对,并交给ServletContext。
  4. 容器创建中的类实例,即创建监听(备注:listener定义的类可以是自定义的类但必须需要继承ServletContextListener)。
  5. 在监听的类中会有一个contextInitialized(ServletContextEvent event)初始化方法,在这个方法中可以通过event.getServletContext().getInitParameter("contextConfigLocation") 来得到context-param 设定的值。在这个类中还必须有一个contextDestroyed(ServletContextEvent event) 销毁方法.用于关闭应用前释放资源,比如说数据库连接的关闭。
  6. 得到这个context-param的值之后,你就可以做一些操作了.注意,这个时候你的WEB项目还没有完全启动完成.这个动作会比所有的Servlet都要早。

所以 web.xml的加载过程是context-param >> listener  >> fileter  >> servlet

 

 

context-param和init-param区别

web.xml里面可以定义两种参数:
(1)application范围内的参数,存放在servletcontext中,可以在servlet中通过getServletContext().getInitParameter("fruitName");

在web.xml中配置如下:

<context-param>  
           <param-name>fruitNameparam-name>  
           <param-value>orangeparam-value>  
context-param>

(2)servlet范围内的参数,只能在servlet的init()方法中通过this.getInitParameter("fruitName")取得.

在web.xml中配置如下:

<servlet>  
    <servlet-name>PersonServletservlet-name>  
    <servlet-class>com.king.servlet.PersonServletservlet-class>  
    <init-param>  
       <param-name>fruitNameparam-name>  
       <param-value>watermelonparam-value>  
    init-param>  
    <load-on-startup>0load-on-startup>  
servlet>  

 

 

 

ContextLoderListener配置

  1. contextConfigLocation为固定写法.
  2. 中可以通过classpath或/WEB-INF 两种路径来加载xml文件.

    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>
              
            classpath:config/applicationContext.xml
            
        param-value>
    context-param>

    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>

 

filter配置

说明都在注释中

 
     <filter>
        <filter-name>SpringCharacterEncodingFilterfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>
        <init-param>
            <param-name>forceEncodingparam-name>
            <param-value>trueparam-value>
        init-param>
    filter> 
    
    <filter-mapping>
        <filter-name>SpringCharacterEncodingFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

 

 

servlet配置

说明都在注释中

    <servlet>
        
        <servlet-name>springServletservlet-name>
        
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            
            
            <param-value>classpath:config/spring/springMVC.xmlparam-value>
        init-param>
         <load-on-startup>1load-on-startup> 
        
    servlet>
    <servlet-mapping>
        <servlet-name>springServletservlet-name>
        <url-pattern>*.dourl-pattern>
    servlet-mapping>

 

注解,定时任务等xml配置在哪加载合适?

如果您把上面的段落都仔细阅读完了,会发现配置如果没有手动配置,那么默认是servlet第一次被访问到才会去初始化的,如果该servlet等web应用启动后,过了很久都没有被访问,那么注释和定时任务都是不会启动的.

而且我们应当小心listener和servlet重复加载注解引起的启动时间浪费  及  重复加载定时任务引起的数据冲突或不一致.

所以注解,定时任务都建议放在全局监听的中,而不建议放在中.

 

 

BeanFactory获取

在EE web应用的servlet或controller中,可通过WebApplicationContextUtils来获取BeanFactory

BeanFactory factory = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
UserManager userManager = (UserManager)factory.getBean("userManager");

在SE 标准应用中可直接通过java代码来获取BeanFactory

BeanFactory factory = new ClassPathXmlApplictionContext("applicationContext.xml");

 

真实环境web.xml,applicationContext.xml,springMVC.xml

web.xml

xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">


    <display-name>SpringMVCdisplay-name>
    
    <welcome-file-list>
        <welcome-file>index.htmlwelcome-file>
        <welcome-file>index.jspwelcome-file>
    welcome-file-list>

    <error-page>
        <error-code>500error-code>
   
        <location>/WEB-INF/jsp/common/errorPage.jsplocation>
    error-page>

    
    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>
              
            classpath:config/applicationContext.xml
            
        param-value>
    context-param>

    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>
    
    
     <servlet> 
        <servlet-name>log4jInitservlet-name> 
        <servlet-class>config.log.Log4jInitservlet-class> 
        <init-param> 
            <param-name>log4j-config-fileparam-name> 
            <param-value>/WEB-INF/classes/config/log/log4j.propertiesparam-value> 
        init-param> 
        <load-on-startup>1load-on-startup> 
    servlet> 
    
    
         
     <filter>
        <filter-name>SpringCharacterEncodingFilterfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>
        <init-param>
            <param-name>forceEncodingparam-name>
            <param-value>trueparam-value>
        init-param>
    filter> 
    
    <filter-mapping>
        <filter-name>SpringCharacterEncodingFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>
    
    <servlet>
        
        <servlet-name>springServletservlet-name>
        
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            
            
            <param-value>classpath:config/spring/springMVC.xmlparam-value>
        init-param>
         <load-on-startup>1load-on-startup> 
        
    servlet>
    <servlet-mapping>
        <servlet-name>springServletservlet-name>
        <url-pattern>*.dourl-pattern>
    servlet-mapping>
    
    
    <session-config>
        <session-timeout>60session-timeout>
    session-config>
web-app>

 

applicationContext.xml

xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:util="http://www.springframework.org/schema/util" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
                           >
    
    
    
    <context:annotation-config />
    
     
    <context:component-scan base-package="com.bobo.code" />
    
    

    <context:property-placeholder location="classpath:/config/database/oracle_jdbc_virtual_tele.properties" /> 
    
    
    
    
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${dataSource.driverClassName}" />
        <property name="url" value="${dataSource.url}" />
        <property name="username" value="${dataSource.username}" />
        <property name="password" value="${dataSource.password}" />
    bean> 

    
     <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">  
          <property name="configLocation">
            <value>classpath:config/mybatis/sqlmap-config.xmlvalue>
        property>  
        <property name="dataSource" ref="dataSource" />  
    bean>

    
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    bean>

    
    
    
    
    
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED" />
        tx:attributes>
    tx:advice>

  
  
    
    <aop:config>
        <aop:pointcut                  id="allManagerMethod"     expression="execution(* com.bobo.code.service.impl.*.*(..))" />
        <aop:advisor  pointcut-ref="allManagerMethod" advice-ref = "txAdvice"/>
    aop:config>


    












    
    
    <import resource="classpath*:config/spring/timetrigger.xml"/>
beans>

 

springMVC.xml

xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:util="http://www.springframework.org/schema/util"  
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

    
    
    
     
    
    
    
    <context:component-scan base-package="com.bobo.code" />

       
     <tx:annotation-driven  transaction-manager="transactionManager" />
    
    
    <bean    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <property name="interceptors">
            <list>
                
                
                
            list>
        property>
    bean>
    
    
    <bean 
            class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="webBindingInitializer">
            <bean
                class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
                <property name="conversionService">
                    <bean
                        class="org.springframework.format.support.FormattingConversionServiceFactoryBean">bean>
                property>
            bean>
        property>
    bean>

    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/" /> 
        <property name="suffix" value="" />
        
        
        <property name="viewClass"    value="org.springframework.web.servlet.view.InternalResourceView" />
    bean>
    
      
  <bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">  
    <property name="exceptionMappings">  
      <props>  
        <prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">jsp/common/exceptionprop>  
      props>  
    property>  
  bean>  
    
beans>

推荐阅读
  • 在重复造轮子的情况下用ProxyServlet反向代理来减少工作量
    像不少公司内部不同团队都会自己研发自己工具产品,当各个产品逐渐成熟,到达了一定的发展瓶颈,同时每个产品都有着自己的入口,用户 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • web.py开发web 第八章 Formalchemy 服务端验证方法
    本文介绍了在web.py开发中使用Formalchemy进行服务端表单数据验证的方法。以User表单为例,详细说明了对各字段的验证要求,包括必填、长度限制、唯一性等。同时介绍了如何自定义验证方法来实现验证唯一性和两个密码是否相等的功能。该文提供了相关代码示例。 ... [详细]
  • 解决java.lang.IllegalStateException: ApplicationEventMulticaster not initialized错误的方法和原因
    本文介绍了解决java.lang.IllegalStateException: ApplicationEventMulticaster not initialized错误的方法和原因。其中包括修改包名、解决service name重复、处理jar包冲突和添加maven依赖等解决方案。同时推荐了一个人工智能学习网站,该网站内容通俗易懂,风趣幽默,值得一看。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 本文讨论了在Spring 3.1中,数据源未能自动连接到@Configuration类的错误原因,并提供了解决方法。作者发现了错误的原因,并在代码中手动定义了PersistenceAnnotationBeanPostProcessor。作者删除了该定义后,问题得到解决。此外,作者还指出了默认的PersistenceAnnotationBeanPostProcessor的注册方式,并提供了自定义该bean定义的方法。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • r2dbc配置多数据源
    R2dbc配置多数据源问题根据官网配置r2dbc连接mysql多数据源所遇到的问题pom配置可以参考官网,不过我这样配置会报错我并没有这样配置将以下内容添加到pom.xml文件d ... [详细]
  • Imtryingtofigureoutawaytogeneratetorrentfilesfromabucket,usingtheAWSSDKforGo.我正 ... [详细]
  • Spring学习(4):Spring管理对象之间的关联关系
    本文是关于Spring学习的第四篇文章,讲述了Spring框架中管理对象之间的关联关系。文章介绍了MessageService类和MessagePrinter类的实现,并解释了它们之间的关联关系。通过学习本文,读者可以了解Spring框架中对象之间的关联关系的概念和实现方式。 ... [详细]
  • Spring常用注解(绝对经典),全靠这份Java知识点PDF大全
    本文介绍了Spring常用注解和注入bean的注解,包括@Bean、@Autowired、@Inject等,同时提供了一个Java知识点PDF大全的资源链接。其中详细介绍了ColorFactoryBean的使用,以及@Autowired和@Inject的区别和用法。此外,还提到了@Required属性的配置和使用。 ... [详细]
  • GreenDAO快速入门
    前言之前在自己做项目的时候,用到了GreenDAO数据库,其实对于数据库辅助工具库从OrmLite,到litePal再到GreenDAO,总是在不停的切换,但是没有真正去了解他们的 ... [详细]
  • SpringBoot整合SpringSecurity+JWT实现单点登录
    SpringBoot整合SpringSecurity+JWT实现单点登录,Go语言社区,Golang程序员人脉社 ... [详细]
  • Asp.net Mvc Framework 七 (Filter及其执行顺序) 的应用示例
    本文介绍了在Asp.net Mvc中应用Filter功能进行登录判断、用户权限控制、输出缓存、防盗链、防蜘蛛、本地化设置等操作的示例,并解释了Filter的执行顺序。通过示例代码,详细说明了如何使用Filter来实现这些功能。 ... [详细]
author-avatar
吴淑舜415
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有