Spring配置

来源:互联网 发布:centos 双网卡驱动 编辑:程序博客网 时间:2024/05/21 18:47

一、ApplicationContex.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
 <!--*************** DataSource ***************-->
 <!-- dataSource数据源c3p0数据库连接池配置 -->
 <bean id="dataSource"
  class="com.mchange.v2.c3p0.ComboPooledDataSource"
  destroy-method="close">
  <property name="driverClass">
   <value>com.mysql.jdbc.Driver</value>
  </property>
  <property name="jdbcUrl">
   <value>jdbc:mysql://localhost:3306/zeus?characterEncoding=UTF-8</value>
  </property>
  <property name="user">
   <value>jewelry</value>
  </property>
  <property name="password">
   <value>classic</value>
  </property>
  <property name="maxPoolSize">
   <value>10</value>
  </property>
  <property name="minPoolSize">
   <value>1</value>
  </property>
  <property name="initialPoolSize">
   <value>1</value>
  </property>
  <property name="maxIdleTime">
   <value>20</value>
  </property>
 </bean>

 <!--*************** Hibernate ***************-->
 <!-- Hibernate基本配置 -->
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource">
   <ref local="dataSource" />
  </property>
  <!-- 加载所有hbm映射文件 -->
  <property name="mappingResources">
   <list>
    <value>
     cn/com/classicjewelry/product/beans/Product.hbm.xml
    </value>
    <value>
     cn/com/classicjewelry/userright/beans/Role.hbm.xml
    </value>
    <value>
     cn/com/classicjewelry/userright/beans/Action.hbm.xml
    </value>
    <value>
     cn/com/classicjewelry/userright/beans/User.hbm.xml
    </value>
   </list>
  </property>
  <!-- 配置hibernate固有属性 -->
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">
     org.hibernate.dialect.MySQLDialect
    </prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
    <prop key="hibernate.jdbc.batch_size">20</prop>
   </props>
  </property>
 </bean>

 <!--*************** 动态代理 ***************-->
 <bean id="transactionManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>

 <!--*************** DAO零件层 ***************-->
 <!-- Product零件类 -->
 <bean id="productDAO"
  class="cn.com.classicjewelry.product.logicparts.ProductDAO">
  <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>

 <!-- 权限零件类 -->
 <bean id="userDAO"
  class="cn.com.classicjewelry.userright.logicparts.UserDAO">
  <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>
 <bean id="roleDAO"
  class="cn.com.classicjewelry.userright.logicparts.RoleDAO">
  <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>
 <bean id="actionDAO"
  class="cn.com.classicjewelry.userright.logicparts.ActionDAO">
  <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>

 <!--*************** Service装配层 ***************-->
 <!-- 项目装配类 -->
 <bean id="productService"
  class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  <property name="transactionManager" ref="transactionManager"></property>
  <property name="target">
   <bean
    class="cn.com.classicjewelry.product.logicmix.ProductService">
    <!-- 这里配置这个projectService实例里面的accessoryDAO等属性,所以必须与类中的属性同名 -->
    <property name="productDAO" ref="productDAO"></property>
    <!-- 上面的属性名称是类中对象名称,最好与bean一致,这里bean是指类中已建立的对象 -->
   </bean>
  </property>
  <property name="transactionAttributes">
   <props>
    <prop key="*">PROPAGATION_REQUIRED</prop>
   </props>
  </property>
 </bean>

 <!-- 权限装配类 -->
 <bean id="userRightService"
  class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  <property name="transactionManager" ref="transactionManager"></property>
  <property name="target">
   <bean
    class="cn.com.classicjewelry.userright.logicmix.UserRightService">
    <property name="userDAO" ref="userDAO"></property>
    <property name="roleDAO" ref="roleDAO"></property>
    <property name="actionDAO" ref="actionDAO"></property>
   </bean>
  </property>
  <property name="transactionAttributes">
   <props>
    <prop key="*">PROPAGATION_REQUIRED</prop>
   </props>
  </property>
 </bean>
</beans>

 

二、Action.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
 <class name="cn.com.classicjewelry.userright.beans.Action"
  table="ACTION_LIST">
  <id name="actionId" column="ACTION_ID" />
  <property name="actionName" column="ACTION_NAME" />
  <property name="detail" column="DETAIL" />

  <set name="roleList" table="ACTION_ROLE_MAP">
   <key column="ACTION_ID" />
   <many-to-many column="ROLE_ID"
    class="cn.com.classicjewelry.userright.beans.Role" />
  </set>

 </class>
</hibernate-mapping>

 

三、struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<struts-config>
 <!-- UserRight -->
 <form-beans>
  <form-bean name="RoleAddForm" type="org.apache.struts.action.DynaActionForm">
    <form-property name="roleName" type="java.lang.String"/>
    <form-property name="roleDetail" type="java.lang.String"/>
  </form-bean>
 </form-beans>
 
 
 
 <action-mappings> 
 <!-- UserRight -->
  <action path="/roleaddAction"
   type="cn.com.classicjewelry.userright.action.RoleAddAction"
   name="RoleAddForm" scope="session">
   <forward name="fail" path="/error/fail.jsp"></forward>
   <forward name="success" path="/../index.jsp"></forward>
  </action>
 
 </action-mappings>

</struts-config>

 

  四、BaseAction

package cn.com.classicjewelry.publicutil;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.Action;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.com.classicjewelry.product.logicmix.ProductService;
import cn.com.classicjewelry.userright.logicmix.UserRightService;

public class BaseAction extends Action {

 protected static UserRightService userRightService;

 protected static ProductService projectService;

 public synchronized WebApplicationContext setApplicationContext(
   HttpServletRequest request ) {
  WebApplicationContext cl = WebApplicationContextUtils.getWebApplicationContext( request
    .getSession().getServletContext() );
  return cl;
 }

 public synchronized UserRightService getUserRightInstance(
   HttpServletRequest request ) {
  return userRightService = ( UserRightService )setApplicationContext(
    request ).getBean( "userRightService" );
 }

 public synchronized ProductService getProjectInstance( HttpServletRequest request ) {
  return projectService = ( ProductService )setApplicationContext(
    request ).getBean( "projectService" );
 }
}