zoukankan      html  css  js  c++  java
  • Guice 学习(七)常量和属性的注入( Constant and Property Inject)

    1、常量注入方式

    package com.guice.ConstantInjectDemo;
    
    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;
    
    /*
     *  绑定常量
     */
    public class ConstantInjectDemo {
        @Inject
        @Named("a")
        private int a;
    
        public static void main(String[] args) {
            ConstantInjectDemo instance = Guice.createInjector(new Module() {
    
                @Override
                public void configure(Binder binder) {
                    // 将数值22绑定到常量值a上
                    binder.bindConstant().annotatedWith(Names.named("a")).to(22);
    
                }
            }).getInstance(ConstantInjectDemo.class);
            System.out.println(instance.a);// 22
        }
    }
    

    除了能够绑定int外。在ConstantBindingBuilder类中还能够绑定其他的基本类型。
    com.google.inject.binder.ConstantBindingBuilder.to(String)
    com.google.inject.binder.ConstantBindingBuilder.to(long)
    com.google.inject.binder.ConstantBindingBuilder.to(boolean)
    com.google.inject.binder.ConstantBindingBuilder.to(double)
    com.google.inject.binder.ConstantBindingBuilder.to(float)
    com.google.inject.binder.ConstantBindingBuilder.to(short)
    com.google.inject.binder.ConstantBindingBuilder.to(char)

    2、属性注入方式

    package com.guice.ConstantInjectDemo;
    
    import java.util.Properties;
    
    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 : 绑定Properties 除了能够绑定基本类型外。还能够绑定一个Properties到Guice中。当然了,因为Properties本质上时一个Map<String,String>, 因此Guice也同意绑定一个Map<String,String>。
     * 
     * @author E468380
     */
    public class PropertiesInjectDemo {
        @Inject
        @Named("csdn")
        private String csdn;
    
        public static void main(String[] args) {
            PropertiesInjectDemo instance = Guice.createInjector(new Module() {
    
                @Override
                public void configure(Binder binder) {
                    Properties properties = new Properties();
                    properties.setProperty("csdn", "www.csdn.com");
                    Names.bindProperties(binder, properties);
                }
            }).getInstance(PropertiesInjectDemo.class);
            System.out.println(instance.csdn);
        }
    }
    
  • 相关阅读:
    BZOJ 2190: [SDOI2008]仪仗队
    BZOJ 3195: [Jxoi2012]奇怪的道路
    【BZOJ-1068】压缩 区间DP
    【BZOJ-1103】大都市meg 树状数组 + DFS序
    【BZOJ-4326】运输计划 树链剖分 + 树上差分 + 二分
    【BZOJ-3721】Final Bazarek 贪心
    【BZOJ-4690】Never Wait For Weights 带权并查集
    【BZOJ-2503】相框 并查集 + 分类讨论
    【BZOJ-3653】谈笑风生 DFS序 + 可持久化线段树
    【BZOJ-3252】攻略 DFS序 + 线段树 + 贪心
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/7259857.html
Copyright © 2011-2022 走看看