Revision: 1414
Author: sberlin
Date: Sun Nov 21 07:17:35 2010
Log: Edited wiki page ThrowingProviders through web user interface.
http://code.google.com/p/google-guice/source/detail?r=1414
Modified:
/wiki/ThrowingProviders.wiki
=======================================
--- /wiki/ThrowingProviders.wiki Fri Dec 5 20:25:48 2008
+++ /wiki/ThrowingProviders.wiki Sun Nov 21 07:17:35 2010
@@ -8,12 +8,13 @@
* Injecting an instance directly rather than a Provider can cause
creation of the injected object to fail.
* Exceptions cannot be advertised in the API.
-!ThrowingProviders are an alternative to Providers that allow a checked
exception to be thrown.
+The !ThrowingProviders extension offers an alternative to Providers that
allow a checked exception to be thrown.
* API consistent with Provider.
* Scopable
* Standard binding DSL
-The !ThrowingProvider interface is similar to Provider, but with a generic
Exception type:
+====Guice 2.0 -- !ThrowingProvider====
+Guice 2.0 offers the !ThrowingProvider interface. It is similar to
Provider, but with a generic Exception type:
{{{
public interface ThrowingProvider<T,E extends Exception> {
T get() throws E;
@@ -23,7 +24,24 @@
{{{
public interface FeedProvider<T> extends ThrowingProvider<T,
FeedUnavailableException> { }
}}}
-After implementing our !WordlNewsFeedProvider and !SportsFeedProvider, we
bind them in our module using !ThrowingProviderBinder:
+
+====Guice 3.0 -- !CheckedProvider====
+Guice 3.0 offers the !CheckedProvider interface. It improves
upon !ThrowingProvider by allowing more than one exception type to be
thrown:
+{{{
+public interface CheckedProvider<T> {
+ T get() throws Exception;
+}
+}}}
+For each application exception, create an interface that extends
the !CheckedProvider. For our news widget application, we created
the !FeedProvider interface that throws a !FeedUnavailableException
and !SecurityException:
+{{{
+public interface FeedProvider<T> extends CheckedProvider<T> {
+ T get() throws FeedUnavailableException, SecurityException
+}
+
+}}}
+
+==Binding the provider==
+After implementing our !WorldNewsFeedProvider and !SportsFeedProvider, we
bind them in our module using !ThrowingProviderBinder:
{{{
public static class FeedModule extends AbstractModule {
protected void configure() {
@@ -41,7 +59,32 @@
}
}
}}}
-Finally, we can inject the !FeedProviders throughout our application code.
Whenever we call get(), the compiler reminds us to handle
the !FeedUnavailableException:
+
+===Using @!CheckedProviders _(new in Guice 3.0)_===
+You can also bind checked providers use an @Provides-like syntax:
@!CheckedProvides. This has all the benefits of [ProvidesMethods @Provides
methods], and also allows you to specify exceptions.
+{{{
+public static class FeedModule extends AbstractModule {
+ protected void configure() {}
+
+ @CheckedProvides(FeedProvider.class) // define what interface will
provide it
+ @HourlyScoped // scoping annotation
+ @WorldNews // binding annotation
+ BbcFeed provideWorld(FeedFactory factory)
+ throws FeedUnavailableException, SecurityException {
+ return factory.tryToFeed("bbc"); // may throw an exception
+ }
+
+ @CheckedProvides(FeedProvider.class) // define what interface will
provide it
+ @QuarterlyHourlyScoped // scoping annotation
+ @Sports // binding annotation
+ BbcFeed provideSports(FeedFactory factory)
+ throws FeedUnavailableException, SecurityException {
+ return factory.tryToFeed("bbc"); // may throw an exception
+ }
+}}}
+
+==Injecting the provider==
+Finally, we can inject the !FeedProviders throughout our application code.
Whenever we call get(), the compiler reminds us to handle
the !FeedUnavailableException and any other exceptions declared in the
interface:
{{{
public class BbcNewsWidget {
private final FeedProvider<BbcFeed> worldNewsFeedProvider;
--
You received this message because you are subscribed to the Google Groups
"google-guice-dev" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-guice-dev?hl=en.