预期?注入到依赖bean中的生产者注入的CDI请求范围bean给出了WELD-001303:没有活动的上下文

时间:2016-04-19 22:00:44

标签: java cdi weld cdi-unit

假设RequestScope处于活动状态(使用cdi-unit的@InRequestScope)

给出

package at.joma.stackoverflow.cdi;

public class Product {

    public Product(String withValue){
        this.value = withValue;
    }

    public String value;

}
  • 将Product注入该活动请求范围
package at.joma.stackoverflow.cdi;
import javax.inject.Inject;

import org.jglue.cdiunit.AdditionalClasses;
import org.jglue.cdiunit.CdiRunner;
import org.jglue.cdiunit.InRequestScope;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(CdiRunner.class)
@AdditionalClasses({ Producer.class, SomeCache.class })
public class ExpectedTest {

    @Inject
    Product productA;

    @Inject
    Product productB;

    @Test
    @InRequestScope
    public void testScoping() {
        Assert.assertNotNull(productA);
        System.out.println(productA.value);
        Assert.assertNotNull(productB);
    }

}
  • 使用依赖的Producer(带有InjectionPoint参数,因此必须依赖)
package at.joma.stackoverflow.cdi;

import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.inject.Inject;

public class Producer {

    public static final String VALUE_B = "valueB";

    public static final String VALUE_A = "valueA";

    @Inject
    SomeCache someCache;

    @Produces
    public Product produceProduct(InjectionPoint ip) {
        if (!VALUE_B.equals(someCache.getCacheValue())) {
            someCache.setCacheValue(VALUE_A);
        } else {
            someCache.setCacheValue(VALUE_B);
        }
        return new Product(someCache.getCacheValue());
    }

}
  • 并且该生产者应该注入一个请求范围的bean
package at.joma.stackoverflow.cdi;

import javax.enterprise.context.RequestScoped;

@RequestScoped
public class SomeCache {

    private String cacheValue;

    public String getCacheValue() {
        return cacheValue;
    }

    public void setCacheValue(String cacheValue) {
        this.cacheValue = cacheValue;
    }

}

当这导致请求范围内的bean

失败时
org.jboss.weld.context.ContextNotActiveException: WELD-001303: No active contexts for scope type javax.enterprise.context.RequestScoped
    at org.jboss.weld.manager.BeanManagerImpl.getContext(BeanManagerImpl.java:708)
    at org.jboss.weld.bean.ContextualInstanceStrategy$DefaultContextualInstanceStrategy.getIfExists(ContextualInstanceStrategy.java:90)
    at org.jboss.weld.bean.ContextualInstanceStrategy$CachingContextualInstanceStrategy.getIfExists(ContextualInstanceStrategy.java:165)
    at org.jboss.weld.bean.ContextualInstance.getIfExists(ContextualInstance.java:63)
    at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:83)
    at org.jboss.weld.bean.proxy.ProxyMethodHandler.getInstance(ProxyMethodHandler.java:125)
    at at.joma.stackoverflow.cdi.SomeCache$Proxy$_$$_WeldClientProxy.getCacheValue(Unknown Source)
    at at.joma.stackoverflow.cdi.Producer.produceProduct(Producer.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.jboss.weld.injection.StaticMethodInjectionPoint.invoke(StaticMethodInjectionPoint.java:88)
    at org.jboss.weld.injection.StaticMethodInjectionPoint.invoke(StaticMethodInjectionPoint.java:78)
    at org.jboss.weld.injection.producer.ProducerMethodProducer.produce(ProducerMethodProducer.java:95)
    at org.jboss.weld.injection.producer.AbstractMemberProducer.produce(AbstractMemberProducer.java:167)
    at org.jboss.weld.bean.AbstractProducerBean.create(AbstractProducerBean.java:183)
    at org.jboss.weld.context.unbound.DependentContextImpl.get(DependentContextImpl.java:69)
    at org.jboss.weld.bean.ContextualInstanceStrategy$DefaultContextualInstanceStrategy.get(ContextualInstanceStrategy.java:101)
    at org.jboss.weld.bean.ContextualInstance.get(ContextualInstance.java:50)
    at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:761)
    at org.jboss.weld.manager.BeanManagerImpl.getInjectableReference(BeanManagerImpl.java:861)
    at org.jboss.weld.injection.FieldInjectionPoint.inject(FieldInjectionPoint.java:92)
    at org.jboss.weld.util.Beans.injectBoundFields(Beans.java:375)
    at org.jboss.weld.util.Beans.injectFieldsAndInitializers(Beans.java:386)
    at org.jboss.weld.injection.producer.ResourceInjector$1.proceed(ResourceInjector.java:70)
    at org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:48)
    at org.jboss.weld.injection.producer.ResourceInjector.inject(ResourceInjector.java:72)
    at org.jboss.weld.injection.producer.BasicInjectionTarget.inject(BasicInjectionTarget.java:121)
    at org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:159)
    at org.jboss.weld.context.AbstractContext.get(AbstractContext.java:96)
    at org.jboss.weld.bean.ContextualInstanceStrategy$DefaultContextualInstanceStrategy.get(ContextualInstanceStrategy.java:101)
    at org.jboss.weld.bean.ContextualInstanceStrategy$ApplicationScopedContextualInstanceStrategy.get(ContextualInstanceStrategy.java:141)
    at org.jboss.weld.bean.ContextualInstance.get(ContextualInstance.java:50)
    at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:99)
    at org.jboss.weld.bean.proxy.ProxyMethodHandler.getInstance(ProxyMethodHandler.java:125)
    at at.joma.stackoverflow.cdi.ExpectedTest$Proxy$_$$_WeldClientProxy.testScoping(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.jglue.cdiunit.CdiRunner$2.evaluate(CdiRunner.java:179)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
  • 这是每个CDI预期的行为吗?
  • 如果是,由于生产者的依赖伪范围?
  • 如果SomeCache是​​应用程序作用域而不是请求作用域,为什么这不会失败?

SomeCache的合理范围解决方案是什么?

那个(不工作)请求作用域SomeCache会缓存生产者生成第一个要注入的bean所需的一些数据。所以它可以

  • 既不依赖(因为这会使缓存变得毫无用处)
  • 也不应该是应用范围或静态(范围太宽)

理想情况下,它应该自动处于注入Product的bean的相同范围内 - 在ExpectedTest示例中。可能是另一个自定义cdi范围,但不应该依赖于定义该范围的客户端/用户开发人员。如果有任何意义,可以称之为“传递范围依赖的bean”。

1 个答案:

答案 0 :(得分:0)

这是每CDI预期的行为吗? - >否

失败的原因是cdi-unit没有记录/报告它无法设置请求范围。因此我错过了定义以下依赖项。顺便说一下,cd-unit中定义了一个。所以关于范围:)

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
</dependency>