Spring 具体分析 applicationContext.xml 和 spring3-servlet.xml
web.xml,其绝对路径为: \com.mimvp.homer\src\main\webapp\WEB-INF\web.xml
通过查看log4j的日志可以发现在服务器启动时 applicationContext.xml 是先于spring3-servlet.xml 加载
[org.springframework.web.context.ContextLoader] - Root WebApplicationContext: initialization started May 22, 2017 4:55:25 PM org.apache.catalina.core.ApplicationContext log 信息: Initializing Spring root WebApplicationContext [org.springframework.web.context.support.XmlWebApplicationContext] - Refreshing Root WebApplicationContext: startup date [Mon May 22 16:55:25 CST 2017]; root of context hierarchy [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from class path resource [spring-mybatis.xml] [org.springframework.beans.factory.config.PropertyPlaceholderConfigurer] - Loading properties file from class path resource [jdbc.properties] [org.springframework.web.context.ContextLoader] - Root WebApplicationContext: initialization completed in 5058 ms May 22, 2017 4:55:30 PM org.apache.catalina.core.ApplicationContext log 信息: Initializing Spring FrameworkServlet 'SpringMVC' [org.springframework.web.servlet.DispatcherServlet] - FrameworkServlet 'SpringMVC': initialization started [org.springframework.web.context.support.XmlWebApplicationContext] - Refreshing WebApplicationContext for namespace 'SpringMVC-servlet': startup date [Mon May 22 16:55:30 CST 2017]; parent: Root WebApplicationContext [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from class path resource [spring-mvc.xml] [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/user/showPwd] onto handler 'userController' [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/user/showPwd.*] onto handler 'userController' [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/user/showPwd/] onto handler 'userController' [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/user/showUser] onto handler 'userController' [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/user/showUser.*] onto handler 'userController' [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/user/showUser/] onto handler 'userController' [org.springframework.web.servlet.DispatcherServlet] - FrameworkServlet 'SpringMVC': initialization completed in 1913 ms May 22, 2017 4:55:32 PM org.apache.catalina.startup.HostConfig deployDescriptor 信息: Deployment of configuration descriptor E:\myPro\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\conf\Catalina\localhost\com.mimvp.homer.xml has finished in 13,551 ms May 22, 2017 4:55:32 PM org.apache.coyote.AbstractProtocol start 信息: Starting ProtocolHandler ["http-bio-8080"] May 22, 2017 4:55:32 PM org.apache.coyote.AbstractProtocol start 信息: Starting ProtocolHandler ["ajp-bio-8009"] May 22, 2017 4:55:32 PM org.apache.catalina.startup.Catalina start 信息: Server startup in 21873 ms [com.mimvp.homer.controller.UserController] - /showUser?id=1 [com.mimvp.homer.controller.UserController] - /showPwd?id=3
Spring MVC 加载顺序依次为:
ContextLoader —> XmlWebApplicationContext —> XmlBeanDefinitionReader —> PropertyPlaceholderConfigurer —> ContextLoader (完成)
DispatcherServlet —> XmlWebApplicationContext —> XmlBeanDefinitionReader —> DefaultAnnotationHandlerMapping —> DispatcherServlet (完成)
因此,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
ContextLoaderListener 类源码
package org.springframework.web.context; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class ContextLoaderListener extends ContextLoader implements ServletContextListener { public ContextLoaderListener() { } public ContextLoaderListener(WebApplicationContext context) { super(context); } /** * Initialize the root web application context. */ @Override public void contextInitialized(ServletContextEvent event) { initWebApplicationContext(event.getServletContext()); } /** * Close the root web application context. */ @Override public void contextDestroyed(ServletContextEvent event) { closeWebApplicationContext(event.getServletContext()); ContextCleanupListener.cleanupAttributes(event.getServletContext()); } }
ContextLoaderListener 有两个构造函数,继承于ContextLoader类,实现了ContextLoader接口及其两个方法。
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" 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"> <!-- 初始化Spring容器, 并加载Spring配置文件applicationContext.xml --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:com/mimvp/config/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 使用filter来统一字符编码 --> <filter> <filter-name>Set Character Encoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>Set Character Encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 加载DispatcherServlet并加载其的配置文件spring3-servlet.xml --> <servlet> <servlet-name>spring3</servlet-name> <!-- 默认找到的是spring3-servlet.xml --> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:com/mimvp/config/spring3-servlet.xml</param-value> </init-param> --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring3</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>spring3</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
启动加载文件 web.xml,会加载配置文件 applicationContext.xml 和 spring3-servlet.xml
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: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"> <!-- 注意上面的default-autowire="byName",如果没有这个声明那么HibernateDaoSupport中的sessionFactory不会被注入 --> <!-- 约定优于配置--> <!-- 自动扫描组件,包括@Controller,@Service,@Repository这里要把web下面的 controller去除,他们已经在spring3-servlet.xml中配置了,也就是已经加入beanFactory,否则会影响注入 --> <!-- 还发现了一个问题,就是如果不扫描@Repository,页面都进不去,报404,后来发现是这里的default-autowire和Javabean中@Autowire有冲突。。但最后还是要扫描@Repository,因为要给DAO注入sessionFactory --> <context:component-scan base-package="com.mimvp"> <context:exclude-filter type="regex" expression="com.mimvp.web.*" /> </context:component-scan> <!-- <context:component-scan base-package="com.mimvp.service.impl"></context:component-scan> <context:component-scan base-package="com.mimvp.dao.impl"></context:component-scan> --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://127.0.0.1:3306/ytcdb2?useUnicode=true&characterEncoding=UTF-8"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> <!-- 为了不用每次都去数据库,可以在这里配置连接池,当然,还要引入响应的jar包 --> <property name="maxActive" value="10"></property> <property name="maxWait" value="60000"></property> <property name="maxIdle" value="5"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 由于通过autowire启用了自动装载,所以下面的这个dataSource其实是可以不用写的 ,后面通过ref引用的都是一样的道理--> <property name="dataSource" ref="dataSource" /> <property name="mappingDirectoryLocations"> <list><!-- 这里直接映射的pojo类所在的包,简单方便不用每次加一个pojo类都需要到这里来添加 --> <value>classpath:com/mimvp/model</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.connection.autocommit">true</prop> </props> </property> </bean> <!-- 下面是配置声明式事务管理的,个人感觉比用注解管理事务要简单方便 --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- advisor包含一个切点和一个通知,而aspect包含多个 --> <aop:config> <aop:advisor pointcut="execution(* com.mimvp.service.*Service.*(..))" advice-ref="txAdvice" /> </aop:config> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <!-- Service类中以get*,query*等等开头的查询操作只能执行只读事务;对于所有的事务,如果出现异常就回滚 --> <tx:method name="get*" read-only="true" /> <tx:method name="query*" read-only="true" /> <tx:method name="find*" read-only="true" /> <tx:method name="load*" read-only="true" /> <tx:method name="*" rollback-for="Exception" /> </tx:attributes> </tx:advice> </beans>
spring3-servlet.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: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",约定优于配置 --> <!--mvc:annotation-driven注册了 RequestMappingHandlerMapping, a RequestMappingHandlerAdapter,and an ExceptionHandlerExceptionResolver 能启动@RequestMapping,@ExceptionHandler, and others注释 --> <mvc:annotation-driven /> <!-- 把无法mapping到Controller的path交给default servlet handler处理 --> <mvc:default-servlet-handler /> <!-- 配置静态资源,直接映射到对应的文件夹,不被DispatcherServlet处理而由前面的default-servlet-handler处理,3.04新增功能,需要重新设置spring-mvc-3.0.xsd --> <mvc:resources mapping="/img/**" location="/img/" /> <mvc:resources mapping="/js/**" location="/js/" /> <mvc:resources mapping="/css/**" location="/css/" /> <mvc:resources mapping="/html/**" location="/html/" /> <!-- ①:对web包中的Controller类进行扫描,以完成Bean创建和自动依赖注入的功能 --> <!-- 也就是说这里相当于实例化了Controller类并且给它注入了Service的实例?它没有扫描Service的包诶。。 --> <!-- 因为applicationContext.xml先于本文件加载,所以那边先进行扫描,其实是注入了的,byName通过@Service(...)里面的值进行匹配 --> <context:component-scan base-package="com.mimvp.web" /> <!--- StringHttpMessageConverter bean --> <!-- 下面这两个处理器映射默认跟<mvc:annotation-driven />功能几乎是一样的,都能够启用SpringMVC的注解功能,只是如果要添加一些其它功能的时候就进行配置了 --> <!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射,添加拦截器,类级别的处理器映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> <property name="interceptors"> <list> <!-- 用于检查session是否存在 --> <bean class="com.mimvp.util.MyHandlerInterceptor" /> </list> </property> </bean> <!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射,对有RequestMapping注解的控制器进行HTTP路径、HTTP方法和请求参数解析, 这里 配置了一个基于注解的定制的WebBindingInitializer,解决日期转换问题,方法级别的处理器映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="cacheSeconds" value="0" /> <!-- 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缓存 --> <property name="webBindingInitializer"> <bean class="com.mimvp.util.MyWebBinding" /> </property> <!-- 配置一下对json数据的转换 --> <property name="messageConverters"> <list> <!-- 对@ResponseBody注释的方法生效 --> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean> <bean class="org.springframework.http.converter.StringHttpMessageConverter" /> </list> </property> </bean> <!-- 配置对JSP文件的视图解析器 --> <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath" value="/WEB-INF/view/" /> <property name="freemarkerSettings"> <props> <prop key="template_update_delay">0</prop> <prop key="default_encoding">UTF-8</prop> <prop key="number_format">0.##########</prop> <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop> <prop key="classic_compatible">true</prop> <prop key="template_exception_handler">ignore</prop> </props> </property> </bean> <bean id="viewResolverJsp" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/" /> <property name="suffix" value=".jsp" /> <property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceView" /> <property name="order" value="1" /> </bean> <!-- 配置对HTML文件的视图解析器 --> <!-- 设置freeMarker的配置文件路径 --> <bean id="freemarkerConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="exposeRequestAttributes" value="true" /> <property name="exposeSessionAttributes" value="true" /> <property name="viewClass"> <value>org.springframework.web.servlet.view.freemarker.FreeMarkerView </value> </property> <property name="cache"> <value>true</value> </property> <!-- <property name="allowSessionOverride" value="true" /> --> <property name="suffix"> <value>.html</value> </property> <!-- <property name="viewNames" value="*.html,*.jsp" /> --> <property name="contentType"> <value>text/html; charset=UTF-8</value> </property> <property name="order" value="0" /> </bean> </beans>
参考推荐:
Java中Dao层、Action层、Service层、Controller层
版权所有: 本文系米扑博客原创、转载、摘录,或修订后发表,最后更新于 2017-05-22 17:10:04
侵权处理: 本个人博客,不盈利,若侵犯了您的作品权,请联系博主删除,莫恶意,索钱财,感谢!
转载注明: Spring 具体分析 applicationContext.xml 和 spring3-servlet.xml (米扑博客)