1

我正在尝试构建一个在启动时启动的 bean,以对我们拥有的数据进行迁移。它被注释为@Singleton @Startup bean,但@PostConstruct 失败了,但我不知道为什么。

这是代码:

@Startup
@Singleton
@DependsOn("SearchInitializer")
public class SearchMigrator {
    @Inject
    @Any
    private Instance<MigrationScenario> migrationScenarios;
    @Inject
    private SearchRepository repository;
    private List<MigrationScenario> sortedScenarioList;

    @PostConstruct
    public void doMigration() {
        (call run() on each MigrationScenario)
    }
}

SearchInitializer 也是一个 @Startup @Singleton bean,在方法上带有 @PostConstruct 注释,并且运行良好(同样,在这个 bean 之前,所以 @DependsOn 工作正常)。SearchRepository 是一个@Stateless bean,它将进入数据库。

但是,如果我运行这段代码,我会得到这个异常:

Create: java.lang.RuntimeException: Could not invoke PostConstruct on the newly created bean instance

有人知道我在做什么错吗?

编辑

附加信息:我怀疑“@Inject 私有 SearchRepository 存储库”是罪魁祸首。当我删除该存储库的注入时,它启动得很好。如果我不这样做,我会收到此错误:

16:10:39,531 ERROR [org.jboss.kernel.plugins.dependency.AbstractKernelController] Error installing to Start: name=startup-singleton-initiator:topLevelUnit=xxx-war.war,unit=xxx-war.war,bean=SearchInitializer aliases=[startup-singleton-initiator:bean=SearchInitializer,topLevelUnit=xxx-war.war,unit=xxx-war.war] state=Create: java.lang.RuntimeException: Could not invoke PostConstruct on the newly created bean instance
at org.jboss.ejb3.singleton.impl.container.SingletonEJBInstanceManagerImpl.create(SingletonEJBInstanceManagerImpl.java:137) [:1.0.2]
at org.jboss.ejb3.singleton.impl.container.SingletonEJBInstanceManagerImpl.get(SingletonEJBInstanceManagerImpl.java:152) [:1.0.2]
at org.jboss.ejb3.singleton.deployer.StartupSingletonInitiator.start(StartupSingletonInitiator.java:84) [:1.0.2]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_30]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [:1.6.0_30]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_30]
at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_30]
at org.jboss.reflect.plugins.introspection.ReflectionUtils.invoke(ReflectionUtils.java:60) [jboss-reflect.jar:2.2.1.SP1]
at org.jboss.reflect.plugins.introspection.ReflectMethodInfoImpl.invoke(ReflectMethodInfoImpl.java:168) [jboss-reflect.jar:2.2.1.SP1]
at org.jboss.joinpoint.plugins.BasicMethodJoinPoint.dispatch(BasicMethodJoinPoint.java:66) [jboss-reflect.jar:2.2.1.SP1]
at org.jboss.kernel.plugins.dependency.KernelControllerContextAction$JoinpointDispatchWrapper.execute(KernelControllerContextAction.java:257) [jboss-kernel.jar:2.2.0.SP2]
at org.jboss.kernel.plugins.dependency.ExecutionWrapper.execute(ExecutionWrapper.java:47) [jboss-kernel.jar:2.2.0.SP2]
at org.jboss.kernel.plugins.dependency.KernelControllerContextAction.dispatchExecutionWrapper(KernelControllerContextAction.java:125) [jboss-kernel.jar:2.2.0.SP2]
at org.jboss.kernel.plugins.dependency.KernelControllerContextAction.dispatchJoinPoint(KernelControllerContextAction.java:72) [jboss-kernel.jar:2.2.0.SP2]
at org.jboss.kernel.plugins.dependency.LifecycleAction.installActionInternal(LifecycleAction.java:202) [jboss-kernel.jar:2.2.0.SP2]
at org.jboss.kernel.plugins.dependency.InstallsAwareAction.installAction(InstallsAwareAction.java:54) [jboss-kernel.jar:2.2.0.SP2]
at org.jboss.kernel.plugins.dependency.InstallsAwareAction.installAction(InstallsAwareAction.java:42) [jboss-kernel.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.action.SimpleControllerContextAction.simpleInstallAction(SimpleControllerContextAction.java:62) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.action.AccessControllerContextAction.install(AccessControllerContextAction.java:71) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.AbstractControllerContextActions.install(AbstractControllerContextActions.java:51) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.AbstractControllerContext.install(AbstractControllerContext.java:379) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:2044) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.AbstractController.incrementState(AbstractController.java:1083) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.AbstractController.executeOrIncrementStateDirectly(AbstractController.java:1322) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:1246) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:1139) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.AbstractController.change(AbstractController.java:939) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.AbstractController.change(AbstractController.java:654) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.deployers.plugins.deployers.DeployersImpl.change(DeployersImpl.java:1983) [:2.2.2.GA]
at org.jboss.deployers.plugins.deployers.DeployersImpl.process(DeployersImpl.java:1076) [:2.2.2.GA]
at org.jboss.deployers.plugins.main.MainDeployerImpl.process(MainDeployerImpl.java:679) [:2.2.2.GA]
at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:380) [:6.1.0.Final]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_30]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [:1.6.0_30]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_30]
at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_30]
at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:157) [:6.0.0.GA]
at org.jboss.mx.server.Invocation.dispatch(Invocation.java:96) [:6.0.0.GA]
at org.jboss.mx.server.Invocation.invoke(Invocation.java:88) [:6.0.0.GA]
at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:271) [:6.0.0.GA]
at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:670) [:6.0.0.GA]
at org.jboss.system.server.jmx.MBeanServerWrapper.invoke(MBeanServerWrapper.java:138) [:6.1.0.Final]
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1427) [:1.6.0_30]
at javax.management.remote.rmi.RMIConnectionImpl.access$200(RMIConnectionImpl.java:72) [:1.6.0_30]
at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1265) [:1.6.0_30]
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1360) [:1.6.0_30]
at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:788) [:1.6.0_30]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_30]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [:1.6.0_30]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_30]
at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_30]
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:303) [:1.6.0_30]
at sun.rmi.transport.Transport$1.run(Transport.java:159) [:1.6.0_30]
at java.security.AccessController.doPrivileged(Native Method) [:1.6.0_30]
at sun.rmi.transport.Transport.serviceCall(Transport.java:155) [:1.6.0_30]
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535) [:1.6.0_30]
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790) [:1.6.0_30]
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649) [:1.6.0_30]
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [:1.6.0_30]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [:1.6.0_30]
at java.lang.Thread.run(Thread.java:662) [:1.6.0_30]
Caused by: java.lang.RuntimeException: java.lang.NullPointerException
at org.jboss.ejb3.singleton.aop.impl.AOPBasedSingletonContainer.invokeCallback(AOPBasedSingletonContainer.java:1065) [:1.0.2]
at org.jboss.ejb3.EJBContainer.invokePostConstruct(EJBContainer.java:1396) [:1.7.21]
at org.jboss.ejb3.singleton.aop.impl.AOPBasedSingletonContainer.postConstruct(AOPBasedSingletonContainer.java:762) [:1.0.2]
at org.jboss.ejb3.singleton.impl.container.SingletonEJBInstanceManagerImpl.create(SingletonEJBInstanceManagerImpl.java:133) [:1.0.2]
... 60 more
Caused by: java.lang.NullPointerException
at org.jboss.ejb3.EJBContainer.getApplicationException(EJBContainer.java:509) [:1.7.21]
at org.jboss.ejb3.singleton.aop.impl.ConstructionInvocationContextAdapter.getApplicationException(ConstructionInvocationContextAdapter.java:62) [:1.0.2]
at org.jboss.ejb3.tx2.impl.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInterceptor.java:163) [:0.0.2]
at org.jboss.ejb3.tx2.impl.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:251) [:0.0.2]
at org.jboss.ejb3.tx2.impl.CMTTxInterceptor.requiresNew(CMTTxInterceptor.java:392) [:0.0.2]
at org.jboss.ejb3.tx2.impl.CMTTxInterceptor.invoke(CMTTxInterceptor.java:211) [:0.0.2]
at org.jboss.ejb3.tx2.aop.CMTTxInterceptorWrapper.invoke(CMTTxInterceptorWrapper.java:52) [:0.0.2]
at org.jboss.aop.joinpoint.ConstructionInvocation.invokeNext(ConstructionInvocation.java:80) [jboss-aop.jar:2.2.2.GA]
at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76) [:1.0.0.GA]
at org.jboss.aop.joinpoint.ConstructionInvocation.invokeNext(ConstructionInvocation.java:80) [jboss-aop.jar:2.2.2.GA]
at org.jboss.ejb3.core.context.CurrentInvocationContextInterceptor.invoke(CurrentInvocationContextInterceptor.java:47) [:1.7.21]
at org.jboss.aop.joinpoint.ConstructionInvocation.invokeNext(ConstructionInvocation.java:80) [jboss-aop.jar:2.2.2.GA]
at org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor.setup(InvocationContextInterceptor.java:90) [:1.1.3]
at org.jboss.aop.advice.org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor_z_setup_1631131631.invoke(InvocationContextInterceptor_z_setup_1631131631.java) [:]
at org.jboss.aop.joinpoint.ConstructionInvocation.invokeNext(ConstructionInvocation.java:80) [jboss-aop.jar:2.2.2.GA]
at org.jboss.aspects.currentinvocation.CurrentInvocationInterceptor.invoke(CurrentInvocationInterceptor.java:67) [:1.0.1]
at org.jboss.aop.joinpoint.ConstructionInvocation.invokeNext(ConstructionInvocation.java:80) [jboss-aop.jar:2.2.2.GA]
at org.jboss.ejb3.singleton.aop.impl.AOPBasedSingletonContainer.invokeCallback(AOPBasedSingletonContainer.java:1061) [:1.0.2]
... 63 more

如果我通过引导过程进行调试,我会看到存储库实际上是代理的(这也通过堆栈跟踪中对 AOP 的引用得到支持)。

但仍然没有运气弄清楚它!任何人?

4

2 回答 2

3

好的,我弄清楚了问题所在。实际上,我的回答将涵盖处理此类错误的一般方法。这个错误的问题在于 JBoss 本身:应用程序服务器实际上吃掉了真正的错误,并且只是在你的脸上抛出了 NullPointerException。

我为得到实际错误所做的只是暂时在我的代码中输入这个 hack,只是为了找出发生了什么:

@PostConstruct
public void doMigration() throws Throwable {
    try {
        runAllFoundScenarios();
    } catch (Throwable e) {
        System.out.println("Cause: " + e.getCause());
        System.out.println("Message: " + e.getMessage());
        System.out.println("Class: " + e.getClass());
        System.out.println("StackTrace: " + e.getStackTrace());
        throw e;
    }
}

这样,我就能够通过查看日志来确定真正的原因,这使我能够真正解决这个问题。我可以在网络上到处找到有同样问题的人,但没有人发布过解决方法,所以我希望这个答案能帮助你找出真正发生的事情。

仅供参考,对我来说,这是注入存储库的 JPA 问题。

于 2012-10-31T10:14:39.373 回答
1

如 PosConstruct 注释 ( PostConstruct javadoc ) 中所述,使用 PostConstruct 注释的方法不允许启动检查异常。

所以,很自然的问题出现了,我们应该如何报告启动 bean 的问题?抛出一个简单的 RuntimeException 来跳过检查的异常限制,似乎不是正确的方法。这种做法用来表示懒惰的开发,许多质量代码分析工具会抱怨。顺便说一句,EJB 容器吞下了原始问题中已经提到的异常。

好吧,在浏览了一段时间后,我还没有找到实际的答案。对于 EJB bean,在 EJBException 异常层次结构( EJBException javadoc)中似乎有一个公平的选择。

对于常规的 CDI bean,我想我们可以选择一些 RuntimeException 子类,如postconstruct 检查异常中所述

于 2014-02-07T10:59:29.873 回答