標籤:classpath 測試方法 pack unicode bean targe .sh config als
本文是基於已構建的struts2項目基礎上整合
spring+hibernate,若讀者還不熟悉struts2項目,請先閱讀
實現步驟:
第一步:引入spring所需jar包,如所示:
第二步:匯入hibernate所需jar包,如中被選中jar檔案:
第三步:匯入struts-spring整合包,暫且就這麼稱呼吧
第四步:匯入MySQL驅動包:
第五步:所有準備工作就緒後,接下來建立spring與hibernate設定檔,命名為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:context="http://www.springframework.org/schema/context"xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"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-3.0.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"></beans>
這是spring設定檔命名空間的引入,命名空間具體含義請參考前輩資料
6.2.3 Spring 2.5設定檔詳解(1)
第六步:在web.xml檔案中配置對spring的監聽,配置如下所示:
<?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_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>SSHDemo</display-name> <!-- spring的監聽器配置開始 --> <!-- spring監聽器的作用:提供執行個體(IOC) --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 將請求路徑交由struts過濾 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping></web-app>
這是在struts2項目基礎配置上增加對spring的監聽。
第七步:項目下構建業務層介面(TestServiceI)、實作類別(TestServiceImpl)以及資料庫操作層介面(TestDaoI)、實作類別(TestDaoImpl)
整個項目結構如所示:
其中dao層實作類別中我們需要注入一個session工廠(工廠注入請留意本文後續),接著開啟session工廠用於操作資料庫,代碼如下所示:
package wjt.com.test.dao.impl;import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.SessionFactory;import wjt.com.test.dao.TestDaoI;public class TestDaoImpl implements TestDaoI{ private SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } private Session getCurrentSession() { try { return sessionFactory.getCurrentSession(); } catch (HibernateException e) { return sessionFactory.openSession(); } } @Override public void testDaoMethod() { System.out.println("dao層測試方法..."); if(getCurrentSession()!=null) { System.out.println("session工廠注入成功!"); } } }
其中service層實作類別中我們暫時寫一個空方法進行測試dao層介面對象的注入成功與否並調用dao層方法,代碼如下:
package wjt.com.test.service.impl;import wjt.com.test.dao.TestDaoI;import wjt.com.test.service.TestServiceI;public class TestServiceImpl implements TestServiceI{ private TestDaoI testDao; public void setTestDao(TestDaoI testDao) { this.testDao = testDao; } @Override public void testServiceMethod() { System.out.println("service層測試方法..."); testDao.testDaoMethod(); }}
其中Action層我們在原來struts2項目的基礎上注入service層介面並調用其方法,代碼如下:
package wjt.com.test.action;import com.opensymphony.xwork2.ActionSupport;import wjt.com.test.service.TestServiceI;public class TestAction extends ActionSupport{ private TestServiceI testService; public void setTestService(TestServiceI testService) { this.testService = testService; } public String execute() throws Exception { System.out.println("struts=========================="); testService.testServiceMethod(); return "success"; }}
整個s2sh項目中的結構布局如上述第七步所示,用於視圖映射的類我們封裝在action包下,用於實現商務邏輯的我們封裝在service包下,用於訪問資料庫對資料進行操作的我們封裝在dao包下。
第八步:配置spring+hibernate的設定檔
截止到現在整個s2sh項目的準備工作已經準備完畢,接下來我們我們配置hibernate的資料來源、session工廠,配置spring的核心之一控制反轉IOC或稱為依賴注入DI。
配置如下:
<?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:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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-3.0.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 資料來源配置 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/wjt_test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&autoReconnect=true" /> <property name="username" value="root" /> <property name="password" value="wujingtao" /> </bean> <!-- 配置hibernate session工廠 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="hibernateProperties"> <props> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> <prop key="hibernate.show_sql">false</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> </bean> <!-- 注入對象 採用屬性注入方式--> <bean id="testDao" class="wjt.com.test.dao.impl.TestDaoImpl" scope="prototype"> <property name="sessionFactory" ref="sessionFactory"/><!-- 引用上方的hibernate工廠 --> </bean> <bean id="testService" class="wjt.com.test.service.impl.TestServiceImpl" scope="prototype"> <property name="testDao" ref="testDao"/> </bean> <bean id="testAction" class="wjt.com.test.action.TestAction" scope="prototype"> <property name="testService" ref="testService"/> </bean></beans>
其中IOC配置採用屬性注入的方式,大家可以根據個人喜好選擇spring注入的幾種方式,我個人偏愛基於註解的屬性注入方式,註解方式注入會在後續的文章中給出。
第九步:將struts2運行時的對象交由spring建立,即所謂的應用IOC
在struts.xml設定檔中添加如下配置:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"><!-- 這裡要和struts2版本號碼統一 --><struts> <!-- 告知Struts2運行時使用Spring來建立對象 這行代碼是新加的--> <constant name="struts.objectFactory" value="spring" /> <package name="default" extends="struts-default"> <action name="login" class="wjt.com.test.action.TestAction"> <result name="success">index.jsp</result> </action> </package></struts>
測試專案:
在tomcat下部署並啟動項目
瀏覽器地址欄輸入:http://localhost:8080/SSHDemo/login
瀏覽器顯示如下所示:
eclipse輸出顯示如下:(因為我們在各層有列印資訊)
到這裡完整的s2sh項目構建完畢並通過測試。
本人是剛畢業的小白,寫文章的目的是全當給自己做筆記了,博文有不合理的地方還請讀者指出!
eclipse環境下基於已構建struts2項目整合spring+hibernate