Spring配置文件和SpringMVC配置文件 web.xml配置文件 保存自用

话不多说,最近在周末自己抽时间写一些框架做的系统,当所有东西都需要自己配置时候发现自己压根记不住这么多类和路径,所以日常总结就变得尤为重要了
db-config.properties 将配置文件常量提出来可多次使用

hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
hibernate.hbm2ddl.auto=none
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.query.substitutions=true 1, false 0
hibernate.default_batch_fetch_size=16
hibernate.max_fetch_depth=2
hibernate.bytecode.use_reflection_optimizer=true
#hibernate.cache.use_second_level_cache=true
#hibernate.cache.use_query_cache=true
#hibernate.cache.region.factory_class=org.hibernate.cache.EhCacheRegionFactory
#net.sf.ehcache.configurationResourceName=/ehcache_hibernate.xml
hibernate.cache.use_structured_entries=true
hibernate.generate_statistics=true
hibernate.enable_lazy_load_no_trans=true
connection.provider_class = org.hibernate.c3p0.internal.C3P0ConnectionProvider
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=300
hibernate.c3p0.idle_test_period=3000
hibernate.c3p0.max_statements=50
hibernate.c3p0.validate=true
hibernate.c3p0.acquire_increment=2
connection.driver_class=com.mysql.jdbc.Driver

connection.url=jdbc:mysql://localhost:3306/dormitory?autoReconnect=true&autoReconnectForPools=true&useUnicode=true&characterEncoding=utf8
connection.username=root
connection.password=123456

c3p0.minPoolSize=3
c3p0.maxPoolSize=10
c3p0.initialPoolSize=3
c3p0.maxIdleTime=60
c3p0.acquireIncrement=3
c3p0.maxStatements=0
c3p0.idleConnectionTestPeriod=60
c3p0.acquireRetryAttempts=30
c3p0.breakAfterAcquireFailure=false
c3p0.testConnectionOnCheckout=false
c3p0.testConnectionOnCheckin=false
c3p0.debugUnreturnedConnectionStackTraces=true
c3p0.unreturnedConnectionTimeout=90

spring-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:task="http://www.springframework.org/schema/task" xmlns:mvc="http://www.springframework.org/schema/mvc"   
    xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/tx http://www.springframework.org/schema/tx/spring-tx-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/task   
       http://www.springframework.org/schema/task/spring-task-4.0.xsd
       http://www.springframework.org/schema/mvc   
       http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

       <!-- 扫描相应的包然后 -->
       <context:annotation-config base-package="com.vrv.common.service"/>
       <context:annotation-config base-package="com.vrv.common.dao"/>
       <context:annotation-config base-package="com.vrv.system.dao"/>
       <context:annotation-config base-package="com.vrv.system.service"/>
       <context:annotation-config base-package="com.vrv.buss.service"/>
       <!-- properties文件配置  引入 -->
       <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
       <property name="locations">
       <list>
       <value>classpath:*-config.properties</value>
       </list>
       </property>
       </bean>
       <!-- 数据库配置 -->

       <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <!-- 指定连接数据库的驱动 -->
        <property name="driverClass" value="${connection.driver_class}"/>
        <!-- 指定连接数据库的URL -->
        <property name="jdbcUrl" value="${connection.url}" />
        <!-- 指定连接数据库的用户名 -->
        <property name="user" value="${connection.username}" />
        <!-- 指定连接数据库的密码 -->
        <property name="password" value="${connection.password}" />
        <!-- 指定连接数据库连接池的最大连接数 -->
        <property name="maxPoolSize" value="${c3p0.maxPoolSize}" />
        <!-- 指定连接数据库连接池的最小连接数 -->
        <property name="minPoolSize" value="${c3p0.minPoolSize}" />
        <!-- 指定连接数据库连接池的初始化连接数 -->
        <property name="initialPoolSize" value="${c3p0.initialPoolSize}" />
        <!-- 指定连接数据库连接池的连接的最大空闲时间 -->
        <property name="maxIdleTime" value="${c3p0.maxIdleTime}" />



            <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
        <property name="acquireIncrement">
            <value>${c3p0.acquireIncrement}</value>
        </property>
        <!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements 属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。 
            如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
        <property name="maxStatements">
            <value>${c3p0.maxStatements}</value>
        </property>
        <!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
        <property name="idleConnectionTestPeriod">
            <value>${c3p0.idleConnectionTestPeriod}</value>
        </property>
        <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
        <property name="acquireRetryAttempts">
            <value>${c3p0.acquireRetryAttempts}</value>
        </property>
        <!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效 保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试 
            获取连接失败后该数据源将申明已断开并永久关闭。Default: false -->
        <property name="breakAfterAcquireFailure">
            <value>${c3p0.breakAfterAcquireFailure}</value>
        </property>
        <!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的 时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable 
            等方法来提升连接测试的性能。Default: false -->
        <property name="testConnectionOnCheckout">
            <value>${c3p0.testConnectionOnCheckout}</value>
        </property>

        <!-- 配置断开连接自动自重连 -->
        <property name="testConnectionOnCheckin">
            <value>${c3p0.testConnectionOnCheckin}</value>
        </property>

        <!-- 泄漏 监控 -->
        <property name="unreturnedConnectionTimeout">
            <value>${c3p0.unreturnedConnectionTimeout}</value>
        </property>

        <property name="debugUnreturnedConnectionStackTraces">
            <value>${c3p0.debugUnreturnedConnectionStackTraces}</value>
        </property>

    </bean>

    <!-- 配置Spring的SessionFactory -->
      <bean id="sessionFactory"  class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="packagesToScan">
            <list>
                <value>com.vrv.buss.entity.*</value>
                 <value>com.vrv.common.entity.*</value>
                  <value>com.vrv.system.entity.*</value>
            </list>
        </property>        
        <!-- 配置数据库本地方言等等操作哦 -->
        <property name="hibernateProperties">
            <props>
                <prop key="javax.persistence.validation.mode">none</prop>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.query.substitutions">${hibernate.query.substitutions}</prop>
                <prop key="hibernate.default_batch_fetch_size">${hibernate.default_batch_fetch_size}</prop>
                <prop key="hibernate.max_fetch_depth">${hibernate.max_fetch_depth}</prop>
                <prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
                <prop key="hibernate.bytecode.use_reflection_optimizer"></prop>
                <prop key="hibernate.enable_lazy_load_no_trans">${hibernate.enable_lazy_load_no_trans}</prop>
                <prop key="connection.provider_class">${connection.provider_class}</prop> 
                    <!-- 开启查询缓存 -->  
                <prop key="hibernate.cache.use_query_cache">true</prop>  
                <!-- 开启二级缓存 -->  
                <prop key="hibernate.cache.use_second_level_cache">true</prop>  
                <!-- 高速缓存提供程序 -->   
                <!-- 由于spring也使用了Ehcache, 保证双方都使用同一个缓存管理器 -->  
                <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop>  
                <!-- 最小连接数 --> 
                <prop key="hibernate.c3p0.min_size">${hibernate.c3p0.min_size}</prop>
                <!-- 最大连接数 -->
                <prop key="hibernate.c3p0.max_size">${hibernate.c3p0.max_size}</prop>
                <!-- 获得连接的超时时间,如果超过这个时间,会抛出异常,单位毫秒 -->
                <prop key="hibernate.c3p0.timeout">${hibernate.c3p0.timeout}</prop>
                <!-- 每隔3000秒检查连接池里的空闲连接 ,单位是秒-->
                <prop key="hibernate.c3p0.idle_test_period">${hibernate.c3p0.idle_test_period}</prop>
                <!--查询的最大结果集 -->
                <prop key="hibernate.c3p0.max_statements">${hibernate.c3p0.max_statements}</prop>
                <!-- 每次都验证连接是否可用 -->
                <prop key="hibernate.c3p0.validate">${hibernate.c3p0.validate}</prop>
                <!-- 当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 -->
                <prop key="hibernate.c3p0.acquire_increment">${hibernate.c3p0.acquire_increment}</prop>
            </props>
        </property>        
      </bean>
      <!-- 配置事务管理器 -->
                <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
                         <property name="sessionFactory" ref="sessionFactory"/>
                         <property name="dataSource" ref="dataSource"/>
                 </bean>

<!--事务传播属性配置-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="del*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="find*" propagation="REQUIRED" />
            <tx:method name="get*" propagation="REQUIRED" />
            <tx:method name="apply*" propagation="REQUIRED" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    <!-- 配置参与事务的类   定义事务切入点-->
    <aop:config>
        <aop:pointcut id="txPointcut" expression="execution(* com.vrv..service..*.*(..))" />
        <aop:advisor pointcut-ref="txAdvice" advice-ref="txPointcut" />
    </aop:config>



    <!-- 开启AOP监听 只对当前配置文件有效 -->
    <aop:aspectj-autoproxy expose-proxy="true" />  




    <!-- 避免IE执行AJAX时,返回JSON出现下载文件   编码设置-->
    <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>
    </bean> 

    <!-- cacheManager, 指定ehcache.xml的位置 -->   
    <bean id="cacheManagerEhcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">  
        <property name="configLocation">  
            <value>classpath:ehcache.xml</value>  
        </property>  
        <!-- 由于hibernate也使用了Ehcache, 保证双方都使用同一个缓存管理器 -->  
        <property name="shared" value="true"/>  
    </bean>  



       </beans>

spring-mvc.xml springmvc的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/aop   
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd   
        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/mvc   
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd   
        http://www.springframework.org/schema/tx   
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

        <!-- 定义扫描Controller包下的内容 -->
        <context:component-scan base-package="com.vrv.system.controller"/>
        <context:component-scan base-package="com.vrv.common.controller"/>
        <context:component-scan base-package="com.vrv.buss.controller"/>

          <!-- @Controller注解的使用前提配置  详细解释见(注.txt 1. )-->  
          <mvc:annotation-driven />   
          <!-- 加载静态文件 -->
            <mvc:resources location="/js/" mapping="/js/**" />
           <mvc:resources location="/css/" mapping="/css/**" />
            <mvc:resources location="/image/" mapping="/image/**" />
           <mvc:resources location="/font/" mapping="/font/**" />
          <!--  <context:annotation-config/> -->  
          <!-- 对module包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能-->  
         <context:component-scan base-package="module">  
         </context:component-scan>  

         <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->  
        <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />  

         <!-- 定义视图解析器,在视图模型前后添加前缀后缀 暂时只支持jsp后缀-->  
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="/WEB-INF/views/" /><!-- 路径前缀 -->  
        <property name="suffix" value=".jsp" /><!-- 后缀 -->  
        </bean>  

        <!-- 系统错误转发配置[并记录错误日志] -->
    <bean
        class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="defaultErrorView" value="500"></property>   <!-- 默认为500,系统错误(error.jsp) -->
        <property name="defaultStatusCode" value="404"></property>
        <property name="statusCodes"><!-- 配置多个statusCode -->
            <props>
                <prop key="error">500</prop>  <!-- error.jsp -->
            </props>
        </property>
        </bean>

        <!-- 拦截器配置 -->
        <mvc:interceptors>  
    <mvc:interceptor>  
        <mvc:mapping path="/**"/>  
        <bean class="com.vrv.system.interceptor.AuthInterceptor">  
        <property name="excludeUrls">
        <list>
        <value>LoginController.do?login</value>
        </list>
        </property>
        </bean>
    </mvc:interceptor>  
</mvc:interceptors>  
        </beans>

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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>comvrv</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:*-config.xml</param-value>
  </context-param>
  <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:log4j.properties</param-value>
  </context-param>
  <context-param>
    <param-name>log4jRefreshInterval</param-name>
    <param-value>6000</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>
  <filter>
    <filter-name>openSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
    <init-param>
      <param-name>singleSession</param-name>
      <param-value>true</param-value>
    </init-param>
    <init-param>
      <param-name>sessionFactoryBeanName</param-name>
      <param-value>sessionFactory</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>openSessionInViewFilter</filter-name>
    <url-pattern>*.do</url-pattern>
  </filter-mapping>
  <filter>
    <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <listener>
    <listener-class>
            org.springframework.web.util.IntrospectorCleanupListener
        </listener-class>
  </listener>
  <listener>
        <description>request监听器</description>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
  <servlet>
    <servlet-name>CaptchaServlet</servlet-name>
    <servlet-class>com.vrv.system.CaptchaServlet</servlet-class>
    <load-on-startup>10</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>CaptchaServlet</servlet-name>
    <url-pattern>/captchaCode</url-pattern>
  </servlet-mapping>
  <session-config>
    <session-timeout>30</session-timeout>
  </session-config>
  <error-page>
    <error-code>404</error-code>
    <location>/pages/common/404.jsp</location>
  </error-page>
    <welcome-file-list>
        <welcome-file>/pages/system/login.jsp</welcome-file>
    </welcome-file-list>
</web-app>

当然这些配置文件在大一些东西全都做了封装,你只需要之后自己要用什么直接调用就行了,但是在个人学习过程中,我们还是需要懂得配置和必须掌握的,这有利于我们对整个框架的工作原理进行掌握。
有句话说得好,不积硅步无以至千里!从小事做起,成长!希望这一周能够在工作之余自己将学生宿舍管理系统搭建起来!!加油
虽然现在出现了点问题,但是明天一定会解决,还有十天参加秋招,加油,多积累,然后看关于linux知识,准备秋招!!加油!

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值