您的位置:首页 > 编程语言 > Java开发

Struts2 + Spring3 + Hibernate3.6(JPA2) + Proxool9 集成环境搭建

2011-02-23 22:10 766 查看
注意事项:

1、不要使用Hibernate自带的cglib-2.2.jar,听网上说会与Spring的冲突,可用cglib-nodep-2.2.jar代替

2、如果使用MSSQL2000数据库,除了它的驱动包外还要下载jtds-1.2.5.jar,因为Hibernate在管理MSSQL2000需要,不然会访问不到数据库

3、Hibernate3会调用slf4j-log4j12-1.6.1.jar中的类,但官网上下的Hibernate3中没有带

4、使用AOP所需要的JAR包aopalliance-1.0.jar,aspectjrt-1.5.3.jar,aspectjweaver-1.5.3.jar,commons-logging-1.1.1.jar

相关配置文件设置

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"> <!-- 默认读取log4j配置 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>

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

<!-- 默认读取spring配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>

<!-- 设置spring applicationContext监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 刷新Introspector防止内存泄露 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>

<!-- 针对JPA的事务进行设置,保证事务的开启与关闭 -->
<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>

<!-- 利用spring设置字符集过滤,防止页面乱码 -->
<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>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 设置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>

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


applicationContext-common.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" default-lazy-init="true">

<!-- 自动装载EntityManager -->
<context:annotation-config />

<!-- 自动装载aop -->
<aop:aspectj-autoproxy />

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

<!-- 配置EntityManagerFactory -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
<property name="persistenceUnitName" value="persistenceUnit" />
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
</bean>

<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<!-- 配置具体方法及事务参数 -->
<tx:advice id="adviceTransaction" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>

<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
</beans>


proxool.xml(proxool连接池配置文件,文件中不能有中文,不能有注释,编码建设用utf-8,最好手工输入,复制的话注意是否有不可见字符,文件放在src文件夹下)

<?xml version="1.0" encoding="UTF-8"?>
<something-else-entirely>
<proxool>
<alias>MSSQLPool</alias>
<driver-url>jdbc:jtds:sqlserver://localhost:1433;DatabaseName=PaymentMgr;SelectMethod=Cursor;</driver-url>
<driver-class>net.sourceforge.jtds.jdbc.Driver</driver-class>
<driver-properties>
<property name="user" value="sa" />
<property name="password" value="1234" />
</driver-properties>
<house-keeping-sleep-time>9000</house-keeping-sleep-time>
<prototype-count>5</prototype-count>
<maximum-connection-count>100</maximum-connection-count>
<minimum-connection-count>10</minimum-connection-count>
</proxool>
</something-else-entirely>


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">

<hibernate-configuration>
<session-factory>
<!-- 数据库方言设置 -->
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- 后台打印执行的SQL语句 -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>

<property name="hibernate.connection.provider_class">org.hibernate.connection.ProxoolConnectionProvider</property>
<property name="hibernate.proxool.pool_alias">MSSQLPool</property>
<!-- 调用连接池配置文件 -->
<property name="hibernate.proxool.xml">proxool.xml</property>
<!-- 默认调用已存在的连接池 -->
<!--<property name="hibernate.proxool.existing_pool">true</property>-->
<!-- 默认自动连接 -->
<property name="connection.autoReconnect">true</property>
<property name="connection.autoReconnectForPools">true</property>
<property name="connection.is-connection-validation-required">true</property>
</session-factory>
</hibernate-configuration>


src/META-INF/persistence.xml(Hibernate3.6文档规定)

<?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/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0">
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.ejb.cfgfile" value="hibernate.cfg.xml"/>
</properties>
</persistence-unit>
</persistence>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: