s2sh集成配置

--------------------Struts2--------------------web

Struts.xml的配置以下:spring

<?xml version="1.0" encoding="UTF-8" ?>sql

<!DOCTYPE struts PUBLICexpress

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"apache

    "http://struts.apache.org/dtds/struts-2.0.dtd">session

 

<struts>oracle

<constant name="struts.devMode" value="true" />app

<!-- 默認的視圖主題 -->ssh

    <constant name="struts.ui.theme" value="simple" />jsp

    <constant name="struts.objectFactory" value="spring" />

    <package name="test" extends="struts-default">

            <action name="login" method="login">

<!--這裏的classssh中是由Spring配置的baen,不是真實存在的類。若是不是ssh,只是配置struts,則是真實存在的類,如com.neusoft.test.action.userAction -->

                                                <result name="success">/main.jsp</result>

                                                <result name="fail">/fail.jsp</result>

                                </action>

    </package>

</struts>

 

Struts.xml是由web.xml配置文件讀取,下面的是讀取的配置:

<filter>

                                <filter-name>struts2</filter-name>

                                <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

                                <!--<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>-->

                                <!--struts2使用這兩個類均可以-->

                               

                                <!--struts2會默認去類路徑下去讀struts.xml配置文件,也就是src/下,通常將struts.xml配置文件放在src/

                                             若是將struts.xml配置文件放在WEB-INF/**/,則須要添加下面的配置,用來指定struts.xml的路徑-->

                                <!--

                                                <init-param>

                                                <param-name>config</param-name>

                                                <param-value>struts-default.xml,struts-plugin.xml,../**/struts.xml</param-value>

                                                </init-param>

                                -->

                </filter>

                <filter-mapping>

                                <filter-name>struts2</filter-name>

                                <url-pattern>/*</url-pattern>

                </filter-mapping>

--------------------Spring--------------------

Spring的配置文件是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" xmlns:aop="http://www.springframework.org/schema/aop"

                xmlns:tx="http://www.springframework.org/schema/tx"

                xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

 

<!--

咱們的action對象,在此以前是struts容器給咱們建立的, 而不是Spring IoC給咱們建立的,即不是從IOC容器中拿出來, 若是要利用Spring IoC容器生成該Actiond對象並取出來, 咱們就必須須要獲得BeanFactory對象,這樣的話雖然能夠在這類裏實現, 可是產生了對BeanFactory依賴!! 因此在"applicationContext.xml"文件中,僅憑以下配置:

<bean id="loginAction" class="com.neusoft.web.actions.LoginAction">

                <property name="userManager" ref="userManager"/></bean>

是不起任何做用的!也注入不進來。 因此要修改配置, id 屬性修改成name

                 * spring 默認是建立單實例的,在這裏,咱們能夠經過scope="prototype"來建立多實例。

                -->

                <bean name="userAction" class="com.neusoft.test.action.UserAction">

                                <property name="sessionFactory" ref="sessionFactory"/>

                </bean>

               

                <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"

                                destroy-method="close">

                                <property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />

                                <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost :1521:orcl" />

                                <property name="user" value="hr" />

                                <property name="password" value="orcl" />

                                <!--初始化時獲取的鏈接數,取值應在minPoolSizemaxPoolSize之間。Default: 3 -->

                                <property name="initialPoolSize" value="1" />

                                <!--鏈接池中保留的最小鏈接數。-->

                                <property name="minPoolSize" value="1" />

                                <!--鏈接池中保留的最大鏈接數。Default: 15 -->

                                <property name="maxPoolSize" value="30" />

                                <!--最大空閒時間,60秒內未使用則鏈接被丟棄。若爲0則永不丟棄。Default: 0 -->

                                <property name="maxIdleTime" value="60" />

                                <!--當鏈接池中的鏈接耗盡的時候c3p0一次同時獲取的鏈接數。Default: 3 -->

                                <property name="acquireIncrement" value="5" />

                                <!--60秒檢查全部鏈接池中的空閒鏈接。Default: 0 -->

                                <property name="idleConnectionTestPeriod" value="60" />

                </bean>

 

                <!--

                                #### 配置sessionFactory ####

注入hibernatesessionFactory,LocalSessionFactoryBean裏面的哪一個參數上,由property屬性決定:configLocation,它是LocalSessionFactoryBean類的屬性,對應的該類的代碼在248行: public void setConfigLocation(Resource configLocation)            { this.configLocations = new Resource[] {configLocation};} 因此 propertyname的值是 configLocation」。要把hibernate的配置文件hibernate.cfg.xml注入進來,即LocalSessionFactoryBean類的屬性中。那麼如何找到這個hibernate的配置文件,咱們需使用spring給咱們實現的一個協議classpath:******,這樣spring就會到classpath路徑下搜索咱們這個配置文件,而後就能幫助咱們建立出sessionFactory

                -->

 

                <bean id="sessionFactory"

                                class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

                                <property name="dataSource" ref="dataSource" />

 

                                <property name="configLocation">

                                                <value>classpath:hibernate.cfg.xml</value>

                                </property>

                </bean>

 

                <!--

                                #### 配置事務管理器 #### 事務管理器是由Spring

HibernateTransactionManager類來封裝實現的,只要配置進來便可。首先要將sessionFactory注入進來,見代碼:HibernateTransactionManager類的: 第:135行:private SessionFactory sessionFactory; 第:179行: public void  setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory              = sessionFactory;} 這樣就能夠把剛纔容器幫咱們實現的sessionFactory實例注入進來了。 <ref  bean="sessionFactory"/>這個ref引用的就是25行的id.

                -->

 

                <bean id="txManager"

                                class="org.springframework.orm.hibernate3.HibernateTransactionManager">

                                <property name="sessionFactory" ref="sessionFactory" />

                </bean>

 

                <tx:advice id="txAdvice" transaction-manager="txManager">

                                <tx:attributes>

                                                <tx:method name="add*" propagation="REQUIRED" read-only="false" />

                                                <tx:method name="del*" propagation="REQUIRED" read-only="false" />

                                                <tx:method name="modify*" propagation="REQUIRED" read-only="false" />

                                                <tx:method name="find*" read-only="true" />

                                                <!--

對如上開頭命名的方法之外的方法的事務處理應該以下配置: <tx:method name="*" read-only="true"/>    配置爲只讀事務,若是是隻讀事務,會提升性能,即當你更新了某一個對象的時候, 再也不作髒數據檢查了,性能上有必定的優化。

                                                -->

                                </tx:attributes>

                </tx:advice>

 

                <aop:config>

                                <!-- 其實這裏就是在指定(或者說定義)切入點 -->

                                <aop:pointcut id="allManagerMethod"

                                                expression="execution(* com.neusoft.test.service..*.*(..))" />

                                <!-- 其實就是定義並指定在該切入點使用什麼切面(也就是建言) -->

                                <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice" />

                </aop:config>

</beans>

 

applicationContext.xmlweb.xml配置讀取,下面的是讀取的配置:

<!-- 注意:Spring的配置文件applicationContext.xml的位置能夠不是固定的。

                 因爲本項目中咱們的applicationContext.xml是放在了src classpath根目錄下,

                 因此咱們應該以下配置它,固然也能夠放在WEB-INF下:

  <context-param>

              <param-name>contextConfigLocation</param-name>

              <param-value>classpath*:applicationContext.xml </param-value>

<!--<param-value> /WEB-INF/applicationContext.xml</param-value>-->

  </context-param>

  -->

  <!-- 本項目中,我根據配置文件中內容的不一樣,拆分爲3個文件,

                 都是以"applicationContext-"開頭,因此這裏,我以*來匹配他們。

                                classpath:只會到你的class路徑中查找文件;

classpath*:不只包含class路徑,還包括jar文件中(class路徑)進行查找

   -->

   <context-param>

              <param-name>contextConfigLocation</param-name>

              <param-value>classpath*:applicationContext-*.xml</param-value>

  </context-param>

 

  <!--  接下來還須要配置Spring 的上下文加載監聽器,             該監聽器負責讀取咱們的配置變量contextConfigLocation           獲得咱們的配置文件 applicationContext-common.xmlapplicationContext-actions.xmlapplicationContext-beans.xml。而後生成BeanFactory對象放入到ServletContext對象中。  能夠在spring.jar中找到專門來讀取這個配置文件的類: org.springframework.web.context. ContextLoaderListener    對應的相關代碼能夠在其中查看獲得。

              -->

  <listener>

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

  </listener>

 

  <!-- CharacterEncodingFilterSpring爲咱們提供的字符編碼過濾器,

                 固然咱們能夠本身寫一個相關的Filter鏈進來,也可使用Spring提供的。

   -->

  <filter>

              <filter-name>Spring character encoding filter</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>

  </filter>

  <filter-mapping>

              <filter-name>Spring character encoding filter</filter-name>

              <url-pattern>/*</url-pattern>

  </filter-mapping>

 

--------------------Hibernate--------------------

Hibernate的配置文件是hibernate.cfg.xml,在ssh中使用時配置以下:

<!DOCTYPE hibernate-configuration PUBLIC

          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

 

<hibernate-configuration>

                <session-factory>

                               

                                <!--指定映射文件-->

                                <mapping resource="com/neusoft/test/entity/User.hbm.xml" />

                </session-factory>

</hibernate-configuration>

 

ssh中使用時,hibernate.cfg.xml是由Spring配置文件中的sessionFactory讀取,以下:

<bean id="sessionFactory"

                                class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

                                <property name="dataSource" ref="dataSource" />

 

                                <property name="configLocation">

<value>classpath:hibernate.cfg.xml</value><!-- hibernate.cfg.xml 放在src-->

<value>/WEB-INF/hibernate.cfg.xml</value><!-- hibernate.cfg.xml 放在WEB-INF-->

                                </property>

                </bean>

ssh中使用時,還須要在web.xml配置以下:

<filter>

                                <filter-name>hibernateFilter</filter-name>

                                <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>

                </filter>

                <filter-mapping>

                                <filter-name>hibernateFilter</filter-name>

                                <url-pattern>/*</url-pattern>

                </filter-mapping>

 

Hibernate單獨使用時, hibernate.cfg.xml的配置以下:

<!DOCTYPE hibernate-configuration PUBLIC

          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

 

<hibernate-configuration>

                <session-factory>

                                <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>

                                <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost :1521:orcl</property>

                                <property name="hibernate.connection.username">hr</property>

                                <property name="hibernate.connection.password">orcl</property>

 

                                <property name="hibernate.connection.pool.size">20 </property>

                                <property name="hibernate.show_sql">true </property>

                                <property name="jdbc.fetch_size">50 </property>

                                <property name="jdbc.batch_size">23 </property>

                                <property name="jdbc.use_scrollable_resultset">false </property>

                                <property name="Connection.useUnicode">true </property>

                               

<!--指定映射文件-->

                                <mapping resource="com/neusoft/test/entity/User.hbm.xml" />

                </session-factory>

</hibernate-configuration>

 

Hibernate單獨使用時,Hibernate.cfg.xml路徑問題:

Hibernate.cfg.xml路徑默認的是在src下,使用時以下

SessionFactory sessionFactory= new Configuration().configure().buildSessionFactory();

//默認訪問src/Hibernate.cfg.xml

SessionFactory sessionFactory= new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();

//上面倆個寫法是同樣的,都是訪問src/Hibernate.cfg.xml

SessionFactory sessionFactory= new Configuration().configure(/cfg/hibernate.cfg.xml).buildSessionFactory();

                                                                //此方法是訪問src/cfg/Hibernate.cfg.xml

Session session = sessionFactory.openSession();

放在WEB-INF ,使用時以下:

使用絕對路徑

File file = new File("E:/Documents and Settings/Administrator/Workspaces/MyEclipse 8.6/hibernate/WebRoot/WEB-INF/hibernate.cfg.xml");

SessionFactory sessionFactory= new Configuration().configure(file).buildSessionFactory();

相對路徑,下面的方法還沒調出來

SessionFactory sessionFactory = new Configuration().configure("WEB-INF/hibernate.cfg.xml").buildSessionFactory();

Session session = sessionFactory.openSession();

總結:hibernate單獨使用時,Hibernate.cfg.xml最好放在src

相關文章
相關標籤/搜索