自己理解(口語化以及借鑒別人的脖岛,自己理解)
ssm框架的整合
1.web.xml解剖
a.啟動一個WEB項目的時候,容器(如:Tomcat)會去讀它的配置文件web.xml.讀兩個節(jié)點
-
監(jiān)聽器<listener></listener>
-
寫法
<listener> <listener-class> org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
-
看ContextLoaderListener類的源碼
public class ContextLoaderListener extends ContextLoader implements ServletContextListener {}
繼承于ContextLoader類严蓖。
實現(xiàn) ServletContextListener接口
-
-
<context-param></context-param>
-
寫法
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>
自己理解就是加載applicationContext.xml文件,也就是spring.xml次洼。
-
b.容器創(chuàng)建一個ServletContext(上下文),這個WEB項目所有部分都將共享這個上下文
-
創(chuàng)建過程:
public void contextInitialized(ServletContextEvent event) { initWebApplicationContext(event.getServletContext()); }
類ContextLoaderListener實現(xiàn)ServletContextListener接口所實現(xiàn)的方法
c.容器將<context-param></context-param>轉(zhuǎn)化為鍵值對,并交給ServletContext.
d.容器創(chuàng)建<listener></listener>中的類實例,即創(chuàng)建監(jiān)聽.
a-d是啟動一個WEB項目的時候,加載web.xml讀取其中的兩個節(jié)點稳摄。讀取原理
e.在監(jiān)聽中會有contextInitialized(ServletContextEvent args)初始化方法,在這個方法中獲得ServletContext = ServletContextEvent.getServletContext();context-param的值 = ServletContext.getInitParameter("context-param的鍵");
f.得到這個context-param的值之后,你就可以做一些操作了.注意,這個時候你的WEB項目還沒有完全啟動完成.這個動作會比所有的Servlet都要早.
g..你可能想在項目啟動之前就打開數(shù)據(jù)庫.那么這里就可以在<context-param>中設(shè)置數(shù)據(jù)庫的連接方式,在監(jiān)聽類中初始化數(shù)據(jù)庫的連接.
h.這個監(jiān)聽是自己寫的一個類,除了初始化方法,它還有銷毀方法.用于關(guān)閉應(yīng)用前釋放資源.比如說數(shù)據(jù)庫連接的關(guān)閉.
e-h是具體的一些應(yīng)用
2.web.xml常用寫法
-
讀取web.xml文件時加載的節(jié)點
-
上下文參數(shù)
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>
-
監(jiān)聽器
<listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
-
-
其他
<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> <!-- 聲明監(jiān)聽器厌处,在tomcat啟動時創(chuàng)建spring容器 --> <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:springmvc.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>
-
Listener,Filter和servlet都是配置在web.xml文件中。這三個組成傳統(tǒng)意義上的servlet
啟動的順序為listener->Filter->servlet.
-
Listener生命周期:一直從程序啟動到程序停止運(yùn)行凝危。
ServletRequestListener:每次訪問一個Request資源前,都會執(zhí)行requestInitialized()方法晨逝,方法訪問完畢蛾默,都會執(zhí)行requestDestroyed()方法。
HttpSessionListener:每次調(diào)用request.getSession()捉貌,都會執(zhí)行sessionCreated()方法支鸡,執(zhí)行session.invalidate()方法,都會執(zhí)行sessionDestroyed()方法趁窃。
ServletRequestAttributeListener:每次調(diào)用request.setAttribute()都會執(zhí)行attributeAdded()方法牧挣,如果set的key在request里面存在,就會執(zhí)行attributeReplacerd()方法醒陆,調(diào)用request.removeAttribute()方法瀑构,都會執(zhí)行attributeRemoved()方法。
3. Filter生命周期:程序啟動調(diào)用Filter的init()方法(永遠(yuǎn)只調(diào)用一次,具體看啟動日志)刨摩,程序停止調(diào)用Filter的destroy()方法(永遠(yuǎn)只調(diào)用一次寺晌,具體看關(guān)閉日志),doFilter()方法每次的訪問請求如果符合攔截條件都會調(diào)用(程序第一次運(yùn)行澡刹,會在servlet調(diào)用init()方法以后調(diào)用呻征,不管第幾次,都在調(diào)用doGet(),doPost()方法之前)罢浇。
4. Servlet生命周期:程序第一次訪問陆赋,會調(diào)用servlet的init()方法初始化(只執(zhí)行一次,具體看日志)嚷闭,每次程序執(zhí)行都會根據(jù)請求調(diào)用doGet()或者doPost()方法攒岛,程序停止調(diào)用destory()方法(具體看結(jié)束日志)。
-
welcome頁面以及報錯頁面的書寫
<error-page> <!-- 錯誤碼 --> <error-code>404</error-code> <location>/WEB-INF/jsp/404.jsp</location> </error-page> <welcome-file-list> <welcome-file>/WEB-INF/jsp/login.jsp</welcome-file> </welcome-file-list>
2.springmvc.xml解析
讀取web.xml文件后胞锰,會根據(jù)里面的配置分別去尋找各自的配置文件
<?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" 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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 配置掃描的包
spring可以自動去掃描base-package下面或者子包下面的java文件阵子,
如果掃描到有@Component @Controller @Service @Repository等這些注解的類,則把這些類注冊為bean -->
<context:component-scan base-package="com.ajiatech.*" />
<!-- 注冊HandlerMapper胜蛉、HandlerAdapter兩個映射類 -->
<mvc:annotation-driven />
<!-- 在web.xml中springmvc攔截了所有請求挠进,包括靜態(tài)資源的請求色乾,Spring MVC會將它們當(dāng)成一個普通請求處理,因此找不到對應(yīng)處理器將導(dǎo)致錯誤领突。下面設(shè)視是spring3.0后改進(jìn)的暖璧,當(dāng)springmvc攔截了所有的請求以后,會在Spring MVC上下文中定義一個org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler君旦,它會像一個檢查員澎办,對進(jìn)入DispatcherServlet的URL進(jìn)行篩查,如果發(fā)現(xiàn)是靜態(tài)資源的請求金砍,就將該請求轉(zhuǎn)由Web應(yīng)用服務(wù)器默認(rèn)的Servlet處理局蚀,如果不是靜態(tài)資源的請求,才由DispatcherServlet繼續(xù)處理恕稠。 -->
<!-- 訪問靜態(tài)資源 -->
<!--會自動注冊RequestMappingHandlerMapping與RequestMappingHandlerAdapter兩個Bean,這是Spring MVC為@Controller分發(fā)請求所必需的琅绅,并且提供了數(shù)據(jù)綁定支持,@NumberFormatannotation支持鹅巍,@DateTimeFormat支持,@Valid支持讀寫XML的支持(JAXB)和讀寫JSON的支持(默認(rèn)Jackson)等功能千扶。
使用該注解后的springmvc-config.xml:-->
<mvc:default-servlet-handler />
<!-- 消息轉(zhuǎn)換器 當(dāng)springmvc的controller返回對象時,執(zhí)行消息轉(zhuǎn)換器骆捧, 把返回的java對象轉(zhuǎn)成json字符串澎羞,輸出貴客戶端。 -->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes" value="text/html;charset=UTF-8" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 視圖解析器 Controller處理完畢后將會轉(zhuǎn)發(fā)到相應(yīng)位置 prefix前綴 suffix后綴 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
3.applicationContext.xml解析
-
spring:業(yè)務(wù)邏輯組件
- 1.建一個jdbc.properties敛苇,先引入妆绞,數(shù)據(jù)源,事務(wù)控制枫攀,xxx摆碉。
- 2,掃描業(yè)務(wù)邏輯組件包(不掃描控制器)
- 3.配置Mybatis的整合:SqlSessionFactoryBean
指定mybatis全局配置文件的位置
指定mybatis脓豪,mapper文件的位置 - 4.配置掃描器巷帝,將mybatis接口的實現(xiàn)加入到ioc容器中
掃描所有DAO接口,加入到ioc容器中 - 5.事務(wù)控制配置
控制數(shù)據(jù)源 - 6.開啟基于注解的事物或者使用xml配置形式的事物(主要使用配置式)
1.切入點表達(dá)式
2.配置事物增強(qiáng)
3.配置事務(wù)如何增強(qiáng)扫夜;事物如何切入
所有方法都是事物方法
以get開始的所有方法
-
配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 掃描Spring除Controller之外的注解 --> <context:component-scan base-package="com.ajiatech"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <!-- 配置數(shù)據(jù)源 --> <bean id="jdbcDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/ajia_store?useSSL=true&characterEncoding=UTF8"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> <!-- spring和MyBatis完美整合楞泼,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="jdbcDataSource" /> <property name="mapperLocations" value="classpath:com/ajiatech/mapper/*.xml"></property> </bean> <!-- 加載mapper接口 代理對象 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.ajiatech.mapper"></property> </bean> </beans>
寫法
根據(jù)url找到對應(yīng)的servlet后,根據(jù)注解進(jìn)入對應(yīng)的controller中笤闯,傳參堕阔,獲取想要的對象,調(diào)用service的方法颗味。具體的實現(xiàn)在serviceImpl中書寫超陆,new出查詢語句。返回查詢的結(jié)果,然后進(jìn)行視圖渲染
-
文件
- *Mapper.java 一些增刪改查的方法
- *Mapper.xml 具體方法的實現(xiàn) sql語句
-
pojo 文件夾
- *.java 數(shù)據(jù)表的結(jié)構(gòu) 與set时呀。get方法
- *Example.java 用于添加sql里面的添加(eg:where)
-
service 文件夾
- *service.java 方法的提出
- *serviceImpl.java 方法的具體實現(xiàn)张漂。寫增刪改查
-
controller
- url找到controller
注解
- @Service用于標(biāo)注業(yè)務(wù)層組件
- @Controller用于標(biāo)注控制層組件(如struts中的action)
- @Repository用于標(biāo)注數(shù)據(jù)訪問組件,即DAO組件
- @RequestMapping是一個用來處理請求地址映射的注解谨娜,可用于類或方法上航攒。用于類上,表示類中的所有響應(yīng)請求的方法都是以該地址作為父路徑趴梢。