Chinaunix首页 | 论坛 | 博客
  • 博客访问: 18904
  • 博文数量: 21
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 211
  • 用 户 组: 普通用户
  • 注册时间: 2015-12-09 16:47
文章分类

全部博文(21)

文章存档

2017年(1)

2016年(9)

2015年(11)

我的朋友
最近访客

分类: Java

2015-12-24 14:33:20

要整合三大框架,一般是通过spring的依赖注入来进行自动生成代理,从而使他们自动关联在一起。

Spring整合struts

Spring整合struts的思路是让spring去管理应用中的控制器。可以充分利用springIOC属性,需要首先引入struts2-spring-plugin这个jar包,spring为插件提供了一种伪Action。在struts中配置action指定相应的属性,可以通过配置spring中的beanid来实现。这样用户请求action的时候sturts提供的只是一个伪类,实际调用是通过spring生成的代理类。

Spring 整合 Hibernate

Spring整合hibernate需要将DataSource注入到Spring中的sessionFactory bean工厂,而且要将sessionFactory注入到事务管理中,然后为其添加事务管理
对应的web.xml

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="" xmlns="" xsi:schemaLocation=" /web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  3.   <display-name>SSH</display-name>
  4.   <welcome-file-list>
  5.     <welcome-file>index.html</welcome-file>
  6.     <welcome-file>index.htm</welcome-file>
  7.     <welcome-file>index.jsp</welcome-file>
  8.     <welcome-file>default.html</welcome-file>
  9.     <welcome-file>default.htm</welcome-file>
  10.     <welcome-file>default.jsp</welcome-file>
  11.     <welcome-file>welcome.jsp</welcome-file>
  12.   </welcome-file-list>
  13.   <context-param>
  14.       <param-name>contextConfigLoaction</param-name>
  15.       <param-value>
  16.           /Web-INF/dap.xml,/Web-INF/dab.xml
  17.       </param-value>
  18.   </context-param>
  19.    <filter>
  20.       <filter-name>encoder</filter-name>
  21.       <filter-class>util.EncoderFilter</filter-class>
  22.   </filter>
  23.   <filter-mapping>
  24.       <filter-name>encoder</filter-name>
  25.       <url-pattern>/*</url-pattern>
  26.   </filter-mapping>
  27.   <filter>
  28.           <filter-name>struts2</filter-name>
  29.           <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  30.   </filter>
  31.   <filter-mapping>
  32.       <filter-name>struts2</filter-name>
  33.       <url-pattern>/*</url-pattern>
  34.   </filter-mapping>
  35.   <!-- spring 的监听器 以及相关属性配置文件的路径地址-->
  36.   <context-param>
  37.       <param-name>contextConfigLocation</param-name>
  38.       <param-value>classpath:applicationContext.xml</param-value>
  39.   </context-param>
  40.  <listener>
  41.      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  42.  </listener>
  43. </web-app>

applicationContext.xml中的文件

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns=""
  3.        xmlns:xsi=""
  4.        xmlns:aop=""
  5.        xmlns:tx=""
  6.        xmlns:context=""
  7.        xsi:schemaLocation="
  8.                            /spring-beans-2.5.xsd
  9.                            
  10.                            /spring-aop-2.5.xsd
  11.                            
  12.                            /spring-context-2.5.xsd
  13.                            
  14.                            /spring-tx-2.5.xsd">
  15.        <import resource="applicationContext-db.xml"/>
  16.       <!-- <import resource="applicationContext-person.xml"/>
  17.        
  18.        <import resource="applicationContext-department.xml"/> -->
  19.        <bean id="loginAction" class="action.LoginAction">
  20.        
  21.        </bean>
  22. </beans>

applicationContext-db.xml中的代码

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns=""
  3.        xmlns:xsi=""
  4.        xmlns:aop=""
  5.        xmlns:tx=""
  6.        xmlns:context=""
  7.        xsi:schemaLocation="
  8.                            /spring-beans-2.5.xsd
  9.                            
  10.                            /spring-aop-2.5.xsd
  11.                            
  12.                            /spring-context-2.5.xsd
  13.                            
  14.                            /spring-tx-2.5.xsd">
  15.     <!-- 注入sessionFactory -->
  16.     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  17.             <property name="configLocation">
  18.                 <value>classpath:hibernate.cfg.xml</value>
  19.             </property>
  20.     </bean>
  21.     <!-- 为事务注入sessionFactory -->
  22.     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  23.         <property name="sessionFactory" ref="sessionFactory"/>
  24.     </bean>
  25.     <!-- 配置事务管理策略 -->
  26.     <tx:advice id="adviceId" transaction-manager="transactionManager">
  27.         <tx:attributes>
  28.             <tx:method name="find*" read-only="true"/>
  29.             <tx:method name="update*"/>
  30.             <tx:method name="delete*"/>
  31.             <tx:method name="save*"/>
  32.         </tx:attributes>
  33.     </tx:advice>
  34.     <!-- 为事务管理注入切点 -->
  35.     <aop:config>
  36.         <aop:pointcut expression="execution(* service.Imp.*.*(..))" id="pointId"/>
  37.         <aop:advisor advice-ref="adviceId" pointcut-ref="pointId"/>
  38.     </aop:config>
  39.     <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
  40.          <property name="sessionFactory">
  41.              <ref bean="sessionFactory"/>
  42.          </property>
  43.      </bean>
  44. </beans>

struts.xml对应的数据

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE struts PUBLIC
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
  4.     "">
  5. <struts>
  6.    <!-- 配置文件改了以后不用重新启动 -->
  7.    <constant name="struts.devMode" value="true"/>
  8.    <constant name="struts.custom.i18n.resources" value="mess"/>
  9.  <!-- <include file=""></include> -->
  10. <!-- <include file="struts/struts-person.xml"></include> -->
  11.     <package name="action.hufei" extends="struts-default">
  12.         <!-- -->
  13.         <interceptors>
  14.             <!-- 定义一个拦截器 -->
  15.             <interceptor name="sportName1" class="intercepter.IntercepterTest">
  16.             </interceptor>
  17.             <interceptor name="methodInterceptor" class="intercepter.MethodInterceptor">
  18.                 <param name="name">拦截方法的拦截器</param>
  19.             </interceptor>
  20.             <!-- 定义一个拦截器栈 -->
  21.             <!--
  22.             <interceptor-stack name="SpotName">
  23.                 <interceptor-ref name="sportName1">
  24.              -->
  25.                     <!-- 给拦截器传递参数 -->
  26.                     <!--
  27.                     <param name="param1"></param>
  28.                     <param name="param2"></param>
  29.                 </interceptor-ref>
  30.                 <interceptor-ref name="sportName2"></interceptor-ref>
  31.             </interceptor-stack>
  32.         -->
  33.         </interceptors>
  34.         <action name="loginAction" class="loginAction" method="testForNow">
  35.             <!-- 使用拦截器 ,可以使用拦截器做过滤效果-->
  36.              <interceptor-ref name="sportName1"></interceptor-ref>
  37.              <interceptor-ref name="defaultStack"/>
  38.              <interceptor-ref name="methodInterceptor">
  39.                  <param name="includeMethods">
  40.                      execute
  41.                  </param>
  42.              </interceptor-ref>
  43.             <result name="success">
  44.                     /login.jsp
  45.             </result>
  46.         </action>
  47.     </package>
  48. </struts>

hibernate.cfg.xml对应的数据

点击(此处)折叠或打开

  1. <?xml version='1.0' encoding='UTF-8'?>
  2. <!-- <!DOCTYPE hibernate-configuration PUBLIC
  3.           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4.           ""> -->
  5. <!DOCTYPE hibernate-configuration PUBLIC
  6. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  7. "">
  8. <!-- Generated by MyEclipse Hibernate Tools. -->
  9. <hibernate-configuration>
  10.     <session-factory>
  11.         <!-- 定义数据库连接驱动 -->
  12.         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  13.         <!-- 指定数据库连接方式 -->
  14.         <property name="connection.url">jdbc:mysql://localhost/hibernate</property>
  15.         <property name="connection.username">root</property>
  16.         <property name="connection.password">root</property>
  17.         <property name="hibernate.show_sql">true</property>
  18.         
  19.         <!-- org.hibernate.dialect.MySQL5InnoDBDialect -->
  20.         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  21.         <!-- <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property> -->
  22.         <!-- 是否自动创建表 -->
  23.         <property name="hbm2ddl.auto">update</property>
  24.         <mapping resource="model/hibernate-person.xml"/>
  25.         <mapping resource="model/report.hb.xml"/>
  26.     </session-factory>


  27. </hibernate-configuration>


阅读(208) | 评论(0) | 转发(0) |
0

上一篇:servlet的接口实现

下一篇:自定义标签

给主人留下些什么吧!~~