• 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();
    
        }
    
    }
    // 假设不小心一个属性绑定了多个接口怎么办? --》不能够绑定多个服务。
  • 相关阅读:
    艾伟_转载:二十行C#代码打造Ruby Markup Builder 狼人:
    艾伟_转载:WCF基本异常处理模式[上篇] 狼人:
    UVA 11389 The Bus Driver Problem(贪心)
    java list内部构造
    【人在运维囧途_04】 逃离故障的十条运维的工作经验总结
    Windows phone8 基础篇(一) 环境搭建和项目结构说明
    jquery如何把参数列严格转换成数组
    ORA27102: out of memory 故障
    卢松松:周一的文章最难写 晚上发文最合适
    Android开发环境搭建
  • 原文地址:https://www.cnblogs.com/mfmdaoyou/p/7279168.html
Copyright © 2020-2023  润新知