首先導入jar包
當你導入jar完之後呢? 第二步就要進行配置文件了
首先來配置spring的配置文件 我們首先把這個配置文件放到src根目錄下便於測試
命名爲applicationContext.xml
在spring配置文件中我們首先配置sessionFactory同時加載了hibernate.cfg.xml配置文件
當然你也可以寫在spring配置文件appliactionContext.xml中
這裏我用的spring2.0的配置方式大家能夠看的出來
我知道大家肯定很多東西是不需要去記的我們知道它的原理就可以了
找得到地方ctrl+C ctrl+V可以了
但是英語單詞也是要記的
比如說我們集成要的記得單詞LocalSessionFactoryBean 、 HibernateTransactionManager、DelegatingRequestProcessor等
回到正文:
接下來配置聲明式事務
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="orderAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* cn.order.dao.impl.*.*(..))" id="mycut"/>
<aop:advisor advice-ref="orderAdvice" pointcut-ref="mycut"/>
</aop:config>
配好這個之後呢? 接下來我們就容易了 依次到我們java類中set注入
dao、biz、web層set注入 這裏不介紹了
然後這樣hibernate和spring集成了 但是還有struts呀
所以接下來就是和struts集成
第一種:首先是通用的 配置
<!-- 配置監聽,加載spring配置文件
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
-->
第二種:
用插件的方式集成:
<message-resources parameter="com.order.struts.ApplicationResources" />
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="classpath:applicationContext.xml"
/>
</plug-in>
這個單詞 ContextLoaderPlugIn我們記住就可以了 哪裏找呢? 記住這個單詞 當然是spring-webmvc-struts.jar中找了
顧名思義嘛 對吧 所以不管spring名字再長 我們知道它的含義就可以了 所以配置這些東西對我們來說不難
難的是理解 理解其中的模式切入點切面通知哪些東西 要理解的話還需要時間的
學東西一定要抓住它的核心 這裏聲明一下我 不是主要不是講步驟 這些東西可以優化的 你瞭解了原理怎麼配都是對的
好了不管你是用第一種方式還是第二中方式 接下來是什麼呢? 不就是action了嗎?
當然是處理這個東西
因爲什麼呢? 我們要從spring的配置文件中去讀取biz業務層的功能
所以什麼呢? 所以我們就要把action和biz結合 但是action在struts-config.xml文件中呀
所以我們這裏用了代理
可以在action中配置type 爲org.springframework.web.struts.DelegatingActionProxy
struts-config.xml 中<action path="/do" type="org.springframework.web.struts.DelegatingActionProxy">
然後把applicationContext.xml spring配置文件中的<bean name="/do"></bean>
然後注入業務就行了不
還有其他方式就是配置controller就可以不要所有action中的DelegatingActionProxy
只需要在
</action-mappings>
<controller>
<set-property value="org.springframework.web.struts.DelegatingRequestProcessor" property="processorClass"/>
</controller>
<message-resources parameter="com.order.struts.ApplicationResources" />
在上面兩個節點中配置個set-property 然後對應指定就可以不用action代理了
說完了 當然我這個沒怎麼說清楚 截取幾個圖
希望學習還是多看幫助文檔 東西全在文檔上的