scope - How to get ApplicationScoped producer in Java SE via weld? -
i'm trying implement dependency injection in java se program using weld , have problems using application scoped producer. poc of problem minimal code. mybean class:
public class mybean implements serializable{ private int value; public mybean(int value) { this.value = value; } public int getvalue() { return value; } public void setvalue(int value) { this.value = value; } } the injection point:
@path("api") public class myresource implements serializable { @inject private mybean bean; @get @path("bean") @produces(mediatype.application_json) public response getbean() { return response.ok(bean).build(); } } the producer want it's produced instance @applicationscoped is:
public class beanproducer { @produces @applicationscoped public mybean beanproducer(){ system.out.println("producing"); return new mybean(42); } } and bound in main class:
public class main { public void main(@observes containerinitialized event) { try { uri baseuri = uribuilder.fromuri("http://localhost").port(1234).build(); resourceconfig config = new resourceconfig(myresource.class) .register(jacksonfeature.class); simpleserver server = simplecontainerfactory.create(baseuri, config); system.in.read(); server.close(); } catch (ioexception e) { e.printstacktrace(); } } } when try run this, following exception:
exception in thread "main" org.jboss.weld.exceptions.deploymentexception: weld-001410: injection point [backedannotatedfield] @inject @applicationscoped private myresource.bean has non-proxyable dependencies @ org.jboss.weld.bootstrap.validator.validateinjectionpointfordeploymentproblems(validator.java:392) @ org.jboss.weld.bootstrap.validator.validateinjectionpoint(validator.java:293) @ org.jboss.weld.bootstrap.validator.validategeneralbean(validator.java:134) @ org.jboss.weld.bootstrap.validator.validateribean(validator.java:167) @ org.jboss.weld.bootstrap.validator.validatebean(validator.java:530) @ org.jboss.weld.bootstrap.concurrentvalidator$1.dowork(concurrentvalidator.java:68) @ org.jboss.weld.bootstrap.concurrentvalidator$1.dowork(concurrentvalidator.java:66) @ org.jboss.weld.executor.iterativeworkertaskfactory$1.call(iterativeworkertaskfactory.java:60) @ org.jboss.weld.executor.iterativeworkertaskfactory$1.call(iterativeworkertaskfactory.java:53) @ java.util.concurrent.futuretask.run(futuretask.java:266) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1142) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:617) @ java.lang.thread.run(thread.java:745) caused by: org.jboss.weld.exceptions.unproxyableresolutionexception: weld-001435: normal scoped bean class mybean not proxyable because has no no-args constructor - producer method [mybean] qualifiers [@any @default] declared [[backedannotatedmethod] @produces @applicationscoped public beanproducer.beanproducer()]. @ org.jboss.weld.util.proxies.getunproxyableclassexception(proxies.java:214) @ org.jboss.weld.util.proxies.getunproxyabletypeexception(proxies.java:178) @ org.jboss.weld.bootstrap.validator.validateinjectionpointfordeploymentproblems(validator.java:390) ... 12 more
if define scope producer, won't work. if set injection point scope @applicationscoped , producer without scope, works want, means have single instance of bean in entire lifecycle of app.
but setting scope of injection point @applicationscoped same default scope, ie. new instance each request.
what reason behind of this?
this happens because @applicationscoped normal scope, needs proxyable. proxyable, need no-args constructor.
to clarify 1 of points
it works want, means have single instance of bean in entire lifecycle of app. this isn't accurate. injection points don't define scope, producer does. there's no type-safe way in java, since can have producer field.
Comments
Post a Comment