SSM 常用功能配置

SSM框架是由Spring、SpringMVC、MyBatis三個(gè)開源框架組成的,要使用SSM框架首先需要導(dǎo)入相應(yīng)的包,接著開始編寫配置文件。

主要的配置文件有:web.xml(自動(dòng)生成,在WEB-INF下),applicationContext.xml(spring配置文件,文件名可能不同,以自己的文件名為準(zhǔn)),springMVC.xml,Mapper.xml文件(操作數(shù)據(jù)庫的配置文件)。

執(zhí)行一個(gè)使用SSM框架搭建的javaWEB工程時(shí),服務(wù)器加載順序如下:

1.讀取web.xml配置文件,web.xml配置文件中 主要的配置有:

<!-- 根據(jù)指定路徑加載spring配置文件 -->

<!-- 1.關(guān)聯(lián)到spring主配置文件的路徑地址 classpath根目錄-->

<context-param>

? ? <param-name>contextConfigLocation</param-name>

? ? <param-value>classpath:spring/applicationContext.xml</param-value>

? </context-param>

<!--注意:此時(shí)服務(wù)器會(huì)去加載spring配置文件,暫停本段配置之后的代碼執(zhí)行;-->

2.加載spring配置文件,applicationContext.xml中的主要配置如下:

<!-- 1.開啟spring注解驅(qū)動(dòng) -->

<context:component-scan base-package="com.leo"/>

<!-- 2.讀取數(shù)據(jù)庫properties -->

<context:property-placeholder location="classpath:mybatis/properties/db.properties"/>

<!--3. 配置數(shù)據(jù)庫連接池 c3p0 -->

<bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">

? ? ? ? <property value="${jdbc.driver}" name="driverClass"/>

? ? ? ? <!-- 配置Jdbc的Url -->

? ? ? ? <property value="${jdbc.url}" name="jdbcUrl"/>

? ? ? ? <!-- 配置用戶名 -->

? ? ? ? <property value="${jdbc.username}" name="user"/>

? ? ? ? <!-- 密碼 -->

? ? ? ? <property value="${jdbc.password}" name="password"/>

? ? </bean>

<!-- 4.配置事務(wù)管理器 -->

<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">

? ? ? ? <!-- 事務(wù)管理數(shù)據(jù)庫連接池 -->

? ? ? ? <property name="dataSource" ref="dataSource"/>

? ? </bean>

? ? <!--5. 開啟事務(wù)的注解驅(qū)動(dòng) @Transactional -->

? ? <tx:annotation-driven transaction-manager="transactionManager"/>

? ? <!-- 6.spring管理mybatis配置文件 -->

? ? <!--生成mapper接口的代理類(簡而言之就是,dao層中只定義接口,spring使用了這個(gè)配置之后就會(huì)自動(dòng)根據(jù)mapper.xml文件中的語句生成dao層的實(shí)現(xiàn)類區(qū)操作數(shù)據(jù)庫-->

? ? <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">

? ? ? ? <!--dataSource屬性指定要用的連接池 ref映射的是id=dataSource的bean標(biāo)簽,讀取連接數(shù)據(jù)庫信息-->

? ? ? ? <property name="dataSource" ref="dataSource"/>

? ? ? ? <!--configLocation屬性指定mybatis的核心配置文件,管理mybatis配置文件-->

? ? ? ? <property value="classpath:mybatis/sqlMapConfig.xml" name="configLocation"/>

? ? ? ? <!-- 所有配置的mapper文件,mybatis的實(shí)體類配置文件所有的sql映射文件 -->

? ? ? ? <property value="classpath*:mybatis/mapper/*.xml" name="mapperLocations"/>

? ? </bean>

? ? <!-- 7.spring管理mybatis映射接口和sql映射文件之間關(guān)聯(lián)關(guān)系 -->

? ? <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

? ? ? ? <property value="com.leo.mapper" name="basePackage"/>

? ? ? ? <property value="sqlSessionFactory" name="sqlSessionFactoryBeanName"/>

? ? </bean>

? ? <!-- 8.AOP配置,執(zhí)行日志文件等操作-->

? ? <aop:config>

? ? ? ? <aop:pointcut expression="execution(* com.leo.service.*.*(..))" id="tp" />

? ? ? ? <aop:advisor advice-ref="ta" pointcut-ref="tp" />

? ? </aop:config>

</beans>

<!--至此spring配置文件加載完成,服務(wù)器回到web.xml文件中進(jìn)行后續(xù)的執(zhí)行:-->

3.繼續(xù)讀取web.xml配置文件(從上到下加載):

? ? ? 加載監(jiān)聽器;加載過濾器;加載前端控制器(即servlet或者springMVC),前端控制器中的1配置決定了前端控制器是否在容器啟動(dòng)時(shí)就加載,若其值大于等于0則在容器啟動(dòng)時(shí)加載,小于零或不設(shè)置時(shí)則不在容器啟動(dòng)時(shí)加載。若要在容器(服務(wù)器Tomcat)啟動(dòng)時(shí)就加載前端控制器,則此時(shí)暫停web.xml的加載,先去加載springMVC。

<!-- 2.配置監(jiān)聽器加載spring的配置文件 -->

? ? <listener>

? ? ? ? <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

? ? </listener>

? ? <!-- 3.配置過濾器,過濾編碼格式 為utf-8,只支持post提交 -->

? ? <filter>

? ? ? ? <filter-name>CharacterEncodingFilter</filter-name>

? ? ? ? <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

? ? ? ? <!--異步支持 -->

? ? ? ? <async-supported>true</async-supported>

? ? ? ? <!-- 初始化 編碼格式為UTF-8,只適用于post提交-->

? ? ? ? <init-param>

? ? ? ? ? ? <param-name>encoding</param-name>

? ? ? ? ? ? <param-value>UTF-8</param-value>

? ? ? ? </init-param>

? ? </filter>

? ? <filter-mapping>

? ? ? ? <filter-name>CharacterEncodingFilter</filter-name>

? ? ? ? <url-pattern>/*</url-pattern>

? ? </filter-mapping>

? ? <!-- 4.加載前端控制器(servlet或者springmvc) -->

? ? <!-- 配置DispatcherServlet,來加載springmvc的配置 -->

? ? <servlet>

? ? ? ? <servlet-name>DispatcherServlet</servlet-name>

? ? ? ? <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

? ? ? ? <init-param>

? ? ? ? ? ? <param-name>contextConfigLocation</param-name>

? ? ? ? ? ? <param-value>classpath:mvc/SpringMvc.xml</param-value>

? ? ? ? </init-param>

? ? ? ? <!-- 啟動(dòng)優(yōu)先加載 -->

? ? ? ? <load-on-startup>1</load-on-startup>

? ? </servlet>

? ? <servlet-mapping>

? ? ? ? <servlet-name>DispatcherServlet</servlet-name>

? ? ? ? <url-pattern>/</url-pattern>

? ? </servlet-mapping>

</web-app>

4.加載springMVC配置文件。

? ? ? ? 在web.xml配置文件加載前端控制器時(shí)根據(jù)其中的相關(guān)配置加載springMVC配置。加載控制器(第二步中未加載),自動(dòng)實(shí)例化相關(guān)類以便之后直接使用不用new(controller和servse),加載視圖解析器,完成springMVC配置文件的加載。

<!-- 1.開啟controller層中spring注解驅(qū)動(dòng) 打開 Component Controller Service Reposity -->

? ? <context:component-scan base-package="com.leo" />

<!-- 2.開啟springmvc特有的注解驅(qū)動(dòng)? 打開 @RequestMapping? @RequestParam? -->

? ? <mvc:annotation-driven>

? ? ? ? <mvc:message-converters><!--response.getWriter().Write()? @responsebody -->

? ? ? ? ? ? <bean class="org.springframework.http.converter.StringHttpMessageConverter">

? ? ? ? ? ? ? ? <constructor-arg value="UTF-8" /><!--解決@responsebody響應(yīng)中文亂碼-->

? ? ? ? ? ? </bean>

? ? ? ? ? ? <bean? ? ? ? ? ? ? ? ? ? class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">

? ? ? ? ? ? </bean>

? ? ? ? </mvc:message-converters>

? ? </mvc:annotation-driven>

? ? <!--3.放行靜態(tài)文件-->

? ? <mvc:default-servlet-handler/>

? ? <!--DispatcherServlet-->

? ? <!-- 靜態(tài)資源路徑配置? location表示路徑地址 -->

? ? <mvc:resources mapping="/resources/css/**" location="/resources/css/"/>

? ? <mvc:resources mapping="/resources/js/**" location="/resources/js/"/>

? ? <mvc:resources mapping="/resources/jsp/**" location="/resources/jsp/"/>

? ? <!-- 4.加載視圖解析器(處理jsp頁面所在的前綴和后綴) -->

? ? <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

? ? ? ? <!-- 前綴,確定當(dāng)前訪問的頁面路徑地址 -->

? ? ? ? <property name="prefix" value="/resources/jsp/">

? ? ? ? </property>

? ? ? ? <!-- 后綴,確定要訪問的文件類型 -->

? ? ? ? <property name="suffix">

? ? ? ? ? ? <value>.jsp</value>

? ? ? ? </property>

? ? </bean>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容