java - Expected? CDI Request scoped bean injected into producer injected into dependent bean gives WELD-001303: No active contexts -
assume requestscope active (using cdi-unit's @inrequestscope)
given
package at.joma.stackoverflow.cdi; public class product { public product(string withvalue){ this.value = withvalue; } public string value; }
- an injection of product active request scope
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); } }
- using dependent producer (with injectionpoint parameter, must dependent)
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()); } }
- and producer should injected request scoped 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; } }
when fails cause of request scoped bean with
org.jboss.weld.context.contextnotactiveexception: weld-001303: no active contexts scope type javax.enterprise.context.requestscoped @ org.jboss.weld.manager.beanmanagerimpl.getcontext(beanmanagerimpl.java:708) @ org.jboss.weld.bean.contextualinstancestrategy$defaultcontextualinstancestrategy.getifexists(contextualinstancestrategy.java:90) @ org.jboss.weld.bean.contextualinstancestrategy$cachingcontextualinstancestrategy.getifexists(contextualinstancestrategy.java:165) @ org.jboss.weld.bean.contextualinstance.getifexists(contextualinstance.java:63) @ org.jboss.weld.bean.proxy.contextbeaninstance.getinstance(contextbeaninstance.java:83) @ org.jboss.weld.bean.proxy.proxymethodhandler.getinstance(proxymethodhandler.java:125) @ at.joma.stackoverflow.cdi.somecache$proxy$_$$_weldclientproxy.getcachevalue(unknown source) @ at.joma.stackoverflow.cdi.producer.produceproduct(producer.java:18) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:483) @ org.jboss.weld.injection.staticmethodinjectionpoint.invoke(staticmethodinjectionpoint.java:88) @ org.jboss.weld.injection.staticmethodinjectionpoint.invoke(staticmethodinjectionpoint.java:78) @ org.jboss.weld.injection.producer.producermethodproducer.produce(producermethodproducer.java:95) @ org.jboss.weld.injection.producer.abstractmemberproducer.produce(abstractmemberproducer.java:167) @ org.jboss.weld.bean.abstractproducerbean.create(abstractproducerbean.java:183) @ org.jboss.weld.context.unbound.dependentcontextimpl.get(dependentcontextimpl.java:69) @ org.jboss.weld.bean.contextualinstancestrategy$defaultcontextualinstancestrategy.get(contextualinstancestrategy.java:101) @ org.jboss.weld.bean.contextualinstance.get(contextualinstance.java:50) @ org.jboss.weld.manager.beanmanagerimpl.getreference(beanmanagerimpl.java:761) @ org.jboss.weld.manager.beanmanagerimpl.getinjectablereference(beanmanagerimpl.java:861) @ org.jboss.weld.injection.fieldinjectionpoint.inject(fieldinjectionpoint.java:92) @ org.jboss.weld.util.beans.injectboundfields(beans.java:375) @ org.jboss.weld.util.beans.injectfieldsandinitializers(beans.java:386) @ org.jboss.weld.injection.producer.resourceinjector$1.proceed(resourceinjector.java:70) @ org.jboss.weld.injection.injectioncontextimpl.run(injectioncontextimpl.java:48) @ org.jboss.weld.injection.producer.resourceinjector.inject(resourceinjector.java:72) @ org.jboss.weld.injection.producer.basicinjectiontarget.inject(basicinjectiontarget.java:121) @ org.jboss.weld.bean.managedbean.create(managedbean.java:159) @ org.jboss.weld.context.abstractcontext.get(abstractcontext.java:96) @ org.jboss.weld.bean.contextualinstancestrategy$defaultcontextualinstancestrategy.get(contextualinstancestrategy.java:101) @ org.jboss.weld.bean.contextualinstancestrategy$applicationscopedcontextualinstancestrategy.get(contextualinstancestrategy.java:141) @ org.jboss.weld.bean.contextualinstance.get(contextualinstance.java:50) @ org.jboss.weld.bean.proxy.contextbeaninstance.getinstance(contextbeaninstance.java:99) @ org.jboss.weld.bean.proxy.proxymethodhandler.getinstance(proxymethodhandler.java:125) @ at.joma.stackoverflow.cdi.expectedtest$proxy$_$$_weldclientproxy.testscoping(unknown source) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:483) @ org.junit.runners.model.frameworkmethod$1.runreflectivecall(frameworkmethod.java:50) @ org.junit.internal.runners.model.reflectivecallable.run(reflectivecallable.java:12) @ org.junit.runners.model.frameworkmethod.invokeexplosively(frameworkmethod.java:47) @ org.junit.internal.runners.statements.invokemethod.evaluate(invokemethod.java:17) @ org.jglue.cdiunit.cdirunner$2.evaluate(cdirunner.java:179) @ org.junit.runners.parentrunner.runleaf(parentrunner.java:325) @ org.junit.runners.blockjunit4classrunner.runchild(blockjunit4classrunner.java:78) @ org.junit.runners.blockjunit4classrunner.runchild(blockjunit4classrunner.java:57) @ org.junit.runners.parentrunner$3.run(parentrunner.java:290) @ org.junit.runners.parentrunner$1.schedule(parentrunner.java:71) @ org.junit.runners.parentrunner.runchildren(parentrunner.java:288) @ org.junit.runners.parentrunner.access$000(parentrunner.java:58) @ org.junit.runners.parentrunner$2.evaluate(parentrunner.java:268) @ org.junit.runners.parentrunner.run(parentrunner.java:363) @ org.eclipse.jdt.internal.junit4.runner.junit4testreference.run(junit4testreference.java:50) @ org.eclipse.jdt.internal.junit.runner.testexecution.run(testexecution.java:38) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:459) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:675) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.run(remotetestrunner.java:382) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.main(remotetestrunner.java:192)
- is per cdi expected behavior?
- if yes, because of dependent pseudo scope of producer?
- why not fail if somecache application scoped instead of request scoped?
what reasonable scope solution somecache?
that (not-working) request scoped somecache cache data producer needs produce first to-be-injected bean. can
- neither dependent (as renders caching useless)
- nor should application scoped or static (too broad scope)
ideally should automagically in same scope of bean product injected - in example expectedtest. may custom cdi scope, should not depend on client/user developer defining scope. 1 may call "transitive scope-dependent bean", if makes sense.
is per cdi expected behavior? -> no
cause of failure cdi-unit not log/report not setup request scope. , therefore missed define following dependency. btw 1 defined provided in cdi-unit. scopes :)
<dependency> <groupid>javax.servlet</groupid> <artifactid>javax.servlet-api</artifactid> <version>3.0.1</version> <scope>provided</scope> </dependency>
Comments
Post a Comment