Guice Learning (v) Implementation of multiple interfaces (many Interface implementation)

Source: Internet
Author: User

1. Interface
/* * Creation : 2015年6月30日 */package com.guice.InterfaceManyImpl;publicinterface Service {    publicvoidexecute();}
2, two implementation classes
package  Com.guice.InterfaceManyImpl; public  class  oneservice  Span class= "Hljs-keyword" >implements  service  {  @Override  public  void   execute  () {System.out.println ( "hello! I ' M Service 1! ");}  
package com.guice.InterfaceManyImpl;publicclass TwoService implements Service {    @Override    publicvoidexecute() {        System.out.println("Hello!  I‘M Service 2!");    }}
3, two annotation classes
Packagecom. Guice. Interfacemanyimpl;Import Java. Lang. Annotation. ElementType;Import Java. Lang. Annotation. Retention;Import Java. Lang. Annotation. Retentionpolicy;Import Java. Lang. Annotation. Target;Importcom. Google. Inject. Bindingannotation;@Retention (Retentionpolicy. RUNTIME) @Target ({ElementType. FIELD, ElementType. PARAMETER}) @BindingAnnotationpublic @interface one {}
Packagecom. Guice. Interfacemanyimpl;Import Java. Lang. Annotation. ElementType;Import Java. Lang. Annotation. Retention;Import Java. Lang. Annotation. Retentionpolicy;Import Java. Lang. Annotation. Target;Importcom. Google. Inject. Bindingannotation;@Retention (Retentionpolicy. RUNTIME) @Target ({ElementType. FIELD, ElementType. PARAMETER}) @BindingAnnotationpublic @interface the {}
4. Multi-Interface implementation test class
Packagecom. Guice. Interfacemanyimpl;Importcom. Google. Inject. Binder;Importcom. Google. Inject. Guice;Importcom. Google. Inject. Inject;Importcom. Google. Inject. Module;/* Multiple implementations of the interface: * The structure of this class is injected with two service services, annotated one and oneservice associated, the second is the same as it */public class Interfacemanyimpl {@Inject @One private Service Oneservice;@Inject @Two Private Service Twoservice;public static void Main (string[] args) {Interfacemanyimpl instance = Guice. Createinjector(New Module () {@Override public void Configure (Binder binder) {//Let the annotation class and the implementation class bind Binder. Bind(Service. Class). Annotatedwith(One. Class). to(Oneservice. Class);Binder. Bind(Service. Class). Annotatedwith(Both. Class). to(Twoservice. Class);}        }). getinstance(Interfacemanyimpl. Class);Instance. Oneservice. Execute();Instance. Twoservice. Execute();}}
5. Non-annotated multi-interface implementation test class
Packagecom. Guice. Interfacemanyimpl;Importcom. Google. Inject. Binder;Importcom. Google. Inject. Guice;Importcom. Google. Inject. Inject;Importcom. Google. Inject. Module;Importcom. Google. Inject. Name. Named;Importcom. Google. Inject. Name. Names;/** * TODO: Programmers are lazy and don't want to write annotations to differentiate multiple services you can use a template called names provided by Google to generate annotations * @author E468380 * /public class Noannotationmultiinterfaceservicedemo {@Inject @Named ("One") private static Service Oneservice;@Inject @Named ("both") private static Service Twoservice;public static void Main (string[] args) {Guice. Createinjector(New Module () {@Override public void Configure (Binder binder) {//different here Binder. Bind(Service. Class). Annotatedwith(Names. Named("One")). to(Oneservice. Class);Binder. Bind(Service. Class). Annotatedwith(Names. Named("both")). to(Twoservice. Class);Binder. Requeststaticinjection(Noannotationmultiinterfaceservicedemo. Class);}        });Noannotationmultiinterfaceservicedemo. Oneservice. Execute();Noannotationmultiinterfaceservicedemo. Twoservice. Execute();}}
6. Static multi-interface implementation test class

Question (1) How do I write a static injection of multiple services?

 PackageCom.guice.InterfaceManyImpl;ImportCom.google.inject.Binder;ImportCom.google.inject.Guice;ImportCom.google.inject.Inject;ImportCom.google.inject.Module;/** * TODO: You can also inject multiple services statically * * @author E468380 */ Public  class staticmultiinterfaceservicedemo {    @Inject    @One    Private StaticService Oneservice;@Inject    @One    Private StaticService Twoservice; Public Static void Main(string[] args) {Guice.createinjector (NewModule () {@Override             Public void Configure(Binder binder)                {Binder.bind (Service.class). Annotatedwith (One.class). to (Oneservice.class);                Binder.bind (Service.class). Annotatedwith (Two.class). to (Twoservice.class);            Binder.requeststaticinjection (Staticmultiinterfaceservicedemo.class);        }        });        StaticMultiInterfaceServiceDemo.oneService.execute ();    StaticMultiInterfaceServiceDemo.twoService.execute (); }}//If you are not careful what happens if a property is bound to more than one interface? --"cannot bind to multiple services. 

Guice Learning (v) Implementation of multiple interfaces (many Interface implementation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.