ssh中整合spring和hibernate之後的application.xml檔案的超詳細配置

南夏發表於2015-12-02
<?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:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx.xsd">





<!-- 引入外部jdbc.properties檔案 -->
<context:property-placeholder location="classpath:jdbc.properties"/>

<!-- 配置連線池的資訊 c3p0連線池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  <!-- 資料庫連線的四個基本引數 -->
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>

<property name="password" value="${jdbc.password}"/>

<!-- 其他配置 -->
<!--初始化時獲取三個連線,取值應在minPoolSize與maxPoolSize之間。Default: 3 -->
<property name="initialPoolSize" value="10"></property>
<!--連線池中保留的最小連線數。Default: 3 -->
<property name="minPoolSize" value="3"></property>
<!--連線池中保留的最大連線數。Default: 15 -->
<property name="maxPoolSize" value="5"></property>
<!--當連線池中的連線耗盡的時候c3p0一次同時獲取的連線數。Default: 3 -->
<property name="acquireIncrement" value="3"></property>
<!-- 控制資料來源內載入的PreparedStatements數量。如果maxStatements與maxStatementsPerConnection均為0,則快取被關閉。Default: 0 -->
<property name="maxStatements" value="8"></property>
<!--maxStatementsPerConnection定義了連線池內單個連線所擁有的最大快取statements數。Default: 0 -->
<property name="maxStatementsPerConnection" value="5"></property>
<!--最大空閒時間,1800秒內未使用則連線被丟棄。若為0則永不丟棄。Default: 0 -->
<property name="maxIdleTime" value="1800"></property>

</bean>




<!-- 如果使用的是繼承的 HbiernateDaoSupport( XXXDaoImpl extends HbiernateDaoSupport implements XXXDao)的方式來呼叫sesseionfactory,還需要配置HibernateTemplate模板, (注意:這裡注入的是sessionFactory) 

<code class="hljs applescript has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><bean <span class="hljs-property" style="box-sizing: border-box;">id</span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"hibernateTemplate"</span> <span class="hljs-type" style="box-sizing: border-box;">class</span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"org.springframework.orm.hibernate3.HibernateTemplate"</span>>
  <<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">property</span> <span class="hljs-property" style="box-sizing: border-box;">name</span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"sessionFactory"</span>>
    <<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">ref</span> bean=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"sessionFactory"</span> />
  </<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">property</span>>
</bean><span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);">	</span></code>

java程式碼中用法 
1、j繼承父類 HbiernateDaoSupport( XXXDaoImpl extends HbiernateDaoSupport implements XXXDao) 
2、方法中用this.getHibernateTemplate()獲得HibernateTemplate(自動開啟了session,不用再用sessionfactory建立session)。 
3、另還可以用注入的方式用dao,在applicationContext中注入

<code class="hljs applescript has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><bean <span class="hljs-property" style="box-sizing: border-box;">id</span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"XXXDao"</span> <span class="hljs-type" style="box-sizing: border-box;">class</span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"spring.dao.impl.XXXDaoImpl"</span>>
  <<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">property</span> <span class="hljs-property" style="box-sizing: border-box;">name</span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"XXXTemplate"</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">ref</span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"XXXTemplate"</span>></<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">property</span>>
</bean></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

private HibernateTemplate XXXTemplate; 
get。set方法

XXXTemplate.save(obj)

-->

<!-- 配置Hibernate的相關屬性 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 注入連線池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置Hibernate的其他的屬性 -->
<property name="hibernateProperties">
<props>
<!-- Hibernate的方言 -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.connection.autocommit">false</prop>
</props>
</property>
<!-- 配置對映檔案 -->
<property name="mappingResources">
<list>
<value>cn/itcast/shop/user/User.hbm.xml</value>
<value>cn/itcast/shop/category/Category.hbm.xml</value>
<value>cn/itcast/shop/product/Product.hbm.xml</value>
<value>cn/itcast/shop/categorysecond/CategorySecond.hbm.xml</value>
<value>cn/itcast/shop/order/Order.hbm.xml</value>
<value>cn/itcast/shop/order/OrderItem.hbm.xml</value>
<value>cn/itcast/shop/adminuser/AdminUser.hbm.xml</value>
</list>
</property>

</bean>

<!-- 宣告式事務管理 -->
<!-- l利用註解的方式配置事務管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<!-- 注入 sessionFactory-->
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>


<!-- 
//非註解的方式建立事務管理器
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>


  配置事務傳播特性
<tx:advice id="TestAdvice" transaction-manager="txManager">
    <tx:attributes>
      <tx:method name="save*" propagation="REQUIRED"/>
      <tx:method name="del*" propagation="REQUIRED"/>
      <tx:method name="update*" propagation="REQUIRED"/>
      <tx:method name="add*" propagation="REQUIRED"/>
      <tx:method name="find*" propagation="REQUIRED"/>
      <tx:method name="get*" propagation="REQUIRED"/>
      <tx:method name="t*" propagation="REQUIRED"/>
       <tx:method name="in*" propagation="REQUIRED"/>
    </tx:attributes>
</tx:advice>

 配置參與事務的類
<aop:config>
<aop:pointcut id="allTestServiceMethod" expression="execution(* com.itcast.oa.service.*.*(..))"/>
<aop:advisor pointcut-ref="allTestServiceMethod" advice-ref="TestAdvice" />
</aop:config>
 -->


<!--

<!-- 自動掃描與裝配bean,通過 註解的方式依賴注入-->
<context:component-scan base-package="com.itcast.oa"></context:component-scan>

-->


<!-- 配置Action================================== -->
<bean id="indexAction" class="cn.itcast.shop.index.IndexAction" scope="prototype">
<property name="categoryService" ref="categoryService"/>
<property name="productService" ref="productService"/>
</bean>


<bean id="userAction" class="cn.itcast.shop.user.UserAction" scope="prototype">
<property name="userService" ref="userService"/>
</bean>

<bean id="checkImg" class="cn.itcast.shop.user.CheckImgAction" scope="prototype"></bean>

<bean id="productAction" class="cn.itcast.shop.product.ProductAction" scope="prototype">
<property name="categoryService" ref="categoryService"/>
<property name="productService" ref="productService"/>
<property name="categorySecondService" ref="categorySecondService"/>
</bean>

<bean id="cartAction" class="cn.itcast.shop.cart.CartAction" scope="prototype">
<!-- 注入商品的Service -->
<property name="productService" ref="productService"/>
</bean>

<bean id="orderAction" class="cn.itcast.shop.order.OrderAction" scope="prototype">
<property name="orderService" ref="orderService"/>
</bean>

<bean id="adminUserAction" class="cn.itcast.shop.adminuser.AdminUserAction"  scope="prototype">
<property name="adminUserService" ref="adminUserService"/>
</bean>

<bean id="categoryAction" class="cn.itcast.shop.category.CategoryAction" scope="prototype">
<property name="categoryService" ref="categoryService"/>
</bean>

<bean id="categorySecondAction" class="cn.itcast.shop.categorysecond.CategorySecondAction" scope="prototype">
<property name="categorySecondService" ref="categorySecondService"/>
<property name="categoryService" ref="categoryService"></property>
</bean>

<!-- 配置Service================================== -->
<bean id="userService" class="cn.itcast.shop.user.UserService">
<property name="userDao" ref="userDao"/>
</bean>

<bean id="categoryService" class="cn.itcast.shop.category.CategoryService">
<property name="categoryDao" ref="categoryDao"/>
</bean>

<bean id="productService" class="cn.itcast.shop.product.ProductService">
<property name="productDao" ref="productDao"/>
</bean>

<bean id="orderService" class="cn.itcast.shop.order.OrderService">
<property name="orderDao" ref="orderDao"/>
</bean>

<bean id="adminUserService" class="cn.itcast.shop.adminuser.AdminUserService">
<property name="adminUserDao" ref="adminUserDao"/>
</bean>

<bean id="categorySecondService" class="cn.itcast.shop.categorysecond.CategorySecondService">
<property name="categorySecondDao" ref="categorySecondDao"/>
</bean>
<!-- 配置Dao================================== -->
<bean id="userDao" class="cn.itcast.shop.user.UserDao">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<bean id="categoryDao" class="cn.itcast.shop.category.CategoryDao">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<bean id="productDao" class="cn.itcast.shop.product.ProductDao">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<bean id="orderDao" class="cn.itcast.shop.order.OrderDao">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<bean id="adminUserDao" class="cn.itcast.shop.adminuser.AdminUserDao">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<bean id="categorySecondDao" class="cn.itcast.shop.categorysecond.CategorySecondDao">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

</beans>










配置好application.xml之後還要在web.xml配置監聽器和各種過濾器

===========================================================================================================

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


<!-- 配置Spring的用於初始化容器物件的監聽器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>


<!--
用於做初始化工作的監聽器,一定要配置到Spring的ContextLoaderListener之後,因為要用到Spring的容器物件
-->
<listener>
<listener-class>com.itcast.oa.util.IniteLisener</listener-class>
</listener>

<!-- 配置Struts2的核心的過濾器 -->
<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>





<!-- 配置Spring的用於解決懶載入問題的過濾器 -->
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 解決的是延遲載入的問題

<filter>   
        <filter-name>OpenSessionInViewFilter</filter-name>   
        <filter-class>    org.springframework.orm.hibernate4.support.OpenSessionInViewFilter </filter-class>   
        指定在spring配置的sessionFactory  
        <init-param>   
            <param-name>sessionFactoryBeanName</param-name>   
            <param-value>sessionFactory</param-value>   
        </init-param>   
        singleSession預設為true,若設為false則等於沒用OpenSessionInView     
        <init-param>   
                <param-name>singleSession</param-name>   
                <param-value>true</param-value>   
        </init-param>   
     </filter> -->



<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

相關文章