乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      引用 Struts2+spring2.5+jpa(hibernate)+proxool

       CevenCheng 2010-09-16

      引用

      jackwang1 的 Struts2+spring2.5+jpa(hibernate)+proxool

      Struts2+spring2.5+jpa(hibernate)這種組合大家在網(wǎng)上看的很多了,不過他們大多數(shù)的數(shù)據(jù)源用的是dbcp,或是cp30,也找了一些用proxool做數(shù)據(jù)源的,不過配置完似乎總有問題

      例如 如果把數(shù)據(jù)源配置在applicationContext.xml里,則應(yīng)用在啟動(dòng)的時(shí)候會(huì)報(bào)“Attempt to refer to a unregistered pool by its alias” 這是因?yàn)?/font>struts2與spring進(jìn)行整合時(shí),web.xml中的spring加載必須使listener來加載,如果使用ContextLoderServlet,則會(huì)出空指向異常,報(bào)的是Struts2的objectFactory中的某處,因?yàn)榻^大多數(shù)WEB容器的加載順序是:Listener,Filter,Servlet,所以將會(huì)現(xiàn)struts2在spring前加載,它就找不到spring的管理容器,產(chǎn)生異常,解決辦法使用ContextLoderListener來加載spring,好,這樣一來又有問題了(proxool在web.xml中的配置采用servlet加載,比Listener啟動(dòng)的晚),spring出異常了,它又會(huì)找不到我們?cè)赼pplicationContext.xml所提供的數(shù)據(jù)源的別名,即:proxool.DbPool,無法管理數(shù)據(jù)庫(kù)連接池了。于是只好放棄這種做法,某位大人說自己寫Listener,這個(gè)也是個(gè)不錯(cuò)的解決方法,其實(shí)我是不建議修改人家源碼的,因?yàn)檫@樣不通用!經(jīng)過多次測(cè)試終于找到了如下的一種配置方案:

      1 準(zhǔn)備:Struts2+spring2.5+jpa(hibernate)+proxool的必要jar包你得有吧,對(duì)了,我連接的是oracle數(shù)據(jù)庫(kù)

      2 開發(fā)工具:myeclipse6.5

      3 新建工程,作必要的準(zhǔn)備工作,copy jar包,我只把主要的配置文件列出來:

        1)web.xml

         <?xml version="1.0" encoding="UTF-8"?>
      <web-app version="2.5" xmlns="http://java./xml/ns/javaee"
       xmlns:xsi="http://www./2001/XMLSchema-instance"
       xsi:schemaLocation="http://java./xml/ns/javaee 
       http://java./xml/ns/javaee/web-app_2_5.xsd">
        <!-- Include this if you are using Hibernate 
        <filter>
        <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
        <filter-class>
        org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
        </filter-class>
        </filter>  
        <filter-mapping>
        <filter-name>Spring OpenEntityManagerInViewFilter</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>
        <listener>
        <listener-class>
         org.springframework.web.context.ContextLoaderListener
        </listener-class>
       </listener> 
       <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
       </welcome-file-list>
      </web-app>

      2)在web.xml同級(jí)目錄建立applicationContext.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www./schema/beans"
       xmlns:xsi="http://www./2001/XMLSchema-instance"
       xmlns:jee="http://www./schema/jee"
       xmlns:tx="http://www./schema/tx"
       xmlns:context="http://www./schema/context"
       xsi:schemaLocation="http://www./schema/beanshttp://www./schema/beans/spring-beans-2.5.xsd http://www./schema/txhttp://www./schema/tx/spring-tx-2.5.xsd http://www./schema/jeehttp://www./schema/jee/spring-jee-2.5.xsd http://www./schema/contexthttp://www./schema/context/spring-context-2.5.xsd"
       default-lazy-init="true">
       <bean
        class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
       <!-- 連接池 -->
       <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"></property>
        <property name="persistenceUnitName" value="punit"></property> <!--value 與persistence.xml中的 unitname相同-->
       </bean>

       <bean id="transactionManager"
        class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory"
         ref="entityManagerFactory" />
       </bean>

       <tx:annotation-driven transaction-manager="transactionManager"/>
       <context:component-scan base-package="spring" />
      </beans>

      3)在src文件夾建立META-INF,在該文件夾建persistence.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <persistence xmlns="http://java./xml/ns/persistence"
       xmlns:xsi="http://www./2001/XMLSchema-instance"
       xsi:schemaLocation="http://java./xml/ns/persistencehttp://java./xml/ns/persistence/persistence_1_0.xsd"
       version="1.0">
       <persistence-unit name="punit" transaction-type="RESOURCE_LOCAL">
       <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>    
         <property name="hibernate.ejb.cfgfile" value="hibernate.cfg.xml"/>
        </properties>
       </persistence-unit>
      </persistence>
      4)在src文件夾建立hibernate.cfg.xml

      <?xml version='1.0' encoding='UTF-8'?>
      <!DOCTYPE hibernate-configuration PUBLIC
                "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                "http://hibernate./hibernate-configuration-3.0.dtd">

      <!-- Generated by MyEclipse Hibernate Tools.                   -->
      <hibernate-configuration>

         <session-factory>
              <!-- Database connection settings -->
              <property name="hibernate.connection.provider_class">org.hibernate.connection.ProxoolConnectionProvider</property>
              <property name="hibernate.proxool.pool_alias">test</property>
              <property name="hibernate.proxool.xml">proxool.xml</property>
              <!-- SQL dialect -->
              <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
              <!-- Echo all executed SQL to stdout -->
              <property name="show_sql">false</property>
              <!-- Drop and re-create the database schema on startup - create-drop-->
              <property name="hbm2ddl.auto">update</property> 
              <mapping class="entity.Person"/>
          </session-factory>  
      </hibernate-configuration>

      5)在src文件建立proxool.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <something-else-entirely>
       <proxool>
        <alias>test</alias>
        <!--proxool只能管理由自己產(chǎn)生的連接-->
        <driver-url>
         jdbc:oracle:thin:@127.0.0.1:1521:orcl
        </driver-url>
        <driver-class>
         oracle.jdbc.OracleDriver
        </driver-class>
        <driver-properties>
         <property name="user" value="book" />
         <property name="password" value="book" />
        </driver-properties>
        <!-- proxool自動(dòng)偵察各個(gè)連接狀態(tài)的時(shí)間間隔(毫秒),偵察到空閑的連接就馬上回收,超時(shí)的銷毀-->
        <house-keeping-sleep-time>90000</house-keeping-sleep-time>
        <!-- 指因未有空閑連接可以分配而在隊(duì)列中等候的最大請(qǐng)求數(shù),超過這個(gè)請(qǐng)求數(shù)的用戶連接就不會(huì)被接受-->
        <maximum-new-connections>20</maximum-new-connections>
        <!-- 最少保持的空閑連接數(shù)-->
        <prototype-count>5</prototype-count>
        <!-- 允許最大連接數(shù),超過了這個(gè)連接,再有請(qǐng)求時(shí),就排在隊(duì)列中等候,最大的等待請(qǐng)求數(shù)由maximum-new-connections決定-->
        <maximum-connection-count>100</maximum-connection-count>
        <!-- 最小連接數(shù)-->
        <minimum-connection-count>10</minimum-connection-count>
       </proxool>
      </something-else-entirely>

       

      至此 所有的配置文件建立完成,測(cè)試程序

      package spring;

      import java.util.List;

      import javax.persistence.EntityManager;
      import javax.persistence.PersistenceContext;
      import javax.persistence.Query;

      import org.springframework.stereotype.Component;
      import org.springframework.transaction.annotation.Transactional;

      import entity.Person;

      @Transactional
      @Component
      public class LoginServiceImpl implements LoginService {
       @PersistenceContext
       private EntityManager em;

       // public void setEntityManager(EntityManager em) {
       // this.em = em;
       // }
       @SuppressWarnings("unchecked")
       public boolean checkuser(String name, String password) {
        Query query = em
          .createQuery("select o from Person o where o.name=?1 and o.password=?2");
        query.setParameter(1, name);
        query.setParameter(2, password);
        List<Person> ls = query.getResultList();
        if (ls.size() > 0)
         return true;
        else
         return false;
       }

      }

      有興趣的話 自己測(cè)試下吧

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多