Java 野蝇与资源排斥

Java 野蝇与资源排斥,java,spring,jpa,wildfly,Java,Spring,Jpa,Wildfly,我手头有一个wildflywar应用程序,它依赖于一个jar,它用Spring启动自己的JPA持久性容器。这个jar有自己的persistence.xml,只有一个持久性单元。 在嵌入式jar中,由于只有一个持久性单元,因此使用@PersistenceContext注入它,而不使用单元名称。此外,具有这些字段的对象不是EJB,它们是简单的Springbean 另一方面,应用程序还有一个带有两个持久性单元的persistence.xml。 应用程序的代码具有带有适当的unitName的@Persi

我手头有一个wildflywar应用程序,它依赖于一个jar,它用Spring启动自己的JPA持久性容器。这个jar有自己的
persistence.xml
,只有一个持久性单元。 在嵌入式jar中,由于只有一个持久性单元,因此使用
@PersistenceContext
注入它,而不使用单元名称。此外,具有这些字段的对象不是EJB,它们是简单的Springbean

另一方面,应用程序还有一个带有两个持久性单元的
persistence.xml
。 应用程序的代码具有带有适当的
unitName
@PersistenceContext
字段,这是必需的,因为其中有两个字段

现在,当我启动我的应用程序时,Wildfly(特别是jpa模块)会扫描jar中的代码,并在
@PersistenceContext
上阻塞,而没有我的SpringBean的单元名:

DEBUG [org.jboss.as.jpa.messages] (MSC service thread 1-1) persistence unit search for unitName=null referenced from class=the.spring.Bean (annotation=@javax.persistence.PersistenceContext() on void the.spring.Bean.setEntityManager(javax.persistence.EntityManager))
为了解决这个问题,我想排除jar:在TomEE中,有一种方法可以明确地将jar排除在扫描之外,但我在WildFly中找不到类似的功能。 我尝试了以下配置,但没有成功:

  • jboss deployment structure.xml
    已读取但没有明显效果:

    <jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
      <deployment>
        <exclusions>
          <module name="deployment.jee-app.war.spring-app.jar" />
          <!-- I have also tried the following -->
          <!-- <module name="jee-app.war.spring-app.jar" /> -->
          <!-- <module name="spring-app.jar" /> -->
        </exclusions>
      </deployment>
    </jboss-deployment-structure> 
    
这两个文件都在我的war应用程序的WEB-INF文件夹中

jee-app.war
  ├── WEB-INF
  │   ├── classes
  |   │   ├── META-INF
  |   │   |    └── persistence.xml _____________ # 2 PUs here
  |   |   └── < lots of packages >
  |   ├── lib
  |   |   ├── spring-app.jar ___________________ # to EXCLUDE from scanning !!!
  |   |   |   ├── META-INF
  |   |   |   |   └── persistence
  |   |   |   |       └── persistence.xml ______ # 1 PU here, loaded by Spring
  |   |   |   ├── application.xml ______________ # Spring definition
  |   |   |   └── the
  |   |   |       └── spring
  |   |   |           └── Bean.class
  |   |   └── < lots of other jars > 
  |   ├── jboss-deployment-structure.xml
  |   ├── jboss-scanning.xml
  |   └── web.xml
  └── < some JSPs >
jee-app.war
├── WEB-INF
│   ├── 班级
|   │   ├── META-INF
|   │   |    └── persistence.xml uuuuuuuuuuuuuuuuuuuuu35; 2 PUs在这里
|   |   └── < 很多套餐>
|   ├── 解放党
|   |   ├── spring-app.jar uuuuuuuuuuuuuuuuuuuuuuuuuu35;从扫描中排除!!!
|   |   |   ├── META-INF
|   |   |   |   └── 坚持不懈
|   |   |   |       └── persistence.xml这里是1pu,由Spring加载
|   |   |   ├── application.xml uuuuuuuuuuuuuuuuuuuu35;弹簧定义
|   |   |   └── 这个
|   |   |       └── 春天
|   |   |           └── Bean.class
|   |   └── < 许多其他罐子>
|   ├── jboss-deployment-structure.xml
|   ├── jboss-scanning.xml
|   └── web.xml
└── < 一些JSP>
当然,我不能修改spring-app.jar(太容易了…)

不用说,我花了相当多的时间在WildFly文档和Google上,除了排除子系统或覆盖WildFly的本地模块外,找不到其他任何东西

谢谢你的帮助

胡安·曼努埃尔

编辑:根据请求,以下是persistence.xml文件

jee-app.war中的一个:

<?xml version="1.0" encoding="UTF-8"?>
<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="PU1" transaction-type="JTA">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <jta-data-source>java:jboss/datasources/DS1</jta-data-source>
    <class>...</class> <!-- a lot of classes included by name !-->
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
      <property name="hibernate.hbm2ddl.auto" value="update" />
      <property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.EhCacheProvider" />
      <property name="hibernate.show_sql" value="true" />
      <property name="hibernate.format_sql" value="false" />
    </properties>
  </persistence-unit>

  <persistence-unit name="PU2" transaction-type="JTA">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <jta-data-source>java:jboss/datasources/DS2</jta-data-source>
    <class>...</class> <!-- a lot of classes included by name !-->
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
      <property name="hibernate.hbm2ddl.auto" value="update" />
      <property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.EhCacheProvider" />
      <property name="hibernate.show_sql" value="true" />
      <property name="hibernate.format_sql" value="false" />
    </properties>
  </persistence-unit>
</persistence>

org.hibernate.jpa.HibernatePersistenceProvider
java:jboss/datasources/DS1
... 
真的
org.hibernate.jpa.HibernatePersistenceProvider
java:jboss/datasources/DS2
... 
真的
spring-app.jar中的那个

<?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" version="1.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
  <persistence-unit name="xa-rm">

    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <mapping-file>META-INF/persistence/orm.xml</mapping-file>

    <exclude-unlisted-classes>false</exclude-unlisted-classes>

  </persistence-unit>

org.hibernate.ejb.HibernatePersistence
META-INF/persistence/orm.xml
假的

你能粘贴persistence.xml吗?我用persistence.xml文件的content@JuanManuel你能解决这个问题吗?因为我们遇到了一个类似的问题:对不起,我最近没有经常来这里:/n问题到目前为止还没有解决,我们不得不重新包装有问题的罐子。。。非常麻烦。。。
<?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" version="1.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
  <persistence-unit name="xa-rm">

    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <mapping-file>META-INF/persistence/orm.xml</mapping-file>

    <exclude-unlisted-classes>false</exclude-unlisted-classes>

  </persistence-unit>