【问题标题】:Wildfly can't find persistence units to inject into CDI managed beanWildfly 找不到要注入 CDI 托管 bean 的持久性单元
【发布时间】:2020-03-17 13:46:50
【问题描述】:

我有一个在 Java 8 和 Wildfly 10 中运行良好的应用程序。Postgres 数据库的驱动程序是 PostgreSQL JDBC Driver 42.2.8

更新到 Java 11 和 Wildfly 17 后,不再找到来自 persistence.xml 的不同持久性单元。消息是:

 Caused by: java.lang.IllegalArgumentException: WFLYWELD0037: Error injecting persistence unit into CDI managed bean. Can't find a persistence unit named 'my-persistence-unit' in demo-app.war for injection point private javax.persistence.EntityManager ProgramRepository.em

这是persistence.xml中持久性单元之一的定义:

<persistence-unit name="my-persistence-unit" transaction-type="JTA">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <jta-data-source>java:/jboss/datasources/my_datasource</jta-data-source>
    <class>com.demo-app.cloud.entity.config.CopyTextConfig</class>
    <class>com.demo-app.cloud.entity.config.SystemConfig</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
    <properties>
        <property name="javax.persistence.schema-generation.database.action" value="none"/>
    </properties>
</persistence-unit>

这是一个使用 PersistenceContext 注释注入持久性单元的类。 “程序”类借助注释@Entity 和@Table 映射数据库中的表:

@Dependent
public class ProgramRepository extends AbstractRepository<Program> {

    @PersistenceContext(unitName = "my-persistence-unit")
    private EntityManager em;

    @Override
    EntityManager entityManager() {
        return em;
    }

这是 Wildfly 的standalone.xml 中的数据源

    <datasources>
        <datasource jndi-name="java:jboss/datasources/my_datasource" pool-name="my_datasource">
            <connection-url>jdbc:postgresql://localhost:5432/my_database</connection-url>
            <driver-class>org.postgresql.Driver</driver-class>
            <driver>postgresql-42.2.8.jar</driver>
            <security>
                <user-name>xxxx</user-name>
                <password>xxxx</password>
            </security>
            <validation>
                <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLValidConnectionChecker"/>
                <background-validation>true</background-validation>
                <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLExceptionSorter"/>
            </validation>
        </datasource>
        <drivers>
            <driver name="h2" module="com.h2database.h2">
                <xa-datasource-class&gt;org.h2.jdbcx.JdbcDataSource</xa-datasource-class&gt;
            </driver>
        </drivers>
    </datasources>

最后,来自 Wildfly 的完整堆栈跟踪:

11:18:42,181 WARN  [org.jboss.as.jaxrs] (management-handler-thread - 1) WFLYRS0017: Failed to read attribute from JAX-RS deployment at [
    ("deployment" => "demo-app.war"),
    ("subsystem" => "jaxrs"),
    ("rest-resource" => "com.demo-app.cloud.api.system.ApiResource")
] with name "sub-resource-locators": java.lang.NullPointerException
    at org.wildfly.extension.undertow@17.0.1.Final//org.wildfly.extension.undertow.deployment.UndertowDeploymentService.getDeployment(UndertowDeploymentService.java:166)
    at org.jboss.as.jaxrs@17.0.1.Final//org.jboss.as.jaxrs.DeploymentRestResourcesDefintion$AbstractRestResReadHandler.execute(DeploymentRestResourcesDefintion.java:216)
    at org.jboss.as.controller@9.0.2.Final//org.jboss.as.controller.operations.global.ReadAttributeHandler.doExecuteInternal(ReadAttributeHandler.java:174)
    at org.jboss.threads@2.3.3.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1377)
    at java.base/java.lang.Thread.run(Thread.java:834)
    at org.jboss.threads@2.3.3.Final//org.jboss.threads.JBossThread.run(JBossThread.java:485)

11:18:42,408 ERROR [org.jboss.as.controller.management-operation] (management-handler-thread - 1) WFLYCTL0013: Operation ("read-attribute") failed - address: ([
    ("deployment" => "demo-app.war"),
    ("subsystem" => "jaxrs"),
    ("rest-resource" => "com.demo-app.cloud.api.system.ApiResource")
]): java.lang.IllegalArgumentException: value is null
    at org.jboss.dmr@1.5.0.Final//org.jboss.dmr.ModelNode.<init>(ModelNode.java:185)
    at org.jboss.as.controller@9.0.2.Final//org.jboss.as.controller.AbstractOperationContext.addResponseWarning(AbstractOperationContext.java:377)
    at org.jboss.as.jaxrs@17.0.1.Final//org.jboss.as.jaxrs.DeploymentRestResourcesDefintion$AbstractRestResReadHandler.execute(DeploymentRestResourcesDefintion.java:320)
    at org.jboss.as.controller@9.0.2.Final//org.jboss.as.controller.operations.global.ReadAttributeHandler.doExecuteInternal(ReadAttributeHandler.java:174)
    at org.jboss.as.controller@9.0.2.Final//org.jboss.as.controller.operations.global.ReadAttributeHandler.doExecute(ReadAttributeHandler.java:136)
org.jboss.threads@2.3.3.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1486) at org.jboss.threads@2.3.3.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1377) at java.base/java.lang.Thread.run(Thread.java:834) at org.jboss.threads@2.3.3.Final//org.jboss.threads.JBossThread.run(JBossThread.java:485)

INFO messages here

11:18:50,355 INFO  [org.jboss.weld.Event] (MSC service thread 1-6) WELD-000411: Observer method [BackedAnnotatedMethod] public org.omnifaces.VetoAnnotatedTypeExtension.processAnnotatedType(@Observes ProcessAnnotatedType<T>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.
11:18:50,368 WARN  [org.jboss.weld.Bootstrap] (MSC service thread 1-6) WELD-000146: BeforeBeanDiscovery.addAnnotatedType(AnnotatedType<?>) used for class com.sun.faces.flow.FlowDiscoveryCDIHelper is deprecated from CDI 1.1!
11:18:50,375 INFO  [io.smallrye.metrics] (MSC service thread 1-6) MicroProfile: Metrics activated (SmallRye Metrics version: 2.3.0)
11:18:50,442 INFO  [org.jboss.weld.Bootstrap] (Weld Thread Pool -- 2) WELD-001125: Illegal bean type org.infinispan.cdi.embedded.event.AbstractEventBridge<org.infinispan.notifications.cachelistener.event.Event<?, ?>> ignored on [EnhancedAnnotatedTypeImpl] public @Dependent class org.infinispan.cdi.embedded.event.cache.CacheEventBridge
11:18:50,453 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-6) MSC000001: Failed to start service jboss.deployment.unit."demo-app.war".WeldStartService: org.jboss.msc.service.StartException in service jboss.deployment.unit."demo-app.war".WeldStartService: Failed to start service
    at org.jboss.msc@1.4.8.Final//org.jboss.msc.service.ServiceControllerImpl$StartTask.execute(ServiceControllerImpl.java:1729)
    at org.jboss.msc@1.4.8.Final//org.jboss.msc.service.ServiceControllerImpl$ControllerTask.run(ServiceControllerImpl.java:1557)
    at org.jboss.threads@2.3.3.Final//org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
    at org.jboss.threads@2.3.3.Final//org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1982)
    at org.jboss.threads@2.3.3.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1486)
    at org.jboss.threads@2.3.3.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1377)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.IllegalArgumentException: WFLYWELD0037: Error injecting persistence unit into CDI managed bean. Can't find a persistence unit named 'my-persistence-unit' in deployment demo-app.war for injection point private javax.persistence.EntityManager com.demo-app.cloud.repository.ProgramRepository.em
    at org.jboss.as.weld.jpa@17.0.1.Final//org.jboss.as.weld.services.bootstrap.WeldJpaInjectionServices.getScopedPUName(WeldJpaInjectionServices.java:105)
    at org.jboss.as.weld.jpa@17.0.1.Final//org.jboss.as.weld.services.bootstrap.WeldJpaInjectionServices.registerPersistenceContextInjectionPoint(WeldJpaInjectionServices.java:68)
    at org.jboss.weld.core@3.1.1.Final//org.jboss.weld.injection.ResourceInjectionFactory$PersistenceContextResourceInjectionProcessor.getResourceReferenceFactory(ResourceInjectionFactory.java:174)

    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)
    at org.jboss.threads@2.3.3.Final//org.jboss.threads.JBossThread.run(JBossThread.java:485)

11:18:50,462 ERROR [org.jboss.as.controller.management-operation] (DeploymentScanner-threads - 2) WFLYCTL0013: Operation ("full-replace-deployment") failed - address: ([]) - failure description: {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"demo-app.war\".WeldStartService" => "Failed to start service
    Caused by: java.lang.IllegalArgumentException: WFLYWELD0037: Error injecting persistence unit into CDI managed bean. Can't find a persistence unit named 'my-persistence-unit' in deployment demo-app.war for injection point private javax.persistence.EntityManager com.demo-app.cloud.repository.ProgramRepository.em"}}
11:18:50,485 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) WFLYSRV0016: Replaced deployment "demo-app.war" with deployment "demo-app.war"
11:18:50,486 INFO  [org.jboss.as.controller] (DeploymentScanner-threads - 2) WFLYCTL0183: Service status report
WFLYCTL0186:   Services which failed to start:      service jboss.deployment.unit."demo-app.war".WeldStartService: Failed to start service

11:42:11,276 WARN  [org.jboss.as.jaxrs] (management-handler-thread - 3) WFLYRS0017: Failed to read attribute from JAX-RS deployment at [
    ("deployment" => "demo-app.war"),
    ("subsystem" => "jaxrs"),
    ("rest-resource" => "com.demo-app.cloud.api.system.ApiResource")
] with name "sub-resource-locators": java.lang.NullPointerException
    at org.wildfly.extension.undertow@17.0.1.Final//org.wildfly.extension.undertow.deployment.UndertowDeploymentService.getDeployment(UndertowDeploymentService.java:166)
    at org.jboss.as.jaxrs@17.0.1.Final//org.jboss.as.jaxrs.DeploymentRestResourcesDefintion$AbstractRestResReadHandler.execute(DeploymentRestResourcesDefintion.java:216)
    at org.jboss.as.controller@9.0.2.Final//org.jboss.as.controller.operations.global.ReadAttributeHandler.doExecuteInternal(ReadAttributeHandler.java:174)
    at org.jboss.threads@2.3.3.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1377)
    at java.base/java.lang.Thread.run(Thread.java:834)
    at org.jboss.threads@2.3.3.Final//org.jboss.threads.JBossThread.run(JBossThread.java:485)

11:42:11,363 ERROR [org.jboss.as.controller.management-operation] (management-handler-thread - 3) WFLYCTL0013: Operation ("read-attribute") failed - address: ([
    ("deployment" => "demo-app.war"),
    ("subsystem" => "jaxrs"),
    ("rest-resource" => "com.demo-app.cloud.api.system.ApiResource")
]): java.lang.IllegalArgumentException: value is null
    at org.jboss.dmr@1.5.0.Final//org.jboss.dmr.ModelNode.<init>(ModelNode.java:185)
    at org.jboss.as.controller@9.0.2.Final//org.jboss.as.controller.AbstractOperationContext.addResponseWarning(AbstractOperationContext.java:377)
    at org.jboss.as.jaxrs@17.0.1.Final//org.jboss.as.jaxrs.DeploymentRestResourcesDefintion$AbstractRestResReadHandler.execute(DeploymentRestResourcesDefintion.java:320)
    at org.jboss.as.controller@9.0.2.Final//org.jboss.as.controller.operations.global.ReadAttributeHandler.doExecuteInternal(ReadAttributeHandler.java:174)
    at org.jboss.threads@2.3.3.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1377)
    at java.base/java.lang.Thread.run(Thread.java:834)
    at org.jboss.threads@2.3.3.Final//org.jboss.threads.JBossThread.run(JBossThread.java:485)


INFO messages here

11:42:18,541 INFO  [org.jboss.weld.Event] (MSC service thread 1-4) WELD-000411: Observer method [BackedAnnotatedMethod] public org.omnifaces.VetoAnnotatedTypeExtension.processAnnotatedType(@Observes ProcessAnnotatedType<T>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.
11:42:18,573 WARN  [org.jboss.weld.Bootstrap] (MSC service thread 1-4) WELD-000146: BeforeBeanDiscovery.addAnnotatedType(AnnotatedType<?>) used for class com.sun.faces.flow.FlowDiscoveryCDIHelper is deprecated from CDI 1.1!
11:42:18,579 INFO  [io.smallrye.metrics] (MSC service thread 1-4) MicroProfile: Metrics activated (SmallRye Metrics version: 2.3.0)
11:42:18,659 INFO  [org.jboss.weld.Bootstrap] (Weld Thread Pool -- 5) WELD-001125: Illegal bean type org.infinispan.cdi.embedded.event.AbstractEventBridge<org.infinispan.notifications.cachelistener.event.Event<?, ?>> ignored on [EnhancedAnnotatedTypeImpl] public @Dependent class org.infinispan.cdi.embedded.event.cache.CacheEventBridge
11:42:18,667 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-4) MSC000001: Failed to start service jboss.deployment.unit."demo-app.war".WeldStartService: org.jboss.msc.service.StartException in service jboss.deployment.unit."demo-app.war".WeldStartService: Failed to start service
    at org.jboss.msc@1.4.8.Final//org.jboss.msc.service.ServiceControllerImpl$StartTask.execute(ServiceControllerImpl.java:1729)
    at org.jboss.msc@1.4.8.Final//org.jboss.msc.service.ServiceControllerImpl$ControllerTask.run(ServiceControllerImpl.java:1557)
    at org.jboss.threads@2.3.3.Final//org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
    at org.jboss.threads@2.3.3.Final//org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1982)
    at org.jboss.threads@2.3.3.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1486)
    at org.jboss.threads@2.3.3.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1377)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.IllegalArgumentException: WFLYWELD0037: Error injecting persistence unit into CDI managed bean. Can't find a persistence unit named 'userprofile-persistence-unit' in deployment demo-app.war for injection point private javax.persistence.EntityManager com.demo-app.cloud.repository.UserProfileRepository.em
    at org.jboss.as.weld.jpa@17.0.1.Final//org.jboss.as.weld.services.bootstrap.WeldJpaInjectionServices.getScopedPUName(WeldJpaInjectionServices.java:105)
    at org.jboss.as.weld.jpa@17.0.1.Final//org.jboss.as.weld.services.bootstrap.WeldJpaInjectionServices.registerPersistenceContextInjectionPoint(WeldJpaInjectionServices.java:68)
    at org.jboss.weld.core@3.1.1.Final//org.jboss.weld.injection.ResourceInjectionFactory$PersistenceContextResourceInjectionProcessor.getResourceReferenceFactory(ResourceInjectionFactory.java:174)

    at org.jboss.weld.core@3.1.1.Final//org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:55)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)
    at org.jboss.threads@2.3.3.Final//org.jboss.threads.JBossThread.run(JBossThread.java:485)

11:42:18,712 ERROR [org.jboss.as.controller.management-operation] (DeploymentScanner-threads - 2) WFLYCTL0013: Operation ("full-replace-deployment") failed - address: ([]) - failure description: {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"demo-app.war\".WeldStartService" => "Failed to start service
    Caused by: java.lang.IllegalArgumentException: WFLYWELD0037: Error injecting persistence unit into CDI managed bean. Can't find a persistence unit named 'userprofile-persistence-unit' in deployment demo-app.war for injection point private javax.persistence.EntityManager com.demo-app.cloud.repository.UserProfileRepository.em"}}
11:42:18,743 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) WFLYSRV0016: Replaced deployment "demo-app.war" with deployment "demo-app.war"
11:42:18,743 INFO  [org.jboss.as.controller] (DeploymentScanner-threads - 2) WFLYCTL0183: Service status report
WFLYCTL0186:   Services which failed to start:      service jboss.deployment.unit."demo-app.war".WeldStartService:

堆栈跟踪抱怨“ApiResource”类,如下所示:

@Path("birds")
public class ApiResource {

    @GET
    public Response check() {
        return Response.ok().build();
    }
}

在此处查看@ApplicationPath:

@ApplicationPath("api")
public class ApplicationConfig extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        final Set<Class<?>> classes = new HashSet<>();
        classes.add(ApiResource.class);
        return classes;
    }
}

【问题讨论】:

  • 这很难帮你,因为你的 JNDI / 数据源 / 持久性单元名称每次出现都是不同的并且总是在变化(例如 our_app_name、app_name_here 或 our_app_name_here-core)请合并它。
  • 真的,对不起。感谢您查看它。我编辑了问题以使其保持一致,并添加了更多信息
  • 你是不是这样设置应用程序路径@ApplicationPath("foobar") ?
  • 是的,就像那样。我刚刚在我的问题中添加了该代码。

标签: wildfly cdi java-11 persistence.xml


【解决方案1】:

最后,修复是在 WEB-INF/classes/META-INF/persistence.xml 中包含 persistence.xml 文件

【讨论】:

    猜你喜欢
    • 2021-02-27
    • 2018-07-07
    • 2015-03-24
    • 2012-05-25
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    • 2011-09-28
    • 2011-10-16
    相关资源
    最近更新 更多