spring+hibernate+struts配置

来源:互联网 发布:unity3d vuforia 教程 编辑:程序博客网 时间:2024/05/17 03:00
  1. applicationContext.xml配置
  2. 1>配置数据连接池
  3. <bean id="dataSource"
  4.         class="org.springframework.jdbc.datasource.DriverManagerDataSource"
  5.         destroy-method="close">
  6.         <property name="driverClassName">
  7.             <value>oracle.jdbc.driver.OracleDriver</value>
  8.         </property>
  9.         <property name="url">
  10.             <value>jdbc:oracle:thin:@localhost:1521:testDB</value>
  11.         </property>
  12.         <property name="username">
  13.             <value>111111</value>
  14.         </property>
  15.         <property name="password">
  16.             <value>111111</value>
  17.         </property>
  18. </bean>
  19. 2>配置sessionFactory
  20. <!--配置sessionFactory, 注意这里引入的包是hibernate3而不是hibernate  -->
  21.     <bean id="sessionFactory"
  22.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  23.         <property name="dataSource">
  24.             <ref local="dataSource" />
  25.         </property>
  26.         <!--库表对象导入-->
  27.         <property name="mappingResources">
  28.             <list>
  29.                 <value>org/seacat/vo/Voteinfo.hbm.xml</value>               
  30.             </list>
  31.         </property>
  32.         <!--操作属性-->
  33.         <property name="hibernateProperties">
  34.             <props>
  35.                 <prop key="hibernate.dialect">
  36.                     org.hibernate.dialect.Oracle9Dialect
  37.                 </prop>
  38.                 <prop key="hibernate.show_sql">true</prop>
  39.             </props>
  40.         </property>
  41.     </bean>
  42.     
  43. 3>将数据连接对象注入DAO
  44.   <bean id="userMgrDAO" class="org.seacat.dao.UserMgrDAO">
  45.         <property name="sessionFactory">
  46.             <ref local="sessionFactory" />
  47.         </property>
  48.     </bean>
  49.     
  50. 4>将数据连接对象注入事务管理
  51.   <bean id="transactionManager"
  52.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  53.         <property name="sessionFactory">
  54.             <ref local="sessionFactory"/>
  55.         </property>
  56.     </bean>
  57. 5>将事务管理代理DAO
  58. <bean id="userMgrDAOProxy"
  59.         class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  60.         <property name="transactionManager">
  61.             <ref bean="transactionManager" />
  62.         </property>
  63.         <property name="target">
  64.             <ref local="userMgrDAO" />
  65.         </property>
  66.         <!-- 添加操作执行权限-->
  67.         <property name="transactionAttributes">
  68.             <props>
  69.                 <prop key="save*">PROPAGATION_REQUIRED</prop>
  70.                 <prop key="delete*">PROPAGATION_REQUIRED</prop>
  71.                 <prop key="update*">PROPAGATION_REQUIRED</prop>
  72.                 <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
  73.             </props>
  74.         </property>
  75.     </bean>
  76. 6>************************spring依赖注入的核心,服务层注入领域层****************************************
  77.   <bean id="userService" class="org.seacat.service.UserServiceImpl">
  78.         <property name="userDao">
  79.             <ref local="userMgrDAOProxy" />
  80.         </property>
  81.     </bean>
  82.     
  83. spring整合struts方式
  84. 1.替换 Action 的 Type
  85. <action path="/login" …
  86. type="org.springframework.web.struts.DelegatingActionProxy" />
  87. -------->>>>applicationContext.xml配置
  88. 要点: 通过 path 和 bean 的 name 进行匹配, 这两个值必须一样
  89. <bean name="/login" class="com.test.struts.action.LoginAction"></bean>
  90. --------->>>(struts-config.xml)
  91. 2.添加 Spring Plug in 最后
  92. <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
  93. <set-property property="contextConfigLocation" value="/WEBINF/
  94. classes/applicationContext.xml" />
  95. </plug-in>
  96. struts1加载spring的两种方式:
  97. 第一种:通过web.xml配置加载spring上下文环境,其配置方式如下:
  98. web.xml
  99. <context-param>
  100. <param-name>contextConfigLocation</param-name>
  101. <param-value>/WEB-INF/applicationContext.xml</param-value>
  102. </context-param>
  103. 通过listener加载
  104. <listener>
  105. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  106. </listener>
  107. 或者利用severlet类加载
  108. <servlet>
  109. <servlet-name>context</servlet-name>
  110. <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
  111. <load-on-startup>1</load-on-startup>
  112. </servlet>
  113. 第二种方式:
  114. 使用Struts 插件
  115. 在struts-config.xml中
  116. <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
  117. <set-property property="contextConfigLocation"
  118. value="/WEB-INF/applicationContext.xml"/>
  119. </plug-in>
原创粉丝点击