第9步 spring 配置 springmvc配置
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/taskhttp://www.springframework.org/schema/task/spring-task-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://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/72859266Spring配置- - -<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.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://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>?
********************************************************************************************************************************************
?
?
總結
以上是生活随笔為你收集整理的第9步 spring 配置 springmvc配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第8步 第2剑客 mybatis p
- 下一篇: eclipse 创建ssm sprin