Re: Comment on InjectingProviders in google-guice

39 views
Skip to first unread message

google...@googlecode.com

unread,
Aug 1, 2010, 4:46:45 PM8/1/10
to google-g...@googlegroups.com
Comment by u.int.32.t:

What if the provided type is supposed to be thread-local scoped?

It seems to me that the provider will be providing you with unscoped
objects, no?


For more information:
http://code.google.com/p/google-guice/wiki/InjectingProviders

google...@googlecode.com

unread,
Aug 1, 2010, 4:59:50 PM8/1/10
to google-g...@googlegroups.com
Comment by u.int.32.t:

Nevermind, I realized that the provider is scoped. Doh.

google...@googlecode.com

unread,
Feb 13, 2011, 9:38:14 PM2/13/11
to google-g...@googlegroups.com
Comment by emaild...@gmail.com:

I created an EntityManagerProvider<EntityManager> to create an
EntityManager on demand for my application. Now I would like to write some
tests and I`m planing to define a test database. But my
EntityManagerProvider<EntityManager> generates an EntityManager that uses
my dev base, not the test one. How can I use an EntityManagerProvider to
inject my test database during the tests?

google...@googlecode.com

unread,
Sep 21, 2011, 9:49:34 PM9/21/11
to google-g...@googlegroups.com
Comment by vfunst...@gmail.com:

This is a nitpick and unrelated to the scoping example, but I think you
would want to rewrite the `logConnection` method as follows:

{{{public void logConnectException(UnreachableException e) {
int failCt = failureCount.incrementAndGet();
User user = userProvider.get();
System.out.println("Connection failed for " + user + ": " +
e.getMessage());
System.out.println("Failure count: " + failCt);

google...@googlecode.com

unread,
Sep 21, 2011, 9:53:43 PM9/21/11
to google-g...@googlegroups.com

google...@googlecode.com

unread,
Sep 21, 2011, 9:57:48 PM9/21/11
to google-g...@googlegroups.com
Comment by vfunst...@gmail.com:

This is a nitpick and unrelated to the scoping example, but I think you

would want to rewrite the `logConnectionException` method in that example
as follows to avoid incrementing the counter twice:

google...@googlecode.com

unread,
Sep 21, 2011, 10:01:52 PM9/21/11
to google-g...@googlegroups.com
Comment by vfunst...@gmail.com:

This is a nitpick and unrelated to point of the scoping example, but I

think you would want to rewrite the `logConnectionException` method in that
example as follows to avoid incrementing the counter twice:

{{{
public void logConnectException(UnreachableException e) {


User user = userProvider.get();
System.out.println("Connection failed for " + user + ": " +
e.getMessage());

System.out.println("Failure count: " + failureCount.incrementAndGet());

google...@googlecode.com

unread,
Jun 15, 2012, 4:33:58 PM6/15/12
to google-g...@googlegroups.com
Comment by orlando....@gmail.com:

I saw this code in a Misko Hevery's presentation: "Conditionals VS
Polymorphism. He explains how to create a provider that selects between two
types of update based on the state of the variable isI18N. He is injecting
that value in the provider definition. However, what I can't see is where
that value comes from? it is injected into the provider but where the value
is? In a Module?

Java? Use Guice!
<code language="java">
class UpdateProvider implements Provider<Update> {

@Inject Provider<I18NUpdate> i18n;
@Inject Provider<NonI18NUpdate> ni18n;
@Inject @Named("i18n_ENABLED") boolean isI18N;

Update get() {
return isI18N ? i18n.get() : ni18n.get();
}
}
</code>

google...@googlecode.com

unread,
Jun 15, 2012, 4:36:49 PM6/15/12
to google-g...@googlegroups.com
Comment by orlando....@gmail.com:

I saw this code in a Misko Hevery's presentation: "Conditionals VS
Polymorphism. He explains how to create a provider that selects between two
types of update based on the state of the variable isI18N. He is injecting
that value in the provider definition. However, what I can't see is where
that value comes from? it is injected into the provider but where the value
is? In a Module?

Java? Use Guice!

{{{
class UpdateProvider implements Provider<Update> {

@Inject Provider<I18NUpdate> i18n;
@Inject Provider<NonI18NUpdate> ni18n;

@Inject @Named("i18n_ENABLED") boolean isI18N;

Update get() {
return isI18N ? i18n.get() : ni18n.get();
}
}

}}}

google...@googlecode.com

unread,
Jul 7, 2012, 10:27:30 PM7/7/12
to google-g...@googlegroups.com
Comment by txmikes...@gmail.com:

@orlando.garcia - this isn't explained well in the documentation, but you
can use Constant bindings to bind @Named parameters to either arbitrary
values (in the module) or more commonly, to load in a properties file. See
the javadoc for the
[http://google-guice.googlecode.com/git/javadoc/com/google/inject/name/Names.html
Names] helper class, there are functions there to bind all the elements in
a properties file or a map.

google...@googlecode.com

unread,
Nov 2, 2012, 12:54:40 PM11/2/12
to google-g...@googlegroups.com
Comment by Ahmad.Ag...@gmail.com:

@Inject methods will be called when your class is initiated. for example:
{{{
public intefrace ITest { void printMe(); }
public class Test implements ITest { @Override public void printMe() {
System.out.println("Test me");}}

public interface IBilling {}
public class Billing implements IBilling {
final ITest test;
@Inject public Billing(ITest test) { this.test = test; }
@Inject public void InjectedMethod() { test.printMe(); }
}

public class BillingModule extends AbstractModule {

@Override
protected void configure() {
bind(ITest.class).to(Test.class);
bind(IBilling.class).to(Billing.class);
}
}

public class Tester
{
public static void main(String[] args) {
Injector inj = Guice.createInjector(new BillingModule());
IBillingService billing = inj.getInstance(IBillingService.class);

{

google...@googlecode.com

unread,
Nov 2, 2012, 1:00:23 PM11/2/12
to google-g...@googlegroups.com
Comment by Ahmad.Ag...@gmail.com:

@Inject methods will be called when your class is initiated. for example:

{{{
public intefrace ITest {
void printMe();
}

public class Test implements ITest {
@Override public void printMe() {
System.out.println("Test me");
}
}

public interface IBilling {}

public class Billing implements IBilling {
final ITest test;
@Inject
public Billing(ITest test) {
this.test = test;
}

@Inject
public void InjectedMethod() {
test.printMe();
}
}

public class BillingModule extends AbstractModule {
@Override
protected void configure() {
bind(ITest.class).to(Test.class);
bind(IBilling.class).to(Billing.class);
}
}

public class Tester{
public static void main(String[] args) {
Injector inj = Guice.createInjector(new BillingModule());
IBilling billing = inj.getInstance(IBilling.class);
}
}

The above code will print: Test me.
Note when the Tester::main is called, it calls the injector to create
Billing class. Billing class is called, and the injectedMethod as well.

google...@googlecode.com

unread,
Jun 14, 2013, 2:50:31 PM6/14/13
to google-g...@googlegroups.com
Comment by martin.j...@gmail.com:

I don't think it's clear from these examples that you don't have to do
anything different in your Module to use this functionality. It just works.
I think the article would be well served by a fully functional example that
you can just run and see it work.

{{{
import java.util.concurrent.atomic.AtomicInteger;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.Provider;

public class InjectProviderExample {
public static void main(String... args) {
Module myModule = new AbstractModule() {
@Override
protected void configure() {
// NOTICE THERE IS NO toProvider HERE
bind(Foo.class).to(FooImpl.class);
// bind(Foo.class).to(FooImpl.class).in(Singleton.class);
}
};

Injector inj = Guice.createInjector(myModule);
Bar myBar = inj.getInstance(Bar.class);
Foo fooOne = myBar.getFoo();
Foo fooTwo = myBar.getFoo();

if(fooOne == fooTwo) {
System.out.println("This should not happen unless Singleton scope is
set");
} else {
System.out.format("FooOne id: %d\tFooTwo id: %d%n", fooOne.getId(),
fooTwo.getId());
}
}

public static interface Foo {
int getId();
}

public static class FooImpl implements Foo {
private static final AtomicInteger counter = new AtomicInteger();
private int uid = counter.incrementAndGet();

public int getId() {
return uid;
}
}

public static class Bar {
private final Provider<Foo> myFooProvider;

@Inject
public Bar(Provider<Foo> fooProvider) {
myFooProvider = fooProvider;
}

public Foo getFoo() {
return myFooProvider.get();

google...@googlecode.com

unread,
Aug 27, 2013, 1:23:07 PM8/27/13
to google-g...@googlegroups.com
Comment by lontentr...@gmail.com:

import java.util.concurrent.atomic.AtomicInteger;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.Provider;

public class InjectProviderExample {
public static void main(String... args) {
Module myModule = new AbstractModule() {
@Override
protected void configure() {
// NOTICE THERE IS NO toProvider HERE
bind(Foo.class).to(FooImpl.class);
//
bind(Foo.class).to(FooImpl.class).in(Singleton.class);
}
};

Injector inj = Guice.createInjector(myModule);
Bar myBar = inj.getInstance(Bar.class);
Foo fooOne = myBar.getFoo();
Foo fooTwo = myBar.getFoo();

you can also take on this blog site:
http://nfljerseyscheapjerseyscheapnfljerseys.blogspot.com
Reply all
Reply to author
Forward
0 new messages