java - 如何在 JBoss 6.3 EAP 上部署 Spring 管理的 JPA 应用程序

标签 java spring hibernate jpa jboss

我有一个 Java Web 应用程序。该应用程序使用 JPA 进行持久化。持久性由 spring 管理。该应用程序可以轻松部署在 Tomcat 上,只需将 war 文件放入 webapps 中即可。但在 JBoss 中部署这个应用程序对我来说成了连续几天的噩梦。我在此网站上找不到合适的帖子来解决该问题,因此发表了这篇文章。

最佳答案

首先,你需要配置一个数据源(我使用的是mysql数据库)。我的 JBoss 安装在 C:\jboss-eap-6.3\ 下而且我用的是windows操作系统。 脚步: 1、创建目录结构com\mysql\mainC:\jboss-eap-6.3\modules 。你最终会得到 C:\jboss-eap-6.3\modules\com\mysql\main目录结构。 2.创建xml文件module.xml在这个main目录。另外掉落mysql驱动程序 jar mysql-connector-java-5.1.23-bin.jar在同一目录中。最后你会得到module.xmlmysql-connector-java-5.1.23-bin.jarC:\jboss-eap-6.3\modules\com\mysql\main 。 3.复制以下xml内容为module.xml

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.0" name="com.mysql">
    <resources>
        <resource-root path="mysql-connector-java-5.1.23-bin.jar"/>
    </resources>
    <dependencies>
        <module name="javax.api"/>
        <module name="javax.transaction.api"/>
    </dependencies>
</module>

4.找到文件standalone.xmlC:\jboss-eap-6.3\standalone\configuration目录。使用您喜欢的文本编辑器打开此文件。找到datasource子系统,即这条线 <subsystem xmlns="urn:jboss:domain:datasources:1.2"> 。添加xml下面的片段位于drivers下元素。

<driver name="mysqlDriver" module="com.mysql">
    <xa-datasource-class>com.mysql.jdbc.Driver</xa-datasource-class>
</driver>

添加 xml下面的片段位于datasources下元素:

<datasource jndi-name="java:jboss/datasources/MYSQLDATASOURCE" pool-name="MYSQLDATASOURCE" enabled="true" use-java-context="true">
    <connection-url>jdbc:mysql://localhost:3306/databaseName</connection-url>
    <driver>mysqlDriver</driver>
    <security>
    <user-name>root</user-name>
    <password>databasepassword</password>
    </security>
</datasource>   

此时您已完成 datasource配置。

下一步是配置您的 persistence.xmlapplicationContext.xmlpersistence.xml

<persistence version="2.0" 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">

    <persistence-unit name="erPU">
        <class>..</class> <!--Contains all your Entity classes-->
        <properties>            
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.format_sql" value="false" />
            <property name="jboss.as.jpa.providerModule" value="application" /> 
            <property name="jboss.as.jpa.managed" value="false" /> 
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>          
        </properties>
    </persistence-unit>    
</persistence>

<!--jboss.as.jpa.providerModule     is the name of the persistence provider module (default is org.hibernate). Should be hibernate3-bundled if Hibernate 3 jars are in the application archive (adapterModule and adapterClass will automatically be set for hibernate3-bundled).  Should be application, if a persistence provider is packaged with the application. -->
<!--jboss.as.jpa.managed    can be set to false to disable container managed JPA access to the persistence unit.  The default is true, which enables container managed JPA access to the persistence unit.  This is typically set to false for Seam 2.x + Spring applications. -->

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:context="http://www.springframework.org/schema/context"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:tx="http://www.springframework.org/schema/tx"    

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
          http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
">

    <context:annotation-config />   
    <context:component-scan base-package="..." /><!--Package base name to scan for annotations -->        

    <jee:jndi-lookup id="dataSource" jndi-name="java:jboss/datasources/MYSQLDATASOURCE" expected-type="javax.sql.DataSource"/>    
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />  
        <property name="packagesToScan" value="..." /><!--Packages to scan for entities--> 
        <property name="persistenceUnitName" value="erPU" />
        <property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml"/> 
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="false" />
            </bean>
        </property>
        <property name="jpaPropertyMap">
            <map>
                <entry key="hibernate.hbm2ddl.auto" value="update" />
                <entry key="hibernate.format_sql" value="false" />
                <entry key="hibernate.show_sql" value="false" />
                <entry key="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            </map>
        </property>
    </bean>       
    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>   
</beans>

注意:无论 persistence.xml 的位置如何,你需要告诉 spring persistence.xml 在哪里位于。 literature below was extracted from

Using Spring-managed persistence units

Spring applications running in JBoss AS7 may also create persistence units on their own, using the LocalContainerEntityManagerFactoryBean. This is what these applications need to consider:
Placement of the persistence unit definitions

When the application server encounters a deployment that has a file named META-INF/persistence.xml (or, for that matter, WEB-INF/classes/META-INF/persistence.xml), it will attempt to create a persistence unit based on what is provided in the file. In most cases, such definition files are not compliant with the Java EE requirements, mostly because required elements such as the datasource of the persistence unit are supposed to be provided by the Spring context definitions, which will fail the deployment of the persistence unit, and consequently of the entire deployment.

Spring applications can easily avoid this type of conflict, by using a feature of the LocalContainerEntityManagerFactoryBean which is designed for this purpose. Persistence unit definition files can exist in other locations than META-INF/persistence.xml and the location can be indicated through the persistenceXmlLocation property of the factory bean class.

Assuming that the persistence unit is in the META-INF/jpa-persistence.xml, the corresponding definition can be:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
       <property name="persistenceXmlLocation" value="classpath*:META-INF/jpa-persistence.xml"/> 
       <!-- other definitions -->
</bean>

WEB-INF/jboss-deployment-struct.xml 最后,您需要创建jboss-deployment-structure.xmlWEB-INF 。文件的内容应该是:

<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.0">
  <deployment>
    <exclusions>
       <module name="org.hibernate"/>
    </exclusions>
  </deployment>
</jboss-deployment-structure>

原因是:

Since the LocalContainerEntityManagerFactoryBean and the corresponding HibernateJpaVendorAdapter are based on Hibernate 3, it is required to use that version with the application. Therefore, the Hibernate 3 jars must be included in the deployment. At the same time, due the presence of @PersistenceUnit or @PersistenceContext annotations on the application classes, the application server will automatically add the 'org.hibernate' module as a dependency.

This can be avoided by instructing the server to exclude the module from the deployment's list of dependencies.

最后,与您的应用程序捆绑的 JPA 库必须是版本 3.x。

关于java - 如何在 JBoss 6.3 EAP 上部署 Spring 管理的 JPA 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26624036/

相关文章:

java - 通过超链接预填充 iPhone 日历事件

java - 具有不同功能的 Spring REST Controller

java - 域对象中的持久性注释是一种不好的做法吗?

java - JPA AttributeConverter 是否有可能知道有关 Entity 正在运行的任何信息?

mysql - jpa/spring/hibernate中的简单乐观锁问题

mysql - 如何通过外键hql排序

java - Tomcat 用户具有主要角色,但 request.isUserInRole() 另有说明

java - 将 Tomcat(使用 Liferay)配置为 2012R2 服务时出现异常

java - 用另一组字母检查单词中字母的算法和数据结构

java - Spring SAML 示例应用程序返回 Could not initialize class org.apache.commons.ssl.TrustMaterial