引用
jackwang1 的 Struts2+spring2.5+jpa(hibernate)+proxoolStruts2+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ì)找不到我們在applicationContext.xml所提供的數(shù)據(jù)源的別名,即:proxool.DbPool,無法管理數(shù)據(jù)庫連接池了。于是只好放棄這種做法,某位大人說自己寫Listener,這個(gè)也是個(gè)不錯(cuò)的解決方法,其實(shí)我是不建議修改人家源碼的,因?yàn)檫@樣不通用!經(jīng)過多次測試終于找到了如下的一種配置方案:
1 準(zhǔn)備:Struts2+spring2.5+jpa(hibernate)+proxool的必要jar包你得有吧,對了,我連接的是oracle數(shù)據(jù)庫
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.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">
<!-- 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.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/contexthttp://www.springframework.org/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.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistencehttp://java.sun.com/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.sourceforge.net/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ì)列中等候的最大請求數(shù),超過這個(gè)請求數(shù)的用戶連接就不會(huì)被接受-->
<maximum-new-connections>20</maximum-new-connections>
<!-- 最少保持的空閑連接數(shù)-->
<prototype-count>5</prototype-count>
<!-- 允許最大連接數(shù),超過了這個(gè)連接,再有請求時(shí),就排在隊(duì)列中等候,最大的等待請求數(shù)由maximum-new-connections決定-->
<maximum-connection-count>100</maximum-connection-count>
<!-- 最小連接數(shù)-->
<minimum-connection-count>10</minimum-connection-count>
</proxool>
</something-else-entirely>
至此 所有的配置文件建立完成,測試程序
package spring;
imp
ort java.util.List; imp
ort javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query; imp
ort org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional; imp
ort 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;
}}
有興趣的話 自己測試下吧
聯(lián)系客服