具体分析applicationContext.xml和spring3-servlet.xml

前端之家收集整理的这篇文章主要介绍了具体分析applicationContext.xml和spring3-servlet.xml前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  • 首先看一下web.xml,通过查看log4j的日志可以发现在服务器启动时applicationContext.xml是先于spring3-servlet.xml加载,因此applicationContext.xml中已经注册beanfactory的bean在spring3-servlet.xml也是可以使用的,比如在applicationContext.xml中通过context:component-scan扫描了service和dao包,而在spring3-servlet.xml只扫描了web包,但web包中的Controller依然被注入了Service。
  • web.xml各个标签的加载顺序:context-param -> listener -> filter -> servlet
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  3. <!-- 初始化Spring容器并加载Spring配置文件applicationContext.xml,默认在WebRoot/WEB-INF目录下面,可以通过<context-param>的contextConfigLocation属性指定路径-->
  4. <!-- <context-param> <param-name>contextConfigLocation</param-name> <param- value>classpath:com/lince/config/applicationContext.xml</param-value> </context-param> -->
  5. <listener>
  6. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  7. </listener>
  8. <!-- 使用filter来统一字符编码 -->
  9. <filter>
  10. <filter-name>Set Character Encoding</filter-name>
  11. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  12. <init-param>
  13. <param-name>encoding</param-name>
  14. <param-value>UTF-8</param-value>
  15. </init-param>
  16. <init-param>
  17. <param-name>forceEncoding</param-name>
  18. <param-value>true</param-value>
  19. </init-param>
  20. </filter>
  21. <filter-mapping>
  22. <filter-name>Set Character Encoding</filter-name>
  23. <url-pattern>/*</url-pattern>
  24. </filter-mapping>
  25.  
  26. <!-- 加载DispatcherServlet并加载其的配置文件spring3-servlet.xml,默认也是在WEB-INF目录下 -->
  27. <servlet>
  28. <servlet-name>spring3</servlet-name> <!-- 默认找到的是spring3-servlet.xml -->
  29. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  30. <!-- <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:com/lince/config/spring3-servlet.xml</param-value> </init-param> -->
  31. <load-on-startup>1</load-on-startup>
  32. </servlet>
  33.  
  34. <servlet-mapping>
  35. <servlet-name>spring3</servlet-name>
  36. <url-pattern>/</url-pattern>
  37. </servlet-mapping>
  38. <!-- <servlet-mapping> <servlet-name>spring3</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> --> <!-- 通过这个过滤,可以在webroot下直接访问html -->
  39.  
  40. <welcome-file-list>
  41. <!-- <welcome-file>login.html</welcome-file> -->
  42. <welcome-file>index.jsp</welcome-file>
  43. </welcome-file-list>
  44. </web-app>
  • applicationContext.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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: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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName">
  3. <!-- 注意上面的default-autowire="byName",如果没有这个声明那么HibernateDaoSupport中的sessionFactory不会被注入 -->
  4. <!-- 约定优于配置-->
  5.  
  6. <!-- 自动扫描组件,包括@Controller,@Service,@Repository这里要把web下面的 controller去除,他们已经在spring3-servlet.xml中配置了,也就是已经加入beanfactory,否则会影响注入 -->
  7. <!-- 还发现了一个问题,就是如果不扫描@Repository,页面都进不去,报404,后来发现是这里的default-autowire和Javabean中@Autowire有冲突。。但最后还是要扫描@Repository,因为要给DAO注入sessionFactory -->
  8. <context:component-scan base-package="com.lince">
  9. <context:exclude-filter type="regex" expression="com.lince.web.*" />
  10. </context:component-scan>
  11. <!-- <context:component-scan base-package="com.lince.service.impl"> </context:component-scan> <context:component-scan base-package="com.lince.dao.impl"> </context:component-scan> -->
  12. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
  13. <property name="driverClassName" value="com.MysqL.jdbc.Driver"></property>
  14. <property name="url" value="jdbc:MysqL://127.0.0.1:3306/ytcdb2?useUnicode=true&amp;characterEncoding=UTF-8"></property>
  15. <property name="username" value="root"></property>
  16. <property name="password" value="root"></property>
  17. <!-- 为了不用每次都去数据库,可以在这里配置连接池,当然,还要引入响应的jar包 -->
  18. <!-- <property name="maxActive" value="10"></property> <property name="maxWait" value="60000"></property> <property name="maxIdle" value="5"></property> -->
  19. </bean>
  20.  
  21. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionfactorybean">
  22. <!-- 由于通过autowire启用了自动装载,所以下面的这个dataSource其实是可以不用写的 ,后面通过ref引用的都是一样的道理-->
  23. <property name="dataSource" ref="dataSource" />
  24. <property name="mappingDirectoryLocations">
  25. <list><!-- 这里直接映射的pojo类所在的包,简单方便不用每次加一个pojo类都需要到这里来添加 -->
  26. <value>classpath:com/lince/model</value>
  27. </list>
  28. </property>
  29. <property name="hibernateProperties">
  30. <props>
  31. <prop key="hibernate.dialect">
  32. org.hibernate.dialect.MysqLDialect
  33. </prop>
  34. <prop key="hibernate.show_sql">
  35. true
  36. </prop>
  37. <!-- <prop key="hibernate.connection.autocommit">true</prop> -->
  38. </props>
  39. </property>
  40. </bean>
  41.  
  42. <!-- 下面是配置声明式事务管理的,个人感觉比用注解管理事务要简单方便 -->
  43. <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  44. <property name="sessionFactory" ref="sessionFactory"></property>
  45. </bean>
  46.  
  47. <aop:config>
  48. <!-- advisor包含一个切点和一个通知,而aspect包含多个 -->
  49. <aop:advisor pointcut="execution(* com.lince.service.*Service.*(..))" advice-ref="txAdvice" />
  50. </aop:config>
  51.  
  52. <tx:advice id="txAdvice" transaction-manager="txManager">
  53. <tx:attributes>
  54. <!-- Service类中以get*,query*等等开头的查询操作只能执行只读事务;对于所有的事务,如果出现异常就回滚 -->
  55. <tx:method name="get*" read-only="true" />
  56. <tx:method name="query*" read-only="true" />
  57. <tx:method name="find*" read-only="true" />
  58. <tx:method name="load*" read-only="true" />
  59. <tx:method name="*" rollback-for="Exception" />
  60. </tx:attributes>
  61. </tx:advice>
  62.  
  63.  
  64. </beans>
  • spring3-servlet.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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" default-autowire="byName"> <!-- default-autowire="byName",约定优于配置 -->
  3. <!--mvc:annotation-driven注册了 RequestMappingHandlerMapping,a RequestMappingHandlerAdapter,and an ExceptionHandlerExceptionResolver 能启动@RequestMapping,@ExceptionHandler,and others注释 -->
  4. <mvc:annotation-driven />
  5. <!-- 把无法mapping到Controller的path交给default servlet handler处理 -->
  6. <mvc:default-servlet-handler />
  7. <!-- 配置静态资源,直接映射到对应的文件夹,不被DispatcherServlet处理而由前面的default-servlet-handler处理,3.04新增功能,需要重新设置spring-mvc-3.0.xsd -->
  8. <mvc:resources mapping="/img/**" location="/img/" />
  9. <mvc:resources mapping="/js/**" location="/js/" />
  10. <mvc:resources mapping="/css/**" location="/css/" />
  11. <mvc:resources mapping="/html/**" location="/html/" />
  12.  
  13. <!-- ①:对web包中的Controller类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
  14. <!-- 也就是说这里相当于实例化了Controller类并且给它注入了Service的实例?它没有扫描Service的包诶。。 -->
  15. <!-- 因为applicationContext.xml先于本文件加载,所以那边先进行扫描,其实是注入了的,byName通过@Service(...)里面的值进行匹配 -->
  16. <context:component-scan base-package="com.lince.web" />
  17.  
  18. <!--- StringHttpMessageConverter bean -->
  19.  
  20. <!-- 下面这两个处理器映射默认跟<mvc:annotation-driven />功能几乎是一样的,都能够启用SpringMVC的注解功能,只是如果要添加一些其它功能的时候就进行配置了 -->
  21. <!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射,添加拦截器,类级别的处理器映射 -->
  22. <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
  23. <property name="interceptors">
  24. <list>
  25. <!-- 用于检查session是否存在 -->
  26. <bean class="com.lince.util.MyHandlerInterceptor" />
  27. </list>
  28. </property>
  29. </bean>
  30.  
  31. <!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射,对有RequestMapping注解的控制器进行HTTP路径、HTTP方法和请求参数解析, 这里 配置了一个基于注解的定制的WebBindingInitializer,解决日期转换问题,方法级别的处理器映射 -->
  32. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
  33. <property name="cacheSeconds" value="0" />
  34. <!-- cacheSeconds =0时,则将设置如下响应头数据: Pragma:no-cache HTTP 1.0的不缓存响应头 Expires:1L useExpiresHeader=true时,HTTP 1.0 Cache-Control :no-cache useCacheControlHeader=true时,HTTP 1.1 Cache-Control :no-store useCacheControlNoStore=true时,该设置是防止Firefox缓存 -->
  35. <property name="webBindingInitializer">
  36. <bean class="com.lince.util.MyWebBinding" />
  37. </property>
  38. <!-- 配置一下对json数据的转换 -->
  39. <property name="messageConverters">
  40. <list>
  41. <!-- 对@ResponseBody注释的方法生效 -->
  42. <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
  43. <bean class="org.springframework.http.converter.StringHttpMessageConverter" />
  44. </list>
  45. </property>
  46. </bean>
  47.  
  48. <!-- 配置对JSP文件的视图解析器 -->
  49. <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
  50. <property name="templateLoaderPath" value="/WEB-INF/view/" />
  51. <property name="freemarkerSettings">
  52. <props>
  53. <prop key="template_update_delay">0</prop>
  54. <prop key="default_encoding">UTF-8</prop>
  55. <prop key="number_format">0.##########</prop>
  56. <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
  57. <prop key="classic_compatible">true</prop>
  58. <prop key="template_exception_handler">ignore</prop>
  59. </props>
  60. </property>
  61. </bean>
  62.  
  63. <bean id="viewResolverJsp" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  64. <property name="prefix" value="/WEB-INF/view/" />
  65. <property name="suffix" value=".jsp" />
  66. <property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceView" />
  67. <property name="order" value="1" />
  68. </bean>
  69. <!-- 配置对HTML文件的视图解析器 -->
  70. <!-- 设置freeMarker的配置文件路径 -->
  71. <bean id="freemarkerConfiguration" class="org.springframework.beans.factory.config.Propertiesfactorybean">
  72. </bean>
  73.  
  74. <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
  75. <property name="exposeRequestAttributes" value="true" />
  76. <property name="exposeSessionAttributes" value="true" />
  77. <property name="viewClass">
  78. <value>org.springframework.web.servlet.view.freemarker.FreeMarkerView
  79. </value>
  80. </property>
  81. <property name="cache">
  82. <value>true</value>
  83. </property>
  84. <!-- <property name="allowSessionOverride" value="true" /> -->
  85. <property name="suffix">
  86. <value>.html</value>
  87. </property>
  88. <!-- <property name="viewNames" value="*.html,*.jsp" /> -->
  89. <property name="contentType">
  90. <value>text/html; charset=UTF-8</value>
  91. </property>
  92. <property name="order" value="0" />
  93. </bean>
  94.  
  95.  
  96. </beans>

猜你在找的XML相关文章