第9步 spring 配置 springmvc配置

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27327261/article/details/85487814

spring配置

有5个网址   springboot

再讲一遍  spring的学习最好的方法是运行  官方demo  学习它里面的配置   。

我们不可能一下子理解spring里面的源码   

spring配置直接复制好了      视频老师也是从官方demo中复制过来的

直接复制

********************************************************************************************************************************************

1.首先讲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_2_5.xsd"
         id="WebApp_ID" version="2.5">

    <display-name>Archetype Created Web Application</display-name>



    <!-- CharacterEncodingFilter  配置过滤器 为了转码用的 -->
    <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>
        <!--  /*  拦截所有请求 走CharacterEncodingFilter
             我们就不用再写转utf-8这种了
           过滤所有的请求    -->
        <url-pattern>/*</url-pattern>
    </filter-mapping>





    <listener>
        <!--     web容器启动和关闭的监听器   只是监听web容器的启动和关闭     -->
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>






    <listener>
        <!--  ContextLoaderListener   web容器和spring容器进行整合进行监听     -->
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            <!--    指向spring配置文件    -->
            classpath:applicationContext.xml
            <!--   ContextLoaderListener 会 通过applicationContext.xml、
                   将spring容器和web容器进行整合
             -->
        </param-value>
    </context-param>







    <servlet>
        <!--DispatcherServlet    配置spring-mvc   -->
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        

        <!--  指定SpringMVC的文件  不写是   默认dispatcher-servlet.xml名字   -->
        <!--<init-param>-->
            <!--<param-name>contextConfigLocation</param-name>-->
            <!--<param-value>/WEB-INF/spring-mvc.xml</param-value>-->
            <!--<param-value>/WEB-INF/xxxx.xml (叫什么名字都可以)</param-value>-->
        <!--</init-param>-->
        
        
        <!--   servlet的配置  当大于等于0 就在容器启动时初始化这个servlet
        小于0或不指定 只有请求时才初始化servlet-->
        <load-on-startup>1</load-on-startup>
    </servlet>



    <servlet-mapping>
        <!-- DispatcherServlet  dispatcher 引用上面的springmvc配置 拦截所有的*.do   -->
        <servlet-name>dispatcher</servlet-name>
        <!--  springmvc 将所有的*.do进行拦截 -->
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>


    <!--NFDFlightDataTaskListener 监听器-->
    <!--<listener>-->
        <!--<listener-class>com.zjyouth.utils.NFDFlightDataTaskListener</listener-class>-->
    <!--</listener>-->

</web-app>

********************************************************************************************************************************************

1.首先讲web.xml

2.再讲一下spring容器的主配置     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:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:task="http://www.springframework.org/schema/task"

       xsi:schemaLocation="
        http://www.springframework.org/schema/task
     http://www.springframework.org/schema/task/spring-task-3.0.xsd
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">


    <!--    spring容器的主配置   -->


    <!--      扫描com.zjyouth下的一些注解 可以很方便的进行注入
    <context:component-scan base-package="com.zjyouth" annotation-config="true"/>
  -->
    <context:component-scan base-package="com.zjut" annotation-config="true"/>

    <!-- 定时 -->
    <context:component-scan base-package="com.zjyouth.*" />
    <task:executor id="executor" pool-size="5" />
    <task:scheduler id="scheduler" pool-size="10" />
    <task:annotation-driven executor="executor" scheduler="scheduler" />




    <!--    <context:annotation-config/>-->


    <!--
            在使用spectj注解实现springAOP:
            1.需要使用@Aspect注解来标注切面
            2.可以使用@before,@afterRuning,@around,@afterThrowning注解,来标注通知
            3.必须有切入点point-cut,使用@pointcut(execution(""))注解来标注切入点
            4.在aop.xml中,需要有
            https://blog.csdn.net/qq_37761074/article/details/72859266
            Spring配置- - -<aop:aspectj-autoproxy />
            2017年11月01日 11:54:08 阅读数:488更多
            个人分类: 日记
            版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ke_zhang_123/article/details/78412536
            <aop:aspectj-autoproxy proxy-target-class="true"/> 基于类的动态代理(依赖于CGlib库)
            通过配置织入@Aspectj切面
    -->
    <!--  aop的配置   spring配置文分成多个文件   datasource.xml   -->
    <aop:aspectj-autoproxy/>



    <!--   spring配置文件  dataSource分出来的子文件  -->
    <import resource="applicationContext-datasource.xml"/>


</beans>

********************************************************************************************************************************************

1.首先讲web.xml

2.再讲一下spring容器的主配置     applicationContext.xml

3.spring配置文件 dataSource分出来的子文件   applicationContext-datasource.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:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--    spring容器的自配置文件   -->
    <!--      扫描com.zjyouth下的所有注解 
 <context:component-scan base-package="com.zjyouth" annotation-config="true"/>
  -->
    <context:component-scan base-package="com.zjut" annotation-config="true"/>


    <!--  将常量分离出来  分出1个文件  -->
    <bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="order" value="2"/>
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="locations">
            <list>
                <!--  将常量分离出来  分出1个文件    -->
                <value>classpath:datasource.properties</value>
            </list>
        </property>
        <!--     指定字符集   -->
        <property name="fileEncoding" value="utf-8"/>
    </bean>





    <!--   dbcp数据库连接池配置    使用dbcp数据库连接池   -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${db.driverClassName}"/>
        <property name="url" value="${db.url}"/>
        <property name="username" value="${db.username}"/>
        <property name="password" value="${db.password}"/>
        <!-- 连接池启动时的初始值 -->
        <property name="initialSize" value="${db.initialSize}"/>
        <!-- 连接池的最大值 -->
        <property name="maxActive" value="${db.maxActive}"/>
        <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
        <property name="maxIdle" value="${db.maxIdle}"/>
        <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
        <property name="minIdle" value="${db.minIdle}"/>
        <!-- 最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制 -->
        <property name="maxWait" value="${db.maxWait}"/>
        <!--#给出一条简单的sql语句进行验证 -->
         <!--<property name="validationQuery" value="select getdate()" />-->
        <property name="defaultAutoCommit" value="${db.defaultAutoCommit}"/>
        <!-- 回收被遗弃的(一般是忘了释放的)数据库连接到连接池中 -->
         <!--<property name="removeAbandoned" value="true" />-->
        <!-- 数据库连接过多长时间不用将被视为被遗弃而收回连接池中 -->
         <!--<property name="removeAbandonedTimeout" value="120" />-->
        <!-- #连接的超时时间,默认为半小时。 -->
        <property name="minEvictableIdleTimeMillis" value="${db.minEvictableIdleTimeMillis}"/>

        <!--# 失效检查线程运行时间间隔,要小于MySQL默认-->
        <property name="timeBetweenEvictionRunsMillis" value="40000"/>
        <!--# 检查连接是否有效-->
        <property name="testWhileIdle" value="true"/>
        <!--# 检查连接有效性的SQL语句-->
        <property name="validationQuery" value="SELECT 1 FROM dual"/>
    </bean>



    <!--   这个配置重要 是mybatis的sqlSesstion的bean   -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--    ref="dataSource"  指的是上面dpcp数据库连接池     -->
        <property name="dataSource" ref="dataSource"/>
        <!--  这个就能读取到mybatis的所有的实现  -->
        <property name="mapperLocations" value="classpath*:mappers/*Mapper.xml"></property>

        <!-- 分页插件   在加上 pom中配置jar  mybatis的分页插件就配置好了  -->
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageHelper">
                    <property name="properties">
                        <value>
                            <!--  指明一下方言是mysql   -->
                            dialect=mysql
                        </value>
                    </property>
                </bean>
            </array>
        </property>

    </bean>

    <!--   mybatis 扫描包的方式    -->
    <!--    mybatis的一个扫描   会扫描dao层  对service层提供接口  这个配置很重要 
      -->
    <bean name="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- (空格删一些不然报错) <property name="basePackage" value="com.zjyouth.dao"/> -->
        <property name="basePackage" value="com.zjut.rtcf.dao"/>
    </bean>


    <!--  spring事务管理的配置     -->
    <!-- 使用@Transactional进行声明式事务管理需要声明下面这行 -->
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
    <!-- 事务管理 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <!--  数据库连接池 dbcp   -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 提交事务失败 出错是否回滚  true 回滚  -->
        <property name="rollbackOnCommitFailure" value="true"/>
    </bean>


</beans>

 spring配置讲完了  

********************************************************************************************************************************************

1.首先讲web.xml

2.再讲一下spring容器的主配置     applicationContext.xml

3.spring配置文件 dataSource分出来的子文件   applicationContext-datasource.xml     spring配置讲完了  

4.再讲一下spring mvc配置     dispatcher-servlet.xml  (名字可以在web.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:aop="http://www.springframework.org/schema/aop"
       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">

    <!--   springmvc配置文件 这个是默认的名字  dispatcher-servlet.xml    -->


    <!--   扫描controller注解 
    <context:component-scan base-package="com.zjyouth" annotation-config="true"/>
    -->
    <context:component-scan base-package="com.zjut.rtcf" annotation-config="true"/>

    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <!--  配置字符集  -->
                        <value>text/plain;charset=UTF-8</value>
                        <value>text/html;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
            <!--  @ResponseBody  注解将对象数据直接转换为json数据  -->
            <!--   SpringMVC自动进行反序列化的时候 的配置类  Jackson配置类  -->
            <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
                <!--  返回对象的时候  返回的可以是null    通过配置就过滤这个   本项目使用默认配置就不要了 -->
                <!--<property name="objectMapper">-->
                    <!--<bean class="org.codehaus.jackson.map.ObjectMapper">-->
                        <!--<property name="serializationInclusion" value="NON_EMPTY"/>-->
                    <!--</bean>-->
                <!--</property>-->
                <property name="supportedMediaTypes">
                    <list>
                        <!--  字符集    -->
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>


    <!--       -->
    <!-- 文件上传   直接使用SpringMVC提供的multipart这个工具就好了   -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="10485760"/> <!--   单位字节   上传的最大字节10m  -->
        <property name="maxInMemorySize" value="4096" />    <!-- 单位字节   最大内存4M  块的大小 -->
        <property name="defaultEncoding" value="UTF-8"></property>  <!--   默认编码    -->
    </bean>


     <!--  SpringMVC配置讲完了   -->

</beans>

********************************************************************************************************************************************

猜你喜欢

转载自blog.csdn.net/qq_27327261/article/details/85487814
今日推荐