我正在尝试在应用程序启动时更新数据库中的某些内容。为了做到这一点,我创建了一个A类,这样做。
@Singleton
@Startup
@DependsOn({ "B", "C" })
public class A{
@Inject B b;
@Inject C c;
...
}
B是一个从数据库中读取一些配置值的类。 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是叶子单体,它们不依赖于其他单体;它们提供数据(从文件读取到数据库):@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
上答案 0 :(得分:1)
我建议你重新考虑你的设计并只创建一个“StartUpController” 它会根据您的需要触发所有其他ejbs上的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连接到链中而不使用此注释外观this和this示例。
我无法准确回答为什么你的代码不起作用,也许这只是一个打字错误。例如,您在上面发布的代码中没有C.class,而是您有QuestionHandler.class,而且我没有在您的代码中看到任何@PostConstruct方法。另一个原因可能是glashfish配置我建议你尝试在wildfly服务器上编码。
答案 1 :(得分:0)
命名有误。 在这里,预期的名称为“ C”的bean不存在,而是Singleton bean的名称为“ QuestionHandler”,并且D是重复的类,具有Singleton名称D和E。这是问题中的错误,不一定是原因。问题。
所以,如何避免这些“拼写错误”。
一种解决方案是使用静态字段连接点。
A:
@Singleton(name = A.BEAN_NAME)
@Startup
@DependsOn({ B.BEAN_NAME, C.BEAN_NAME })
public class A {
public static final BEAN_NAME = "A";
@Inject B b;
@Inject C c;
...
}
B:
@Singleton(name = B.BEAN_NAME)
@Startup
public class B {
public static final BEAN_NAME = "B";
@PersistenceContext
EntityManager em;
...
}
C:
@Singleton(name = QuestionHandler.BEAN_NAME)
@Startup
@DependsOn({ D.BEAN_NAME, E.BEAN_NAME })
public class QuestionHandler {
public static final BEAN_NAME = "C";
@Inject
D d;
@Inject
E e;
...
}
D:
@Singleton(name = D.BEAN_NAME)
@Startup
public class D {
public static final BEAN_NAME = "D";
...
}
E:
@Singleton(name = E.BEAN_NAME)
@Startup
public class E {
public static final BEAN_NAME = "E";
...
}