zoukankan      html  css  js  c++  java
  • Guice 学习(五)多接口的实现( Many Interface Implementation)

    1、接口

    /*
     * Creation : 2015年6月30日
     */
    package com.guice.InterfaceManyImpl;
    
    public interface Service {
        public void execute();
    }
    

    2、两个实现类

    
    package com.guice.InterfaceManyImpl;
    
    public class OneService implements Service {
        @Override
        public void execute() {
            System.out.println("Hello!  I'M Service 1!");
    
        }
    }
    
    package com.guice.InterfaceManyImpl;
    
    public class TwoService implements Service {
    
        @Override
        public void execute() {
            System.out.println("Hello!  I'M Service 2!");
    
        }
    
    }
    

    3、两个注解类

    package com.guice.InterfaceManyImpl;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    import com.google.inject.BindingAnnotation;
    
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ ElementType.FIELD, ElementType.PARAMETER })
    @BindingAnnotation
    public @interface One { 
    }
    package com.guice.InterfaceManyImpl;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    import com.google.inject.BindingAnnotation;
    
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ ElementType.FIELD, ElementType.PARAMETER })
    @BindingAnnotation
    public @interface Two {
    
    }

    4、多接口实现測试类

    package com.guice.InterfaceManyImpl;
    
    import com.google.inject.Binder;
    import com.google.inject.Guice;
    import com.google.inject.Inject;
    import com.google.inject.Module;
    
    /*
     * 接口的多实现:
     * 此类的结构是注入了两个service服务,注解One和OneService关联。第二个和它一样
     */
    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) {
                    //让注解类和实现类绑定
                    binder.bind(Service.class).annotatedWith(One.class).to(OneService.class);
                    binder.bind(Service.class).annotatedWith(Two.class).to(TwoService.class);
                }
            }).getInstance(InterfaceManyImpl.class);
    
            instance.oneService.execute();
            instance.twoService.execute();
        }
    
    }

    5、无注解的多接口实现測试类

    package com.guice.InterfaceManyImpl;
    
    import com.google.inject.Binder;
    import com.google.inject.Guice;
    import com.google.inject.Inject;
    import com.google.inject.Module;
    import com.google.inject.name.Named;
    import com.google.inject.name.Names;
    
    /**
     * TODO : 程序猿比較懒,不想写注解来区分多个服务则能够使用Google提供的一个叫Names的模板来生成注解
     * @author E468380
     */
    public class NoAnnotationMultiInterfaceServiceDemo {
        @Inject
        @Named("One")
        private static Service oneService;
    
        @Inject
        @Named("Two")
        private static Service twoService;
    
        public static void main(String[] args) {
            Guice.createInjector(new Module() {
    
                @Override
                public void configure(Binder binder) {
                    // 这里不同
                    binder.bind(Service.class).annotatedWith(Names.named("One")).to(OneService.class);
                    binder.bind(Service.class).annotatedWith(Names.named("Two")).to(TwoService.class);
                    binder.requestStaticInjection(NoAnnotationMultiInterfaceServiceDemo.class);
                }
            });
            NoAnnotationMultiInterfaceServiceDemo.oneService.execute();
            NoAnnotationMultiInterfaceServiceDemo.twoService.execute();
    
        }
    
    }
    

    6、静态的多接口实现測试类

    问题(1)静态注入多个服务怎么写?

    package com.guice.InterfaceManyImpl;
    
    import com.google.inject.Binder;
    import com.google.inject.Guice;
    import com.google.inject.Inject;
    import com.google.inject.Module;
    
    /**
     * TODO :也能够静态注入多个服务
     * 
     * @author E468380
     */
    public class StaticMultiInterfaceServiceDemo {
    
        @Inject
        @One
        private static Service oneService;
    
        @Inject
        @One
        private static Service twoService;
    
        public static void main(String[] args) {
            Guice.createInjector(new Module() {
    
                @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();
    
        }
    
    }
    // 假设不小心一个属性绑定了多个接口怎么办? --》不能够绑定多个服务。
  • 相关阅读:
    正则表达式提取/过滤字符串中的汉字
    笔记本磁盘中OEM分区的使用
    window10家庭版解决IIS中万维网服务的安全性中无Windows身份验证
    google插件跨域含用户请求WebApi解决的方案
    webApi前端ajax调用后端返回{"readyState":0,"status":0,"statusText":"error"}解决方案
    在VS的依赖项中引用项目
    无需QQ成为好友,直接启动QQ客户端聊天
    jquery点击添加样式,再次点击移除样式
    KVM管理工具 WebVirtMgr
    Proxmox VE:自建虚拟化方案
  • 原文地址:https://www.cnblogs.com/mfmdaoyou/p/7279168.html
Copyright © 2011-2022 走看看