Re: Comment on ProvidesMethods in google-guice

14 views
Skip to first unread message

google...@googlecode.com

unread,
Oct 31, 2010, 4:29:37 AM10/31/10
to google-g...@googlegroups.com
Comment by albertattard:

Hi,

What if the provider throws an exception?

Let say, for example, the "new DatabaseTransactionLog()" throws a
DatabaseException (or anything), how should this be handled?

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

google...@googlecode.com

unread,
Dec 13, 2010, 10:48:02 AM12/13/10
to google-g...@googlegroups.com
Comment by andrew.s...@gmail.com:

Is there some way to override the scope of the Provider specified by a
@Provides method either in #configure() or in a derived class?

For instance, let's say you define a @Provides method with default scope,
but then want to make another version of the Module which applies
@Singleton scope to this method. How might you specify this override?

google...@googlecode.com

unread,
Aug 21, 2011, 3:14:33 PM8/21/11
to google-g...@googlegroups.com
Comment by ashwin.jayaprakash:

How do you use this (below) on the @Inject side? What do you annotate as
`@Named("PayPal API key")`?

{{{
@Provides @PayPal
CreditCardProcessor providePayPalCreditCardProcessor(
@Named("PayPal API key") String apiKey) {
...

google...@googlecode.com

unread,
Sep 7, 2011, 2:27:58 PM9/7/11
to google-g...@googlegroups.com
Comment by cristian...@gmail.com:

Who should be the responsable of release a resource provided by a
Provider/Provider Method?, i. e. a Connection. The module? The aplication?

google...@googlecode.com

unread,
Sep 14, 2011, 9:41:27 PM9/14/11
to google-g...@googlegroups.com
Comment by kaz.r...@gmail.com:

Here is how I've used it;

I had to have providers for IFoo which needed to return different impl old
vs new,

in my module, I added providers like

@Provides @Named("old")
IFoo getFoo() {
.
}


and


@Provides @Named("new")
IFoo getFoo() {
.
}


On injector site, couple of ways to get desired IFoo

* Using constructor

@Inject
MyObj(@Named("old") IFoo foo) {
this.foo = foo;
}

* or explicitly using injector to get desired instance

IFoo foo = injector.getInstance(Key.get(IFoo.class, Names.named("old")));

injector is of course injected in calling class :)

Hope this helps.

google...@googlecode.com

unread,
May 17, 2013, 4:17:26 AM5/17/13
to google-g...@googlegroups.com
Comment by neil.je...@gmail.com:

@Provides @PayPal
CreditCardProcessor providePayPalCreditCardProcessor(
@Named("PayPal API key") String apiKey)

What if your class requires another object instance that is not a
constant? My understanding of the above is that "PayPal API key" is set up
as a constant in your Module's configure() method.

For instance. I have a class that is dependent on a java.io.File
instance. When the application needs an instance of this class it would
pass along a specific instance of java.io.File (if I were using traditional
new statement). I do not see how this is possible with Guice. There is no
way to pass along a parameter to the constructor unless it is a constant.
Help.

google...@googlecode.com

unread,
May 17, 2013, 6:25:41 AM5/17/13
to google-g...@googlegroups.com
Comment by mccu...@gmail.com:

The injector will inject any parameters of the @Provides method so they
just have to be bound in the injector, they don't have to be constants as
such. However, to supply additional constructor parameters at runtime
you'll need to use the AssistedInject extension:
http://code.google.com/p/google-guice/wiki/AssistedInject which lets you
build a factory to create instances that mix injected and traditional
passed-in parameters.

google...@googlecode.com

unread,
Nov 13, 2013, 6:46:01 AM11/13/13
to google-g...@googlegroups.com
Comment by guillaum...@gmail.com:

Why it is not possible to shared @Provides methods from a abstract module ?
Example :
<code language="java">
public abstract class ModuleSkeleton implements Module {

@Provides
final protected EntityManager provideEntityManager() {
return ...;
}

@Provides
final protected InternalUser provideInternalUser() {
return ...;
}
}
</code>

For more information:
https://code.google.com/p/google-guice/wiki/ProvidesMethods

google...@googlecode.com

unread,
Nov 13, 2013, 6:47:10 AM11/13/13
to google-g...@googlegroups.com
Comment by guillaum...@gmail.com:

Why it is not possible to shared @Provides methods from a abstract module ?
Example :

public abstract class ModuleSkeleton implements Module {

@Provides
final protected EntityManager provideEntityManager() {
return ...;
}

@Provides
final protected InternalUser provideInternalUser() {
return ...;
}
}

Reply all
Reply to author
Forward
0 new messages