- 首先看一下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
<?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,默认在Webroot/WEB-INF目录下面,可以通过<context-param>的contextConfigLocation属性指定路径-->
<!-- <context-param> <param-name>contextConfigLocation</param-name> <param- value>classpath:com/lince/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,默认也是在WEB-INF目录下 -->
<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/lince/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> --> <!-- 通过这个过滤,可以在webroot下直接访问html -->
<welcome-file-list>
<!-- <welcome-file>login.html</welcome-file> -->
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</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: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.lince">
<context:exclude-filter type="regex" expression="com.lince.web.*" />
</context:component-scan>
<!-- <context:component-scan base-package="com.lince.service.impl"> </context:component-scan> <context:component-scan base-package="com.lince.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/lince/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>
<aop:config>
<!-- advisor包含一个切点和一个通知,而aspect包含多个 -->
<aop:advisor pointcut="execution(* com.lince.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.lince.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.lince.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.lince.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>
原文地址:https://www.jb51.cc/xml/294872.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。