首先导入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代理了
说完了 当然我这个没怎么说清楚 截取几个图
希望学习还是多看帮助文档 东西全在文档上的