===============================================struts和spring架構結合配置=============================================
要將Spring作為Struts的一個外掛程式來使用,在struts-config.xml檔案中加入如下配置:
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
</plug-in>
在Web.xml中通過下面的方式載入Spring
<context-param>
<param-name>
contextConfigLocation
</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
===============================================struts和spring結合三種方式============================================
1)。繼承org.springframework.web.struts.ActionSupport類
將Action類的繼承方式更改為org.springframework.web.struts.ActionSupport或org.springframework.web.struts.DispatchActionSupport
在Action類中需要得到Spring Context ApplicationContext context = this.getWebApplicationContext();
2)。控制器方式
需要在struts-config.xml檔案中加入:
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor">
</controller>
在applicationContext.xml檔案中不僅需要注入DAO實作類別,還要通過Action類的path路徑注入Action類
3)。委託代理
在struts-config.xml檔案中更改Action標籤裡的type屬性為: org.springframework.web.struts.DelegatinActionProxy
在applicationContext.xml檔案中與控制器方式一樣,注入DAO實作類別與Action。
<!– 注入實作類別 -->
<bean name="threeifc" class="com.soft.test.TestIMP">
<property name="template" ref="template"></property>
</bean>
<!– 注入Action -->
<bean name="/path路徑" class="com.soft.three.ThreeAction">
<property name="threeifc" ref="threeifc"></property>
</bean>
===============================================hibernate架構與spring架構結合========================================
//hibernate設定檔存放在src下面
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
=================================================jdbc資料來源配置=====================================================
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@127.0.0.1:1521:name</value>
</property>
<property name="username">
<value>username</value>
</property>
<property name="password">
<value>password</value>
</property>
</bean>
==================================================JNDI資料來源配置====================================================
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/oracle</value>
</property>
</bean>
<!– Hibernate的一些參數的配置 -->
<property name="hibernateProperties">
<props>
<prop key=“hibernate.dialect”><!– 資料庫方言 -->
org.hibernate.dialect.Oracle9Dialect
</prop>
<prop key=“hibernate.show_sql”><!– 在控制台列印sql語句 -->
true
</prop>
</props>
</property>
<!– 表映射 -->
<property name="mappingResources">
<list>
<value>com/webssh/TableName.hbm.xml</value>
</list>
</property>
=====================================================hibernate模板注入==============================================
<!-- HibernateTemplate -->
<bean name=" hibernateTemplate " class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
使用HibernateTemplate的方式有兩種:
方式一:
<!-- 將模板注入給介面,需要在實作類別中寫屬性 -->
<bean id="classIFC" class="com.webssh.classinfo.ClassIMP2">
<property name="template" ref="hibernateTemplate"></property>
</bean>
方式二:
<!-- 將模板注入給介面,不需要在實作類別中寫屬性 -->
介面要繼承org.springframework.orm.hibernate3.support.HibernateDaoSupport類
<bean id="classIFC" class="com.webssh.classinfo.ClassIMP2">
<property name="hibernateTemplate" ref="hibernateTemplate">
</property>
</bean>