我有一個java web應用程序。此應用程序使用JPA進行持久化。持久性由Spring管理。此應用程序可以輕鬆部署在Tomcat
上,只需將war
文件放入webapps
即可。但是在JBoss
中部署這個應用程序幾天後對我來說成了一場噩夢。我無法在此網站上找到合適的帖子來解決此問題。如何在JBoss 6.3上部署Spring管理的JPA應用程序EAP
回答
首先,你需要配置一個數據源(我使用的是mysql數據庫)。我的JBoss安裝在C:\jboss-eap-6.3\
下,我用的是windows操作系統。 步驟: 1.在C:\jboss-eap-6.3\modules
下創建一個目錄結構com\mysql\main
。您將以C:\jboss-eap-6.3\modules\com\mysql\main
目錄結構結束。 2.在main
目錄中創建一個xml
文件module.xml
。另外在同一個目錄中放入mysql
驅動程序jar mysql-connector-java-5.1.23-bin.jar
。最後,您將在C:\jboss-eap-6.3\modules\com\mysql\main
中有module.xml
和mysql-connector-java-5.1.23-bin.jar
。 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.找到C:\jboss-eap-6.3\standalone\configuration
目錄中的文件standalone.xml
。用你最喜歡的文本編輯器打開這個文件。找到datasource
子系統,即此行<subsystem xmlns="urn:jboss:domain:datasources:1.2">
。在drivers
元素下添加xml
片段。
<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.xml
和applicationContext.xml
。 的persistence.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
的位置,你需要告訴春天在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的部署,structure.xml 最後,您需要WEB-INF
下創建jboss-deployment-structure.xml
。該文件的內容應該是:
<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
- 1. 在jboss eap上部署maven,spring應用程序7
- 2. 在JBOSS EAP 6.3上部署REST Webservice時出現錯誤
- 3. 如何在JBoss SOA 5.x上部署JPA 2應用程序?
- 4. JBOSS EAP 6.3支持spring + websocket嗎?
- 5. 從遠程客戶端部署在Jboss Eap 6.3上的查找Ejb 3.0
- 6. jboss 6.3 eap linux環境下的ear部署問題
- 7. 在jboss上部署應用程序
- 8. 在JBoss上部署Web應用程序
- 9. 如何在jboss EAP 6.3.0 GA中使用管理控制檯重新部署應用程序?
- 10. 在JBoss 6.x上啓用Java安全管理器時無法部署應用程序EAP
- 11. 錯誤部署WAR文件到JBoss EAP 6.3
- 12. 的JBoss 6.3管理應用程序不能識別MySQL驅動
- 13. JBoss EAP 6.3崩潰System.out.println
- 14. Camel Bindy應用程序JBoss EAP 6.1部署問題
- 15. 無法在jboss上部署spring/hibernate/GWT應用程序
- 16. JBOSS EAP 6.2.4上的Spring Boot REST API部署
- 17. 如何在Eclipse中重新部署Jboss上的應用程序?
- 18. 在Heroku上部署Spring應用程序
- 19. JBoss EAP 6.3的最高Infinispan版本
- 20. 在Jboss EAP 6.4中部署EJB 2.0
- 21. 與JBoss上Openshift JPA版本衝突的Spring應用程序
- 22. 如何在Jboss v6上使用Spring + EclipseLink jpa運行Web應用程序
- 23. 將基於JSF 1.2的Web應用程序部署到JBoss EAP 7.0
- 24. 如何在JBOSS EAP 6.3的LdapExtLoginModule中配置角色組?
- 25. JBoss EAP 6.3/Castor 1.0 - MappingException:找不到類
- 26. Intellij IDEA/JBoss EAP 6.3配置被覆蓋
- 27. JBoss EAP 6.3 + cxf 2.7.11 jax-rs導致
- 28. JBoss EAP 6.3 EJB定時器持久性
- 29. 如何在Weblogic 12c(12.1.3)上部署Spring Boot應用程序?
- 30. JBoss EAP 6.2刪除管理用戶
它是顯示一些錯誤,或者你只是不知道在哪裏部署? – 2014-10-29 06:36:32