spring+springmvc+springdatajpa+Mybatis+web.xml配置集成

SpringMVC配置文件

<?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: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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--配置controller扫描包-->
    <context:component-scan base-package="cn.itsoruce.web.controller"/>
    <!--配置spirngmvc的注解驱动-->
    <mvc:annotation-driven/>
    <!--配置静态资源放行-->
    <mvc:default-servlet-handler/>
    <!--配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--配置视图资源前缀-->
        <property name="prefix" value="/WEB-INF/view/"/>
        <!--配置视图资源后缀-->
        <property name="suffix" value=".jsp"/>
    </bean>

 <!-- 解决springMVC在返回时json格式问题 -->
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>application/json; charset=UTF-8</value>
                        <value>application/x-www-form-urlencoded; charset=UTF-8</value>
                    </list>
                </property>
                <!-- No serializer:配置 objectMapper 为我们自定义扩展后的 CustomMapper,解决了返回对象有关系对象的报错问题 -->
                <property name="objectMapper">
                    <bean class="cn.itsource.commerce.common.CustomMapper"></bean>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    <!-- 扫描easypoi中所有的view -->
    <context:component-scan base-package="cn.afterturn.easypoi.view" />
    <!-- Bean解析器,级别高于默认解析器,寻找bean对象进行二次处理 -->
    <bean id="beanNameViewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver" p:order="0"/>
    <!--配置文件上传解析器-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!--配置上传大小10M-->
        <property name="maxUploadSize" value="10485760"/>
        <property name="defaultEncoding" value="utf-8">
        </property>
    </bean>
</beans>

Spring配置文件

<?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:context="http://www.springframework.org/schema/context"
       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/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">
    <!--配置spring管理bean扫描包-->
    <context:component-scan base-package="cn.itsoruce.dao,cn.itsoruce.service"/>
    <!--配置数据库连接jdbc.properties文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--引入数据库连接池-->
    <bean id="basicDataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <!--maxActive: 最大连接数量-->
        <property name="maxActive" value="150"/>
        <!--minIdle: 最小空闲连接-->
        <property name="minIdle" value="5"/>
        <!--maxIdle: 最大空闲连接-->
        <property name="maxIdle" value="20"/>
        <!--initialSize: 初始化连接-->
        <property name="initialSize" value="30"/>
        <!-- 连接被泄露时是否打印 -->
        <property name="logAbandoned" value="true"/>
        <!--removeAbandoned: 是否自动回收超时连接-->
        <property name="removeAbandoned" value="true"/>
        <!--removeAbandonedTimeout: 超时时间(以秒数为单位)-->
        <property name="removeAbandonedTimeout" value="10"/>
        <!--maxWait: 超时等待时间以毫秒为单位 1000等于60秒-->
        <property name="maxWait" value="1000"/>
        <!-- 在空闲连接回收器线程运行期间休眠的时间值,以毫秒为单位. -->
        <property name="timeBetweenEvictionRunsMillis" value="10000"/>
        <!--  在每次空闲连接回收器线程(如果有)运行时检查的连接数量 -->
        <property name="numTestsPerEvictionRun" value="10"/>
        <!-- 1000 * 60 * 30  连接在池中保持空闲而不被空闲连接回收器线程-->
        <property name="minEvictableIdleTimeMillis" value="10000"/>
        <property name="validationQuery" value="SELECT 1"/>
    </bean>
    <!--配置springdataJPA持久层框架-->
    <bean id="entityManagerFactoty" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <!--配置连接数据库属性-->
        <property name="dataSource" ref="basicDataSource"/>
        <!--配置管理domain对象-->
        <property name="packagesToScan" value="cn.itsoruce.domain"/>
        <!--配置jpa的建库建表策略-->
        <property name="jpaVendorAdapter">
            <bean  class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <!--update的建表策略-->
                <property name="generateDdl" value="true"/>
                <!--配置控制台显示sql语句-->
                <property name="showSql" value="true"/>
                <!--配置数据库的方言配置-->
                <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
            </bean>
        </property>
    </bean>

  <!--配置Mybatis持久层框架-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--配置数据库连接参数属性-->
        <property name="dataSource" ref="dataSource"></property>
        <!--配置Mapper扫描器包属性-->
        <property name="mapperLocations" value="classpath:cn/itsource/mybatis/mapper/*Mapper.xml" ></property>
        <!--配置扫描domain类别名 -->
        <property name="typeAliasesPackage">
            <value>
                cn.itsource.mybatis.domain
                cn.itsource.mybatis.query
            </value>
        </property>
        <!--配置分页拦截器,可以使用分页插件-->
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <value>
                           helperDialect=mysql
                          </value>
                    </property>
                </bean>
            </array>
        </property>
    </bean>
    <!-- 配置扫描mapper接口-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.itsource.mybatis.mapper"/>
    </bean>


    <!--事务管理器配置,主要管理上面的持久层框架对象->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactoty"/>
    </bean>
    <!--配置注解的事务声明式-->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

   <!--配置spring集成定时任务-->
    <task:scheduled-tasks>
       <!--配置我们的定时器bean,ref是被管理的类名,method是定时执行方法,cron是执行的时间-->
        <task:scheduled ref="Timer" method="myWorkTimer" cron="0 8,9 22 28 2 ?"/>
    </task:scheduled-tasks>


    <!--配置spring集成发送邮件-->
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="smtp.qq.com"/><!--发送邮箱类型-->
        <property name="username" value="[email protected]"/><!--发送邮箱账户-->
        <property name="password" value="xxxxxx"/><!--发送邮箱密码-->
        <property name="javaMailProperties"><!--发送邮箱其他属性-->
            <props>
                <!--必须进行授权认证,它的目的就是阻止他人任意乱发邮件-->
                <prop key="mail.smtp.auth">true</prop>
                <!--SMTP加密方式:连接到一个TLS保护连接-->
                <prop key="mail.smtp.starttls.enable">true</prop>
            </props>
        </property>
    </bean>

web.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         id="WebApp_ID" version="3.1">
  <!--启动spring的监听器-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!--加载springMVC的配置文件-->
  <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:applicationContext-mvc.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>
  <!--解决post提交的乱码问题-->
  <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>
  <!--解决连接对象提前关闭问题-->
  <filter>
    <filter-name>openEntity</filter-name>
    <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>openEntity</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <!--shiro的核心代理过滤器  -->
  <filter>
    <filter-name>shiroFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>shiroFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>

猜你喜欢

转载自blog.csdn.net/qq_43876886/article/details/89760957
今日推荐