CDI 2.0,Java SE - 条件观察者方法,不在焊接se-shaded 3.0.5.Final中调用

问题描述 投票:0回答:1

我遇到了一个没有被调用的条件观察者方法的问题。这是代码,从junit测试开始:

    import static org.hamcrest.CoreMatchers.notNullValue;
    import static org.hamcrest.CoreMatchers.nullValue;
    import static org.junit.Assert.assertThat;

    import javax.enterprise.inject.Instance;
    import javax.enterprise.inject.se.SeContainer;
    import javax.enterprise.inject.se.SeContainerInitializer;

    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;

    public class CDIMinimalConditionalObserverTest
    {
        private final static Logger LOGGER = LogManager.getLogger(CDIMinimalConditionalObserverTest.class);

        private SeContainer container;

        @Before public void before()
        {
            LOGGER.debug("before");
        final SeContainerInitializer initialiser = SeContainerInitializer.newInstance();
        container = initialiser.initialize();
    }

    @After public void after()
    {
        container.close();
        LOGGER.debug("after");
    }

    @Test public void testObservation_observationInManagedNonExistentConditionalObservers()
    {
        CDIMinimalConditionalObserverEvent event = new CDIMinimalConditionalObserverEvent();
        container.getBeanManager().fireEvent(event);
        assertThat(event.msg, nullValue());
    }

    @Test public void testObservation_observationInManagedExistentConditionalObservers()
    {
        // create observer by selection
        Instance<CDIMinimalConditionalObserver> instance = container.select(CDIMinimalConditionalObserver.class);
        CDIMinimalConditionalObserver observer = instance.get();
        assertThat(observer, notNullValue());

        CDIMinimalConditionalObserverEvent event = new CDIMinimalConditionalObserverEvent();
        container.getBeanManager().fireEvent(event);
        observer.doSomething();
        assertThat(event.msg, notNullValue());
    }
}

这是带有条件观察者方法的类:

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
import javax.enterprise.event.Reception;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import de.jmda.sandbox.cdi.se.CDIMinimalTests.SimpleInnerEvent;

/**
 * {@link Model} annotation assigns non-dependent scope and thereby makes it possible to make {@link
 * #observation(SimpleInnerEvent)} conditional
 */
@ApplicationScoped public class CDIMinimalConditionalObserver
{
    private final static Logger LOGGER = LogManager.getLogger(SimpleConditionalObserver.class);

    public CDIMinimalConditionalObserver()
    {
        LOGGER.debug("constructor");
    }

    @PostConstruct public void postConstruct()
    {
        LOGGER.debug("post construct");
    }

    @PreDestroy public void preDestroy()
    {
        LOGGER.debug("pre destroy");
    }

    public void observation(@Observes(notifyObserver=Reception.IF_EXISTS) CDIMinimalConditionalObserverEvent event)
    {
        event.msg = "observation";
        LOGGER.debug(event.msg);
    }

    public void doSomething()
    {
        LOGGER.debug("doing something");
    }
}

最后这里是事件类:

public class CDIMinimalConditionalObserverEvent { String msg; }

测试失败,因为event.msg为null,尽管它不应该是。记录输出不显示任何“观察”输出。如果条件被删除,则测试通过。

有任何想法吗?谢谢!

java events conditional cdi-2.0
1个回答
0
投票

当您的@ApplicationScoped Bean被发现时,它不会立即实例化。 CDI非常聪明,只有在需要时才能初始化真实物体,即场景背后的物体。

您可以看到通过检索ApplicationScoped Bean

final Instance<App> select = container.select(App.class);
final App app = select.get();

确实会返回代理实例。 在此阶段,仍然没有附加到应用程序上下文的App Bean。

enter image description here

现在,尝试与该对象进行交互(即使只是通过调用toString),然后才启动事件。 您会注意到它确实有效,因为底层对象已通过其无参数构造函数进行实例化。

删除Reception.IF_EXISTS只是告诉CDI它必须创建并立即附加到底层实例的上下文,以便它可以接受传入的事件,无论如何。

这种代理行为记录在规范中(我需要找到页面),这就是Bean需要一个无参数构造函数的原因。

Dependent范围Beans不会遇到这个问题,因为它们是从头开始每次需要时创建的,并且不会被框架跟踪。对于单例,会话或请求范围的Bean,需要代理才能正确管理它们。


Dependent范围Bean,你可以看到它是一个“纯粹的”实例

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.