gpt4 book ai didi

Java CDI 依赖项无效 DependsOn

转载 作者:行者123 更新时间:2023-12-01 09:48:57 26 4
gpt4 key购买 nike

我正在尝试在应用程序启动时更新数据库中的某些内容。为此,我创建了一个 A 类来执行此操作。

@Singleton
@Startup
@DependsOn({ "B", "C" })
public class A{
@Inject B b;
@Inject C c;
...
}

B 是一个从数据库读取一些配置值的类。乙:

@Singleton
@Startup
public class B {
@PersistenceContext
EntityManager em;
...
}


C 是一个树结构,它使用 D 类和 E 类的数据来构造格式正确的树。
C:

@Singleton
@Startup
@DependsOn({ "D", "E" })
public class C{
@Inject
D d;
@Inject
E e;
...
}

D 和 E 是叶单例,因为它们不依赖于其他单例;它们提供数据(从文件读取到数据库):
D:

@Singleton
@Startup
public class D { ... }


E:

@Singleton
@Startup
public class E { ... }

基于DependsOn annotation documentation我假设 CDI 将为单例创建依赖关系图,并按指定的顺序初始化它们(B、D 和 E 将在 C 之前初始化,最后 A 初始化之前)。但是,当我尝试部署应用程序时,我收到一个异常告诉我:
生命周期处理异常java.lang.RuntimeException:EJB ContentUpdater 的 DependsOn 依赖项“C”无效。

完整堆栈跟踪:

java.lang.RuntimeException: Invalid DependsOn dependency 'C' for EJB A
at org.glassfish.ejb.deployment.util.EjbBundleValidator.checkDependsOn(EjbBundleValidator.java:602)
at org.glassfish.ejb.deployment.util.EjbBundleValidator.accept(EjbBundleValidator.java:300)
at org.glassfish.ejb.deployment.descriptor.EjbDescriptor.visit(EjbDescriptor.java:2823)
at org.glassfish.ejb.deployment.descriptor.EjbDescriptor.visit(EjbDescriptor.java:2811)
at org.glassfish.ejb.deployment.util.EjbBundleValidator.accept(EjbBundleValidator.java:115)
at com.sun.enterprise.deployment.BundleDescriptor.visit(BundleDescriptor.java:625)
at org.glassfish.ejb.deployment.descriptor.EjbBundleDescriptorImpl.visit(EjbBundleDescriptorImpl.java:757)
at com.sun.enterprise.deployment.util.ApplicationValidator.accept(ApplicationValidator.java:121)
at com.sun.enterprise.deployment.BundleDescriptor.visit(BundleDescriptor.java:625)
at com.sun.enterprise.deployment.archivist.ApplicationFactory.openArchive(ApplicationFactory.java:190)
at org.glassfish.javaee.core.deployment.DolProvider.processDOL(DolProvider.java:203)
at org.glassfish.javaee.core.deployment.DolProvider.load(DolProvider.java:227)
at org.glassfish.javaee.core.deployment.DolProvider.load(DolProvider.java:96)
at com.sun.enterprise.v3.server.ApplicationLifecycle.loadDeployer(ApplicationLifecycle.java:881)
at com.sun.enterprise.v3.server.ApplicationLifecycle.setupContainerInfos(ApplicationLifecycle.java:821)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:377)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:219)
at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:491)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:539)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:535)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:360)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$2.execute(CommandRunnerImpl.java:534)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:565)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:557)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:360)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:556)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1464)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1300(CommandRunnerImpl.java:109)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1846)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1722)
at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:534)
at com.sun.enterprise.v3.admin.AdminAdapter.onMissingResource(AdminAdapter.java:224)
at org.glassfish.grizzly.http.server.StaticHttpHandlerBase.service(StaticHttpHandlerBase.java:189)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
at java.lang.Thread.run(Thread.java:745)

有谁知道为什么在上述结构的情况下会发生此异常?

当我在 A 类的 @DependsOn 注释中省略 C 类时,我得到另一个异常,所以不幸的是这不是一个解决方案。该应用程序部署在GlassFish 4.1上

最佳答案

我建议你重新考虑你的设计并只创建一个“StartUpController”它会按照您需要的顺序触发所有其他 ejb 上的 init 方法。

@Singleton
public class A {
public void init() {}
}

@Singleton
public class B {
public void init() {}
}

@Singleton
public class C {
public void init() {}
}



@Startup
@Singleton
public class StartUpController {

@Inject
private A a;

@Inject
private B b;

@Inject
private C c;

@PostConstruct
protected void setup() {
a.init();
b.init();
c.init();
}

}

@DependsOn 并不是真正的“依赖控制”,这只是初始化顺序的控制,只有与 @PostConstruct 方法一起使用才有意义。(我没有在您中看到任何初始化方法)示例)所以 @DependsOn 是不必要的。您可以在没有此注释的情况下将 ejb 连接到链中 thisthis示例。

我无法准确回答为什么你的代码不起作用,也许这只是一个打字错误。例如,您在上面发布的代码中没有 C.class,而是有 QuestionHandler.class,并且我在您的代码中没有看到任何 @PostConstruct 方法。另一个原因可能是glashfish 配置,我建议您尝试在wildfly 服务器上编写代码。

关于Java CDI 依赖项无效 DependsOn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37746926/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com