1. 首先從web.xml配置入手
A: <!-- spring的監(jiān)聽器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
B: <!--加載配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
a: contextloaderlistener 這個(gè)類被定義為監(jiān)聽器援奢,并讀取在參數(shù) contextConfigLocation中定義的xml文件胶台,
如果不設(shè)置contextConfigLocation的初始參數(shù)則默認(rèn)會(huì)讀取WEB-INF路徑下的 application.xml文件,如
果需要自定義了另外的xml 則可以在contextConfigLocation下定義,ContextLoaderListener會(huì)讀取這些
XML文件并產(chǎn)生 WebApplicationContext對(duì)象荤懂,然后將這個(gè)對(duì)象放置在ServletContext的屬性里,這樣我們
只要可以得到Servlet就可 以得到WebApplicationContext對(duì)象喻鳄,并利用這個(gè)對(duì)象訪問spring 容器管理的bean憨闰。
b: Spring提供ServletContentListener的一個(gè)實(shí)現(xiàn)類ContextLoaderListener監(jiān)聽器状蜗,該類可以作為Listener
使用,在啟動(dòng)Tomcat容器的時(shí)候,該類的作用就是自動(dòng)裝載ApplicationContext的配置信息鹉动,如果沒有設(shè)置
contextConfigLocation的初始參數(shù)則會(huì)使用默認(rèn)參數(shù)WEB-INF路徑下的application.xml文件诗舰。如果需要自
定義讀取多個(gè)配置文件或者修改默認(rèn)路徑,則可以在web.xml中設(shè)置;ContextLoaderListener會(huì)讀取這些XML
文件并產(chǎn)生 WebApplicationContext對(duì)象训裆,然后將這個(gè)對(duì)象放置在ServletContext的屬性里,這樣我們只要
可以得到Servlet就可 以得到WebApplicationContext對(duì)象蜀铲,并利用這個(gè)對(duì)象訪問spring 容器管理的bean边琉。
c: ServletConfig對(duì)象在Servlet的配置文件中,可以使用一個(gè)或多個(gè)<init-param>標(biāo)簽為servlet配置一些初始化參數(shù)记劝。
(配置在某個(gè)servlet標(biāo)簽或者整個(gè)web-app下)當(dāng)servlet配置了初始化參數(shù)后变姨,web容器在創(chuàng)建servlet實(shí)例對(duì)象時(shí),
會(huì)自動(dòng)將這些初始化參數(shù)封裝到ServletConfig對(duì)象中厌丑,并在調(diào)用servlet的init方法時(shí)定欧,將ServletConfig對(duì)象傳
遞給servlet渔呵。進(jìn)而,程序員通過ServletConfig對(duì)象就可以得到當(dāng)前servlet的初始化參數(shù)信息砍鸠。
d:ServletContext對(duì)象
WEB容器在啟動(dòng)時(shí)扩氢,它會(huì)為每個(gè)WEB應(yīng)用程序都創(chuàng)建一個(gè)對(duì)應(yīng)的ServletContext對(duì)象,它代表當(dāng)前web應(yīng)用爷辱。
ServletContext對(duì)象應(yīng)用1:多個(gè)web組件之間使用它實(shí)現(xiàn)數(shù)據(jù)共享ServletConfig對(duì)象中維護(hù)了ServletContext
對(duì)象的引用录豺,開發(fā)人員在編寫servlet時(shí),可以通過ServletConfig.getServletContext方法獲得ServletContext對(duì)象饭弓。
由于一個(gè)WEB應(yīng)用中的所有Servlet共享同一個(gè)ServletContext對(duì)象双饥,因此Servlet對(duì)象之間可以通過ServletContext
對(duì)象來實(shí)現(xiàn)通訊。ServletContext對(duì)象通常也被稱之為context域?qū)ο?
C: <!-- POST提交過濾器 UTF-8 -->
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
D: <!--springmvc的前端控制器 -->
<servlet>
<servlet-name>crm</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!--
配置“1”弟断,tomcat啟動(dòng)時(shí)就初始化 DispatcherServlet咏花,
否則不配置就是第一次訪問時(shí)候才初始化DispatcherServlet
-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>crm</servlet-name>
<!--
1:*.do *.action 攔截以.do結(jié)尾的請(qǐng)求 (不攔截 jsp png jpg .js .css)
2:/ 攔截所有請(qǐng)求 (不攔截.jsp) 建議使用此種 方式 (攔截 .js.css .png) (放行靜態(tài)資源)
3:/* 攔截所有請(qǐng)求(包括.jsp) 此種方式 不建議使用
-->
<url-pattern>*.action</url-pattern>
</servlet-mapping>
2. 編寫數(shù)據(jù)源applicationContext-dao.xml
★:src下新建db.properties 內(nèi)容如下
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm-crm?characterEncoding=utf-8
jdbc.username=root
jdbc.password=1234
A: <!-- 配置 讀取properties文件 jdbc.properties -->
<context:property-placeholder location="classpath:db.properties" />
B: <!-- 配置 數(shù)據(jù)源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!-- 驅(qū)動(dòng) -->
<property name="driverClassName" value="${jdbc.driver}" />
<!-- url -->
<property name="url" value="${jdbc.url}" />
<!-- 用戶名 -->
<property name="username" value="${jdbc.username}" />
<!-- 密碼 -->
<property name="password" value="${jdbc.password}" />
</bean>
C: <!-- 配置 Mybatis的工廠 -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 數(shù)據(jù)源 -->
<property name="dataSource" ref="dataSource" />
<!-- 配置Mybatis的核心 配置文件所在位置 -->
<property name="configLocation" value="classpath:SqlMapConfig.xml" />
<!-- 配置pojo別名 -->
<property name="typeAliasesPackage" value="com.neo.pojo"></property>
</bean>
D: src下創(chuàng)建空文件 SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
E: <!-- 配置Mapper掃描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.neo.dao" />
</bean>
3. 編寫applicationContext-service.xml
<!-- 配置 掃描 @Service -->
<context:component-scan base-package="com.neo.service"/>
4. 編寫applicationContext-trans.xml
?
A: <!-- 事務(wù)管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 數(shù)據(jù)源 -->
<property name="dataSource" ref="dataSource" />
</bean>
B: <!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 傳播行為 -->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
C: <!-- 切面 -->
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.neo.service.*.*(..))" />
</aop:config>
5. 配置springmvc.xml
0: <!-- 加載屬性文件 -->
<context:property-placeholder location="classpath:resource.properties"/>
A: <!-- 配置掃描 器 -->
<context:component-scan base-package="com.neo.controller"/>
B: <!-- 配置處理器映射器 和 適配器 -->
<mvc:annotation-driven/>
C: <!-- 配置視圖解釋器 jsp -->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>