Wednesday, 15 June 2011

java - Guice constructor injection of String dependency (or other unbound primitive)? -



java - Guice constructor injection of String dependency (or other unbound primitive)? -

here code:

public class injectedclass{ private final dependency dependency; private final string string; @inject public injectedclass(dependency dependency, string string){ this.dependency=dependency; this.string=string; } }

let's have bound dependency in module, not have binding string there. how/what guice take default string argument? in (more complicated) test case, seems setting "", logic how not apparent me , not know if deterministic behavior.

by default guice instantiate object has either

an @inject-marked non-private constructor a non-private no-args constructor a default constructor (no constructor)

because string has no-args public constructor, new string() create string equivalent ""

this deterministic behavior, , happen type has above conditions. if concerned it, can do

binder().requireatinjectonconstructors()

then guice study missing binding. additionally there future enhancement beingness planned prevent java.lang.* types beingness bound without qualifier/bindingannotation. prevent core types string or integer beingness automatically bound "" or 0 because injected them.

java constructor dependency-injection guice

No comments:

Post a Comment