SSM+maven项目配置文件详解(一)

来源:互联网 发布:sql二进制数据 计算符 编辑:程序博客网 时间:2024/06/05 02:37

2015年7月份毕业工作以来,陆陆续续也做了很多项目,基本上都是SSM框架,即:Spring+SpringMVC+MyBatis,项目是Maven构建,每次接手新的项目或者平时项目出现问题的时候,大致统计了下,主要问题一般都出现在SSM项目的配置文件上面,虽然网上也有各个配置文件的详细介绍,但整体感觉都比较零碎,所以我想系统的介绍下整个SSM配置文件的作用和加载过程.


一,闲话少叙,先看下SSM项目的配置文件框架图如下:



二,配置文件整体上可以分为五大部分

1,conf包下的各种给参数赋值的文件

2,maven的配置文件pom.xml文件

3,spring mvc的web.xml的配置文件

4,spring包下各种配置文件

5,log4j等日志的配置文件

这五大部分的之间的调用关系如下图所示:




三,各配置文件在项目中的调用关系

项目启动后会首先加载web.xml文件,先解析有关springmvc的有关配置,比如,监听器,过滤器等内容,然后加载spring的配置文件,主要包括bean的注入,定时器的配置等功能,

最后将spring下的配置文件下的bean注入到Java类中进行调用,进而完成整个配置文件的加载.



四,接下来将分别介绍以上5中配置文件的详解

1,pom.xml文件的配置详解,pom文件主要有2个作用:

下载jar包

给各种参数赋值

一个pom文件基本上包含以下部分

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><artifactId>iapp-webapp</artifactId><packaging>war</packaging>    <build>        <plugins>            <plugin>                <groupId>org.mortbay.jetty</groupId>                <artifactId>maven-jetty-plugin</artifactId>                <version>6.1.26</version>                <configuration>                    <connectors>                        <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">                            <port>8090</port>                            <maxIdleTime>0</maxIdleTime>                        </connector>                    </connectors>                </configuration>            </plugin>        </plugins>        <finalName>iapp-webapp</finalName>    </build>        <dependencies><!-- 以下是需要引入的jar包 --><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.8</version></dependency><dependency><groupId>org.quartz-scheduler</groupId><artifactId>quartz</artifactId><version>2.2.1</version></dependency></dependencies>    <!-- 以下是需要给参数赋的值,由于项目中一般开发环境和线上环境各参数的值一般不同,所以需要配置多份 --><profiles><profile><id>development</id><properties> <_db_mysql_driver>com.mysql.jdbc.Driver</_db_mysql_driver>                <_db_mysql_url>jdbc:mysql://ip:port/domainName?allowMultiQueries=true</_db_mysql_url>                <_db_mysql_user>userName</_db_mysql_user><_db_mysql_pwd>123456</_db_mysql_pwd><_db_mysql_maxActive>255</_db_mysql_maxActive><_db_mysql_maxIdle>10</_db_mysql_maxIdle><_db_mysql_minIdle>2</_db_mysql_minIdle><_db_mysql_maxWait>15000</_db_mysql_maxWait><_timeBetweenEvictionRunsMillis>60000</_timeBetweenEvictionRunsMillis><_minEvictableIdleTimeMillis>180000</_minEvictableIdleTimeMillis><_testWhileIdle>true</_testWhileIdle><_validationQuery>select 1</_validationQuery><_removeAbandoned>true</_removeAbandoned><_removeAbandonedTimeout>180</_removeAbandonedTimeout></properties><activation>    <!--告诉maven需要启动哪个profile  --><activeByDefault>true</activeByDefault></activation></profile><profile><id>production</id><properties>    <_db_mysql_driver>com.mysql.jdbc.Driver</_db_mysql_driver><_db_mysql_url>jdbc:mysql://ip:port/domainName?allowMultiQueries=true</_db_mysql_url><_db_mysql_user>userName</_db_mysql_user><_db_mysql_pwd>123456</_db_mysql_pwd><_db_mysql_maxActive>127</_db_mysql_maxActive><_db_mysql_maxIdle>5</_db_mysql_maxIdle><_db_mysql_minIdle>2</_db_mysql_minIdle><_db_mysql_maxWait>16000</_db_mysql_maxWait><_timeBetweenEvictionRunsMillis>60000</_timeBetweenEvictionRunsMillis><_minEvictableIdleTimeMillis>180000</_minEvictableIdleTimeMillis><_testWhileIdle>true</_testWhileIdle><_validationQuery>select 1</_validationQuery><_removeAbandoned>true</_removeAbandoned><_removeAbandonedTimeout>180</_removeAbandonedTimeout></properties><activation><activeByDefault>false</activeByDefault></activation></profile></profiles></project>


2,conf包下配置文件详解,一般该文件夹下的文件都是properties文件,下面以jdbc.properties进行讲解


#由于一般本地开发环境和线上环境的mysql的配置不相同,所以一般在pom文件中分别配置不同的值,#在这里只引用pom文件中的值即可,引入pom文件中值的格式是:${pom文件中值}#mysql数据库驱动,由于该驱动在测试环境下和线上环境的值都一样所以直接赋值DB_MSSQL_DRIVER=com.mysql.jdbc.Driver#数据库地址DB_MSSQL_URL=${_db_mysql_url}#用户名DB_MSSQL_USER=${_db_mysql_user}#密码DB_MSSQL_PW=${_db_mysql_pwd}#定义最大连接数,连接池的最大值,同一时间可以从池分配的最多连接数量,0时无限制maxActive=${_db_mysql_maxActive}#定义最大空闲,最大空闲值,当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,#一直减少到maxIdle为止,0时无限制 maxIdle=${_db_mysql_maxIdle}#定义最小空闲,当空闲的连接数少于阈值时,连接池就会预先申请一些连接,以免洪峰来时来不及申请 minIdle=${_db_mysql_minIdle}#定义最长等待时间(超时等待时间,单位:毫秒) maxWait=${_db_mysql_maxWait}#毫秒秒检查一次连接池中空闲的连接timeBetweenEvictionRunsMillis=${_timeBetweenEvictionRunsMillis}#连接保持空闲而不被驱逐的最长时间minEvictableIdleTimeMillis=${_minEvictableIdleTimeMillis}#在空闲时检查有效性testWhileIdle=${_testWhileIdle}#要求必需是个SELECT类型的SQL语句,至少返回一行,由于它会在所有应用的SQL语句执行之前运行一次,#所以原则上应该对数据库服务器带来的压力越小越好,推荐使用“SELECT 1validationQuery=${_validationQuery}#是否开启自动清理removeAbandoned=${_removeAbandoned}#自动回收超时时间(以秒数为单位)removeAbandonedTimeout=${_removeAbandonedTimeout}

3,web.xml文件的配置详解,一般包含

context-param(用于加载文件)

listener(加载springmvc的监听器 )

filter(进行url地址请求的过滤器)

servlet(进行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"    id="WebApp_ID" version="2.5">    <display-name>syslog</display-name>        <!-- 加载spring文件夹下的各种配置文件 -->    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:spring/app-config.xml</param-value>    </context-param>        <!-- 加载springmvc的监听器 -->    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>        <!--进行url地址请求的过滤器  -->    <filter>        <filter-name>HiddenHttpMethodFilter</filter-name>        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>    </filter>    <filter-mapping>        <filter-name>HiddenHttpMethodFilter</filter-name>        <servlet-name>springmvc</servlet-name>    </filter-mapping>        <filter>        <filter-name>EncodingFilter</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>    </filter>    <filter-mapping>        <filter-name>EncodingFilter</filter-name>        <url-pattern>/*</url-pattern>     </filter-mapping>            <servlet>        <servlet-name>springmvc</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath:spring/springmvc-servlet.xml</param-value>        </init-param>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>springmvc</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping>            <error-page>        <error-code>404</error-code>        <location>/WEB-INF/error/error_404.html</location>    </error-page>       </web-app>

接下来开始介绍spring文件夹下的文件配置

4,app-config.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"       xmlns:util="http://www.springframework.org/schema/util"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">       <!--用于扫描com.jd.iapp包下的java类  -->    <context:component-scan base-package="com.jd.iapp"/>    <context:annotation-config/>         <!--使用spring的依赖注入通过name和type获取bean-->    <aop:aspectj-autoproxy expose-proxy="true" proxy-target-class="true"/>        <!--给配置文件web-service.properties起个别名iappSys用于在java类中给参数赋值,在java中这么使用       @Value("#{iappSys.clientFile_url}")       private String clientFile_url; -->    <util:properties id="iappSys" location="classpath:conf/web-service.properties"/>        <!-- 加载各种配置文件,用于获取各个参数的值 -->    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">        <property name="locations">            <list>                <value>classpath:conf/important.properties</value>                <value>classpath:conf/jdbc.properties</value>                <value>classpath:conf/web-service.properties</value>                 <value>classpath:authen.properties</value>            </list>        </property>    </bean>        <!--引入别的spring文件夹下的配置文件  -->    <import resource="service-bean.xml"></import>    <import resource="jsf-config.xml"></import>    <import resource="spring-db-config.xml"></import>    <import resource="spring-quartz.xml"></import>    <import resource="spring-producer.xml"></import>    <import resource="spring-consumer.xml"></import></beans>

5,springmvc-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:mvc="http://www.springframework.org/schema/mvc"xmlns:util="http://www.springframework.org/schema/util"xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"><aop:aspectj-autoproxy proxy-target-class="true"/><context:component-scan base-package="com.jd"></context:component-scan><context:annotation-config/><!--是spring MVC为@Controllers分发请求所必须的--><mvc:annotation-driven/><!-- 如果发现是静态资源的请求,就将该请求转由Web应用服务器默认的Servlet处理,如果不是静态资源的请求,才由DispatcherServlet继续处理 --><mvc:default-servlet-handler/><!-- 静态资源所在位置 --><mvc:resources location="/WEB-INF/static/" mapping="/static/**" cache-period="864000"/><!-- 24 * 3600 * 10 -->    <!-- 加载配置文件 --><bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath:conf/spring-velocity.properties</value><value>classpath:authen.properties</value></list></property></bean>    <!-- 配置velocity引擎--><bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"><property name="resourceLoaderPath" value="${velocity.resource.load.path}" /><!-- 模板存放的路径 --> <property name="configLocation" value="${velocity.config.file}" /></bean><!-- 配置视图的显示--><bean id="velocityViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"><property name="order" value="3" /><property name="cache" value="${velocity.cache}" /><property name="suffix" value="${velocity.view.suffix}" /><!-- 视图文件的后缀名 -->  <property name="contentType" value="${velocity.content.type}" /><property name="exposeSpringMacroHelpers" value="true" /><!--是否使用spring对宏定义的支持--><!--是否开放request属性--><property name="exposeRequestAttributes" value="${velocity.exposeable.request.attributes}" />  <property name="exposeSessionAttributes" value="${velocity.exposeable.session.attributes}" /><property name="toolboxConfigLocation" value="${velocity.toolbox}" /><!--toolbox配置文件路径--> <property name="allowSessionOverride" value="true"/> <property name="allowRequestOverride" value="true"/> <property name="attributes"><props><prop key="ver">0.0.1</prop><prop key="indexPage">${demowebapp.homepage}</prop></props></property></bean><!-- 文件上传的配置--><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><property name="defaultEncoding" value="UTF-8" /><property name="maxUploadSize" value="8589934592" /><!--最大上传1G  --></bean><!-- 根据客户端的不同的请求决定不同的view进行响应, 如 /rest/1.json /rest/1.xml--><bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"><property name="order" value="1" /><property name="defaultContentType" value="text/html;charset=utf-8" /><property name="ignoreAcceptHeader" value="true" /><property name="mediaTypes"><map><entry key="xml" value="application/xml"></entry><entry key="json" value="application/json"></entry></map></property><property name="viewResolvers"><list><ref bean="velocityViewResolver" /></list></property><property name="defaultViews"><list><bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"></bean></list></property></bean></beans>


6,spring-db-config.xml主要用于数据库的连接,事务的管理,mapper文件和dao层文件的加载等,详情如下

<?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: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.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">        <property name="driverClassName" value="${pip.jdbc.driver}" />        <property name="url" value="${pip.jdbc.url}" />        <property name="username" value="${pip.jdbc.username}" />        <property name="password" value="${pip.jdbc.password}" />        <property name="defaultAutoCommit" value="false" />        <property name="initialSize" value="4" />        <property name="maxActive" value="80" />        <property name="maxIdle" value="30" />        <property name="minIdle" value="4" />        <property name="maxWait" value="10000" />        <property name="testWhileIdle" value="true" />        <property name="validationQuery" value="select 1" />        <property name="testOnBorrow" value="true" />        <property name="numTestsPerEvictionRun" value="3" />        <property name="removeAbandoned" value="true" />        <property name="removeAbandonedTimeout" value="180" />        <property name="timeBetweenEvictionRunsMillis" value="30000" />        <property name="minEvictableIdleTimeMillis" value="1800000" />    </bean>        <!-- 事务管理 -->    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="dataSource"/>    </bean>    <!--自动加载所有的mapper.xml文件,不再需要单独配置  -->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="dataSource" ref="dataSource"/>        <property name="configLocation" value="classpath:mybatis-config.xml"/>        <property name="mapperLocations">            <value>classpath:mapper/**/*.xml</value>        </property>    </bean>    <!--自动加载所有的mapper.xml所对应的dao层接口,不再需要单独配置  -->    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage" value="com.jd.ecc.workorder.business.center.dao"/>        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>    </bean></beans>

7,service-bean.xml的主要用于配置各种bean,用来给java类提供各种服务,详情如下

<?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:cache="http://www.springframework.org/schema/cache"       xmlns:util="http://www.springframework.org/schema/util"       xsi:schemaLocation="       http://www.springframework.org/schema/util        http://www.springframework.org/schema/util/spring-util-3.1.xsd   http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd   http://www.springframework.org/schema/cache    http://www.springframework.org/schema/cache/spring-cache.xsd">      <cache:annotation-driven cache-manager="cacheManager" />         <!--京东云存储 --><bean id="credential" class="com.jcloud.jss.Credential"><constructor-arg index="0" value="${jss.accessKey}"/><constructor-arg index="1" value="${jss.secretKey}"/></bean><bean id="clientConfig" class="com.jcloud.jss.client.ClientConfig"><property name="endpoint" value="${jss.hostName}"/><property name="connectionTimeout" value="${jss.connectionTimeout}"/><property name="socketTimeout" value="${jss.connectionTimeout}"/>   </bean> </beans>

8,log4j.xml的配置详情

log4j.xml配置文件只需要放到resouce文件夹下就可以了,系统会自动读取,不需要显性的引入,整体上SSM框架关系不大,网上都可以找到详细的配置讲解,我会在下一篇博客里专门来介绍.


五,以上就是整个内容,欢迎大家讨论,指正!

原创粉丝点击