Java Doc for Binder.java in  » Testing » Ejb3Unit » com » bm » ejb3guice » inject » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » Ejb3Unit » com.bm.ejb3guice.inject 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


com.bm.ejb3guice.inject.Binder

All known Subclasses:   com.bm.ejb3guice.inject.BinderImpl,
Binder
public interface Binder (Code)
Collects configuration information (primarily bindings) which will be used to create an Injector . Guice provides this object to your application's Module implementors so they may each contribute their own bindings and other registrations.

The Guice Binding EDSL

Guice uses an embedded domain-specific language, or EDSL, to help you create bindings simply and readably. This approach is great for overall usability, but it does come with a small cost: it is difficult to learn how to use the Binding EDSL in the usual way -- by reading method-level javadocs. Instead, you should consult this series of examples below. To save space, these examples omit the opening binder. , just as you will if your module extends AbstractModule .
 bind(ServiceImpl.class);
This statement does essentially nothing; it "binds the ServiceImpl class to itself" and does not change Guice's default behavior. You may still want to use this if you prefer your Module class to serve as an explicit manifest for the services it provides. Also, in rare cases, Guice may be unable to validate a binding at injector creation time unless it is given explicitly.
 bind(Service.class).to(ServiceImpl.class);
Specifies that a request for a Service instance with no binding annotations should be treated as if it were a request for a ServiceImpl instance. This overrides the function of any ImplementedBy @ImplementedBy or ProvidedBy @ProvidedBy annotations found on Service , since Guice will have already "moved on" to ServiceImpl before it reaches the point when it starts looking for these annotations.
 bind(Service.class).toProvider(ServiceProvider.class);
In this example, ServiceProvider must extend or implement Provider . This binding specifies that Guice should resolve an unannotated injection request for Service by first resolving an instance of ServiceProvider in the regular way, then calling Provider.get get() on the resulting Provider instance to obtain the Service instance.

The Provider you use here does not have to be a "factory"; that is, a provider which always creates each instance it provides. However, this is generally a good practice to follow. You can then use Guice's concept of Scope scopes to guide when creation should happen -- "letting Guice work for you".

 bind(Service.class).annotatedWith(Red.class).to(ServiceImpl.class);
Like the previous example, but only applies to injection requests that use the binding annotation @Red . If your module also includes bindings for particular values of the @Red annotation (see below), then this binding will serve as a "catch-all" for any values of @Red that have no exact match in the bindings.
 bind(ServiceImpl.class).in(Singleton.class);
 // or, alternatively
 bind(ServiceImpl.class).in(Scopes.SINGLETON);
Either of these statements places the ServiceImpl class into singleton scope. Guice will create only one instance of ServiceImpl and will reuse it for all injection requests of this type. Note that it is still possible to bind another instance of ServiceImpl if the second binding is qualified by an annotation as in the previous example. Guice is not overly concerned with preventing you from creating multiple instances of your "singletons", only with enabling your application to share only one instance if that's all you tell Guice you need.

Note: a scope specified in this way overrides any scope that was specified with an annotation on the ServiceImpl class.

Besides Singleton / Scopes.SINGLETON , there are servlet-specific scopes available in com.bm.ejb3guice.servlet.ServletScopes , and your Modules can contribute their own custom scopes for use here as well.

 bind(new TypeLiteral<PaymentService<CreditCard>>() {})
 .to(CreditCardPaymentService.class);
This admittedly odd construct is the way to bind a parameterized type. It tells Guice how to honor an injection request for an element of type PaymentService . The class CreditCardPaymentService must implement the PaymentService interface. Guice cannot currently bind or inject a generic type, such as Set ; all type parameters must be fully specified.
 bind(Service.class).toInstance(new ServiceImpl());
 // or, alternatively
 bind(Service.class).toInstance(SomeLegacyRegistry.getService());
In this example, your module itself, not Guice, takes responsibility for obtaining a ServiceImpl instance, then asks Guice to always use this single instance to fulfill all Service injection requests. When the Injector is first created, it will automatically perform field and method injection for this instance, but any injectable constructor on ServiceImpl is simply ignored. Note that using this approach results in "eager loading" behavior that you can't control.
 bindConstant().annotatedWith(ServerHost.class).to(args[0]);
Sets up a constant binding. Constant bindings are typeless in Guice; you can provide the values in a variety of types and the values can be injected in a variety of types; Guice performs the standard type conversions for you behind the scenes. Because of this "typelessness", constant injections must always be annotated.
 Color("red") Color red; // A member variable (field)
 . . .
 red = MyModule.class.getField("red").getAnnotation(Color.class);
 bind(Service.class).annotatedWith(red).to(RedService.class);
If your binding annotation has parameters you can apply different bindings to different specific values of your annotation. Getting your hands on the right instance of the annotation is a bit of a pain -- one approach, shown above, is to apply a prototype annotation to a field in your module class, so that you can read this annotation instance and give it to Guice.
 bind(Service.class)
 .annotatedWith(Names.named("blue"))
 .to(BlueService.class);
Differentiating by names is a common enough use case that we provided a standard annotation, com.bm.ejb3guice.name.Named @Named . Because of Guice's library support, binding by name is quite easier than in the arbitrary binding annotation case we just saw. However, remember that these names will live in a single flat namespace with all the other names used in your application.

The above list of examples is far from exhaustive. If you can think of how the concepts of one example might coexist with the concepts from another, you can most likely weave the two together. If the two concepts make no sense with each other, you most likely won't be able to do it. In a few cases Guice will let something bogus slip by, and will then inform you of the problems at runtime, as soon as you try to create your Injector.

The other methods of Binder such as Binder.bindScope , Binder.bindInterceptor , Binder.install , Binder.requestStaticInjection , Binder.addError and Binder.currentStage are not part of the Binding EDSL; you can learn how to use these in the usual way, from the method documentation.
author:
   crazybob@google.com (Bob Lee)





Method Summary
 voidaddError(String message, Object... arguments)
     Records an error message which will be presented to the user at a later time.
 voidaddError(Throwable t)
     Records an exception, the full details of which will be logged, and the message of which will be presented to the user at a later time.
 LinkedBindingBuilder<T>bind(Key<T> key)
     See the EDSL examples at Binder .
 AnnotatedBindingBuilder<T>bind(TypeLiteral<T> typeLiteral)
     See the EDSL examples at Binder .
 AnnotatedBindingBuilder<T>bind(Class<T> type)
     See the EDSL examples at Binder .
 AnnotatedConstantBindingBuilderbindConstant()
     See the EDSL examples at Binder .
 voidbindInterceptor(Matcher<? super Class<?>> classMatcher, Matcher<? super Method> methodMatcher, MethodInterceptor... interceptors)
     Binds a method interceptor to methods matched by class and method matchers.
Parameters:
  classMatcher - matches classes the interceptor should apply to.
 voidbindScope(Class<? extends Annotation> annotationType, Scope scope)
     Binds a scope to an annotation.
 StagecurrentStage()
     Gets the current stage.
 Provider<T>getProvider(Key<T> key)
     Returns the provider used to obtain instances for the given injection key. The returned will not be valid until the Injector has been created.
 Provider<T>getProvider(Class<T> type)
     Returns the provider used to obtain instances for the given injection type. The returned will not be valid until the Injector has been created.
 voidinstall(Module module)
     Uses the given module to configure more bindings.
 voidrequestStaticInjection(Class... types)
     Upon successful creation, the Injector will inject static fields and methods in the given classes.



Method Detail
addError
void addError(String message, Object... arguments)(Code)
Records an error message which will be presented to the user at a later time. Unlike throwing an exception, this enable us to continue configuring the Injector and discover more errors. Uses String.format(StringObject[]) to insert the arguments into the message.



addError
void addError(Throwable t)(Code)
Records an exception, the full details of which will be logged, and the message of which will be presented to the user at a later time. If your Module calls something that you worry may fail, you should catch the exception and pass it into this.



bind
LinkedBindingBuilder<T> bind(Key<T> key)(Code)
See the EDSL examples at Binder .



bind
AnnotatedBindingBuilder<T> bind(TypeLiteral<T> typeLiteral)(Code)
See the EDSL examples at Binder .



bind
AnnotatedBindingBuilder<T> bind(Class<T> type)(Code)
See the EDSL examples at Binder .



bindConstant
AnnotatedConstantBindingBuilder bindConstant()(Code)
See the EDSL examples at Binder .



bindInterceptor
void bindInterceptor(Matcher<? super Class<?>> classMatcher, Matcher<? super Method> methodMatcher, MethodInterceptor... interceptors)(Code)
Binds a method interceptor to methods matched by class and method matchers.
Parameters:
  classMatcher - matches classes the interceptor should apply to. Forexample: only(Runnable.class) .
Parameters:
  methodMatcher - matches methods the interceptor should apply to. Forexample: annotatedWith(Transactional.class) .
Parameters:
  interceptors - to bind



bindScope
void bindScope(Class<? extends Annotation> annotationType, Scope scope)(Code)
Binds a scope to an annotation.



currentStage
Stage currentStage()(Code)
Gets the current stage.



getProvider
Provider<T> getProvider(Key<T> key)(Code)
Returns the provider used to obtain instances for the given injection key. The returned will not be valid until the Injector has been created. The provider will throw an IllegalStateException if you try to use it beforehand.



getProvider
Provider<T> getProvider(Class<T> type)(Code)
Returns the provider used to obtain instances for the given injection type. The returned will not be valid until the Injector has been created. The provider will throw an IllegalStateException if you try to use it beforehand.



install
void install(Module module)(Code)
Uses the given module to configure more bindings.



requestStaticInjection
void requestStaticInjection(Class... types)(Code)
Upon successful creation, the Injector will inject static fields and methods in the given classes.
Parameters:
  types - for which static members will be injected



www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.