gpt4 book ai didi

java - 预期的?注入(inject)生产者的 CDI 请求作用域 bean 注入(inject)依赖 bean 给出 WELD-001303 : No active contexts

转载 作者:行者123 更新时间:2023-12-01 10:02:04 29 4
gpt4 key购买 nike

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

给定

package at.joma.stackoverflow.cdi;

public class Product {

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

public String value;

}
  • 将 Product 注入(inject)到该 Activity 请求范围
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());
}

}
  • 并且应该向该生产者注入(inject)一个请求范围的 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 预期的行为吗?
  • 如果是,是因为 Producer 的依赖伪作用域吗?
  • 如果 SomeCache 是应用程序范围而不是请求范围,为什么这不会失败?

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

那个(不工作的)请求范围的 SomeCache 确实缓存了生产者生成第一个要注入(inject)的 bean 所需的一些数据。所以可以

  • 两者都不依赖(因为这会使缓存变得无用)
  • 也不应该是应用程序范围的或静态的(范围太宽)

理想情况下,它应该自动位于注入(inject) Product 的 bean 的同一范围内 - 在示例中为 ExpectedTest。很可能是另一个自定义 cdi 范围,但不应依赖于定义该范围的客户端/用户开发人员。如果有意义的话,人们可以将其称为“传递作用域相关 bean”。

最佳答案

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

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

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

关于java - 预期的?注入(inject)生产者的 CDI 请求作用域 bean 注入(inject)依赖 bean 给出 WELD-001303 : No active contexts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36730259/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com