1.web.xml web工程的配置文件。
web.xml的命名空间
<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="MyWebApp" version="2.5">
首先配置监听器以装载applicationContext.xml配置文件。
例:
<!--Spring的ApplicationContext 载入 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- web容器启动时,按照如下的路径载入applicationContext.xml配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext*.xml</param-value>
</context-param>
然后可以配置编码过滤器:
<!-- 编码过滤器,以UTF8编码
很简单很实用的一个过滤器,当前台JSP页面和JAVA代码中使用了不同的字符集进行编码的时候就会出现表单提交的数据或者上传/下载中文名称文件出现乱码
的问题,那这个类就可以出场了。
CharacterEncodingFilter类具有encoding和forceEncoding两个属性,其中encoding是表示设置request的编码,
forceEncoding表示是否同时设置response的编码。
-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
然后时配置SpringMVC的DispatcherServlet:
<!-- 配置DispatcherServlet -->
<servlet>
<servlet-name>manage</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 自定义SpringMVC配置文件路径 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springMVCConfig.xml</param-value>
</init-param>
<!-- 随容器自动启动完成初始化 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>manage</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
web.xml完成。
2.diapatcher-servlet.xml SpringMVC配置文件
dispatcher-servlet.xml的命名空间是:
<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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
首先,我们使用<context:component-scan>查找使用构造型(stereotype)注解所标注的类,如@Component(组件),@Service(服务),@Controller(控制器),@Repository(数据仓库)。告诉dispatcherServlet,controller类在哪个包下。
<!-- 注解驱动 -->
<mvc:annotation-driven />
<!-- 定义Controller的扫描包 -->
<context:component-scan base-package="mms.controller" />
<context:annotation-config>
我们一般在含有Spring的项目中,可能会看到配置项中包含这个配置节点<context:annotation-config>,这是一条向Spring容器中注册
AutowiredAnnotationBeanPostProcessor
CommonAnnotationBeanPostProcessor
PersistenceAnnotationBeanPostProcessor
RequiredAnnotationBeanPostProcessor
这4个BeanPostProcessor.注册这4个BeanPostProcessor的作用,就是为了你的系统能够识别相应的注解。
那么那些注释依赖这些Bean呢。
如果想使用@ Resource 、@ PostConstruct、@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor。
如果想使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessor的Bean。
如果想使用@Autowired注解,那么就必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。
如果想使用 @Required的注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean。
这里来说说配置开启的两个常用的组件,RequestMappingHandlerMapping和RequestMappingHandlerAdapter。
RequestMappingHandlerMapping是HandlerMapping的实现类,它会在容器启动的时候,扫描容器内的bean,解析带有@RequestMapping
注解的方法,并将其解析为url和handlerMethod键值对方式注册到请求映射表中。
RequestMappingHandlerAdapter是HandlerAdapter的实现类,它是处理请求的适配器,说白了,就是确定调用哪个类的哪个方法,并且构造方法参数,返回值。
像其实<context:component-scan/>标签是告诉Spring容器来扫描指定包下的类,并注册被@Component,@Controller,@Service,@Repository等注解标记的组件相似。
<mvc:annotation-driven/>是告知Spring容器,我们启用注解驱动,支持@RequestMapping注解,这样我们就可以使用@RequestMapping来配置处理器
接着可以配置视图解析器:
<!-- 定义视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="" />
<property name="suffix" value=".html" />
</bean>
如果设置了拦截器则需要在这里配置拦截器:避免一些静态资源被拦截的问题
<!-- 处理静态资源被“/”所拦截的问题 -->
<mvc:default-servlet-handler />
<!-- 定义拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<!-- 所有的请求都进入 -->
<mvc:mapping path="/**"/>
<!-- 不拦截登陆页面 -->
<mvc:exclude-mapping path="/login.html" />
<mvc:exclude-mapping path="/mms/css/*" />
<mvc:exclude-mapping path="/mms/js/**" />
<mvc:exclude-mapping path="/mms/images/*" />
<!-- 不拦截处理登陆的业务 -->
<mvc:exclude-mapping path="/Login/loginUser" />
<bean class="mms.interceptors.MyHandlerInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
3.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
首先可以配置propertiesConfigurer,使用PropertyPlaceholderConfigurer可以在XML配置文件中加入外部属性文件,当然也可以指定外部文件的编码,这样以后,在配置文件的上下文中就可以以${xxx}的形式进行引用如:
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- 允许JVM参数覆盖 -->
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<!-- 忽略没有找到的资源文件 -->
<property name="ignoreResourceNotFound" value="true" />
<!-- 配置资源文件 -->
<property name="locations">
<!-- 配置jdbc配置文件 -->
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
在<list>中也可以同时引入多个属性文件。
接着配置的是<context:component-scan basepackage="xxx">这个元素的意思是,扫描这个包,把这个包里和Spring相关的注解的类,注册为Spring的Bean。
<!-- 自动注入mms中的bean -->
<context:component-scan base-package="mms" />
接下来可以配置DataSource连接池。连接池这边则使用了开始引入的属性文件jdbc.properties。
<!-- 注入连接池 -->
<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"
destroy-method="close">
<!-- 数据库驱动 -->
<property name="driverClass" value="${jdbc.driver}" />
<!-- 相应驱动的jdbcUrl -->
<property name="jdbcUrl" value="${jdbc.url}" />
<!-- 数据库的用户名 -->
<property name="username" value="${jdbc.username}" />
<!-- 数据库的密码 -->
<property name="password" value="${jdbc.password}" />
<!-- 检查数据库连接池中空闲连接的间隔时间,单位是分,默认值:240,如果要取消则设置为0 -->
<property name="idleConnectionTestPeriod" value="60" />
<!-- 连接池中未使用的链接最大存活时间,单位是分,默认值:60,如果要永远存活设置为0 -->
<property name="idleMaxAge" value="30" />
<!-- 每个分区最大的连接数 -->
<property name="maxConnectionsPerPartition" value="150" />
<!-- 每个分区最小的连接数 -->
<property name="minConnectionsPerPartition" value="5" />
</bean>
DataSource连接池是为产生一个SqlSeessionFactory Bean。因此接着配置SqlSessionFactory,把这个DataSource注入到里面去。
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 定义数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- 指定mybatis全局配置文件 -->
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
<!-- 扫描mappers目录以及子目录下的所有xml文件 -->
<property name="mapperLocations" value="classpath:mybatis/mappers/**/*.xml" />
<!-- 别名扫描包 -->
<property name="typeAliasesPackage" value="mms.pojo"/>
</bean>
然后可以配置事务
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
最后配置最重要的mapper,用自动扫描方式
<!-- 定义Mapper接口扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mms.mapper" />
</bean>
4.mybatis-config.xml
在这个配置文件中,由于在applicationContext.xml文件中已经配置完,而且mapper的映射路径也指定完毕,故在此文件中不再需要配置这两样。
在此配置文件中,可以配置<settings/>和<typeAliases/>元素。