ssh2框架搭建

struts2+spring4.0+hibernate4.0

4.x版本與3.x版本有較大區別,要配置方法須要注意,用到的jar包如下



文件結構



src/applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <!-- 事務管理器,將委託給HibernateTransactionManager進行管理// -->
    <bean id="transactionManager"
          class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <!-- 事務處理的AOP配置 所有服務層bean聲明都要繼承此bean ,在proxy裏沒有定義target屬性,所以一定要在bean里加上
        abstract="true" // -->
    <bean id="TransactionProxyTemplate" abstract="true"
          class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager" ref="transactionManager"/>
        <property name="transactionAttributes">
            <props>
                <!-- 爲了保證服務層統一的事務處理。服務層接口,類的方法必須以下面的方法爲開頭 -->
                <!--spring 捕獲到RuntimeException和其他一些異常時纔會回滾,不是所有異常都會回滾,-Exception 設置 爲任何異常都回滾 -->
                <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="user*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
                <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
                <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
                <prop key="up*">PROPAGATION_REQUIRED,-Exception</prop>
                <prop key="mod*">PROPAGATION_REQUIRED,-Exception</prop>
                <prop key="del*">PROPAGATION_REQUIRED,-Exception</prop>
                <prop key="remove*">PROPAGATION_REQUIRED,-Exception</prop>
                <prop key="create*">PROPAGATION_REQUIRED,-Exception</prop>
                <prop key="execute*">PROPAGATION_REQUIRED,-Exception</prop>
                <prop key="do*">PROPAGATION_REQUIRED,-Exception</prop>
            </props>
        </property>
    </bean>

    <!-- 提供普java類獲取spring上下文 通過上下文獲取具體bean,調用其中的方法 -->
    <bean id="springApplicationContextUtil" class="com.soyann.common.util.SpringApplicationContextUtil"></bean>

    <!-- 系統初始化的時候讀取數據庫配置文件配置的數據庫類型 -->
    <bean class="com.soyann.common.util.DBPropertiesUtil" lazy-init="false" init-method="init"/>

    <!-- 根據MySQL數據庫identity獲取NEXTVAL -->
    <bean id="mySqlKeyGenerator"
          class="org.springframework.jdbc.support.incrementer.MySQLMaxValueIncrementer">
        <property name="dataSource" ref="sysDataSource"/>
        <property name="incrementerName">
            <value>sy_key_sequence</value>
        </property>
        <property name="columnName">
            <value>ID</value>
        </property>
    </bean>
</beans>

src/applicationContext-db

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<bean id="propertyConfigure"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:resource/properties/dbconfig.properties</value>
			</list>
		</property>
	</bean>
	<!-- 指定連接數據庫的驅動 -->
	<bean id="sysDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<!--The driver class name -->
		<property name="driverClass">
			<value>${jdbc.driverClassName}</value>
		</property>
		<!-- 指定連接數據庫的URL -->
		<property name="jdbcUrl">
			<value>${jdbc.url}</value>
		</property>
		<!-- 指定連接數據庫的用戶名 -->
		<property name="user">
			<value>${jdbc.username}</value>
		</property>
		<!-- 指定連接數據庫的密碼 -->
		<property name="password">
			<value>${jdbc.password}</value>
		</property>
		<!-- 指定連接池的初始化連接數 取值應在minPoolSize 與 maxPoolSize 之間.Default:3 -->
		<property name="initialPoolSize">
			<value>${jdbc.initialPoolSize}</value>
		</property>
		<!--最大空閒時間,60秒內未使用則連接被丟棄。若爲0則永不丟棄。Default: 0 -->
		<property name="maxIdleTime">
			<value>60</value>
		</property>
	</bean>

	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="sysDataSource"/>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
				<prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop>
				<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
				<prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
				<prop key="hibernate.connection.url">jdbc:mysql://192.168.8.100:3306/soyann</prop>
				<prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
			</props>

		</property>

		<!--  文件夾映射 -->
		 <property name="mappingDirectoryLocations">
		 	<list>
		 		<value>WEB-INF/classes/com/soyann</value>
		 	</list>
		 </property>

		<!-- 註解方式映射 -->
		 <property name="packagesToScan">
		 	<list>
		 	<value>com.soyann.*</value>
		 	</list>
		</property>
	</bean>

</beans>  

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <display-name>sshFrame</display-name>
    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>sshFrame.root</param-value>
    </context-param>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:resource/spring/**/app*.xml
        </param-value>
    </context-param>
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/classes/resource/properties/log4j.properties</param-value>
    </context-param>


    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>
        <filter-name>cleanup</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>cleanup</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

更多代碼:http://download.csdn.net/detail/neil89/8822091
發佈了65 篇原創文章 · 獲贊 24 · 訪問量 35萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章