将EJB注入启动单例bean

时间:2022-10-30 09:14:47

In my application, I have the following startup bean:

在我的应用程序中,我有以下启动bean:

@Startup
@Singleton
@DependsOn("RecordAcumulator")
public class StartupBean {
    private static final Logger logger = Logger.getLogger(StartupBean.class);

    @Inject
    RecordAcumulator recordAcumulator;

    /**
     * Initializes the EJB system on the post construct event
     */
    @PostConstruct
    public void init() {

The record accumulator is an EJB that accesses the database. The startup is intended to preload database tables into the cache.

记录累加器是访问数据库的EJB。启动旨在将数据库表预加载到缓存中。

@Stateless
public class RecordAcumulator {

When this launches, I get

当这个发布时,我明白了

Caused by: com.ibm.ws.exception.RuntimeWarning: CNTR0200E: The StartupBean singleton session bean in the EJB.jar module depends on the RecordAcumulator enterprise bean in the EJB.jar, but the target is not a singleton session bean.

I have tried many variations of this and I can't seem to get the thing to inject. My log file indicates that the RecordAcumulator EJB was bound prior to the startup bean being loaded, so I can't figure out why I can't inject the EJB into my startup.

我已经尝试了很多这方面的变化,我似乎无法得到注入的东西。我的日志文件表明RecordAcumulator EJB在加载启动bean之前已绑定,所以我无法弄清楚为什么我不能将EJB注入我的启动。

If I remove the @DependsOn I get this:

如果我删除了@DependsOn,我得到这个:

Caused by: javax.ejb.NoSuchEJBException: An error occurred during initialization of singleton session bean bla#EJB.jar#StartupBean, resulting in the discarding of the singleton instance.; nested exception is: javax.ejb.EJBException: The @Inject java.lang.reflect.Field.recordAcumulator reference of type com.foo.bar.accum.RecordAcumulator for the StartupBean component in the EJB.jar module of the bla application cannot be resolved.

Any ideas how to pull this off?

任何想法如何解决这个问题?

EDIT---------- I found this link: Controlling CDI Startup inside EJB 3.1

编辑----------我发现此链接:在EJB 3.1中控制CDI启动

But the issue with that is i'm using WAS 8.5.5.0, That issue was supposed to be resolved in 8.5.0.2

但问题是我正在使用WAS 8.5.5.0,该问题应该在8.5.0.2中得到解决

1 个解决方案

#1


From what I can see:

从我所看到的:

  1. Remove the @DependsOn
  2. 删除@DependsOn

  3. Make your EJB @Singleton
  4. 制作你的EJB @Singleton

There can be complexities with singleton EJBs, as all the traffic going through your application will go through that one instance. In your case that may not be an issue.

单例EJB可能存在复杂性,因为通过应用程序的所有流量都将通过该实例。在你的情况下,这可能不是一个问题。

#1


From what I can see:

从我所看到的:

  1. Remove the @DependsOn
  2. 删除@DependsOn

  3. Make your EJB @Singleton
  4. 制作你的EJB @Singleton

There can be complexities with singleton EJBs, as all the traffic going through your application will go through that one instance. In your case that may not be an issue.

单例EJB可能存在复杂性,因为通过应用程序的所有流量都将通过该实例。在你的情况下,这可能不是一个问题。