Google Guice(读音:juice)是从Google AdWords项目开源出来的一款轻量级的依赖注入(DI,Dependency Injection)框架(类似于spring boot依赖注入框架)

注解:@Inject 注入类(跟spring 的Autowired类似)
注解:@Singleton单例模式(跟spring 的Singleton类似)

主类:

package com.zhangmen;import com.beust.jcommander.JCommander;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.zhangmen.context.BasicModule;
import com.zhangmen.entry.Student;
import com.zhangmen.service.TrackEventService;
import com.zhangmen.util.FileUtil;
import com.zhangmen.util.HdfsUtils;
import lombok.extern.slf4j.Slf4j;
import java.io.InputStream;@Slf4j
public class TrackDataMain {@Injectprivate HdfsUtils hdfsUtils;@Injectprivate TrackEventService trackEventService;@Injectprivate Student student;public static void main(String[] args) throws Exception {log.info("DeduplicationJob.args:{}", String.join(" ", args));try {//            Injector injector = Guice.createInjector(); //如果没有接口可以用这个,如果有接口及实现类,必须要在 BasicModule 中绑定Injector injector = Guice.createInjector(new BasicModule());TrackDataMain main = injector.getInstance(TrackDataMain.class);main.execute(args);} catch (Throwable ex) {log.error(ex.getMessage(), ex);throw ex;}}private void execute(String[] args) throws Exception {InputStream in = TrackDataMain.class.getClassLoader().getResourceAsStream("./application.properties");String[] loadConfigs = FileUtil.loadConfigs(in);JCommander.newBuilder().addObject(new Object[]{student}).acceptUnknownOptions(true).build().parse(loadConfigs);System.out.println(student.toString());System.out.println(student.getName());System.out.println("输出语句!!!");String hdfsConfTmp = hdfsUtils.getHdfsConfTmp();System.out.println(hdfsConfTmp);System.out.println(trackEventService.getName());}
}

绑定接口及实现类,让guice发现

package com.zhangmen.context;import com.google.inject.AbstractModule;
import com.zhangmen.service.TrackEventService;
import com.zhangmen.service.impl.TrackEventServiceImpl;
public class BasicModule extends AbstractModule {@Overrideprotected void configure() {bind(TrackEventService.class).to(TrackEventServiceImpl.class);
//        bind(SourceDelegation.class).to(KafkaSourceDelegation.class);}
}

接口及实现类
注意: default 关键字的用法

package com.zhangmen.service;/*** @Author zsq* @create 2021/6/17 17:35* @Description:*/
public interface TrackEventService {/*** default 修饰方法,则实现类的值优先于接口的方法* @return*/default String getName() {return null;}
}
package com.zhangmen.service.impl;import com.zhangmen.service.TrackEventService;/*** @Author zsq* @create 2021/6/17 17:35* @Description:*/
public class TrackEventServiceImpl implements TrackEventService {@Overridepublic String getName() {return "来玩耍呀";}
}

实体类: 通过 jcommander 来注入配置参数

package com.zhangmen.entry;import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.google.inject.Singleton;
import lombok.Data;@Singleton
@Data
@Parameters(separators = "=")
public class Student {@Parameter(names = "--kafka.consumer.servers")private String name;@Parameter(names = "--kafka.consumer.group.id")private String age;
}

Google Guice的使用方式相关推荐

  1. guice google_与Google Guice的动手实践

    guice google by Sankalp Bhatia 通过Sankalp Bhatia 与Google Guice的动手实践 (A hands-on session with Google G ...

  2. Google Guice使用入门

    2019独角兽企业重金招聘Python工程师标准>>> 本文通过范例简单地介绍Google Guice的使用,通过下面的范例我们可以知道,Google Guice的使用非常简单. G ...

  3. guice 实例_使用Google Guice消除实例之间的歧义

    guice 实例 如果接口有多个实现,则Google guice提供了一种精巧的方法来选择目标实现. 我的示例基于Josh Long ( @starbuxman )的出色文章,内容涉及Spring提供 ...

  4. 使用Google Guice消除实例之间的歧义

    如果接口有多个实现,则Google guice提供了一种精巧的方法来选择目标实现. 我的示例基于Josh Long ( @starbuxman )的出色文章,内容涉及Spring提供的类似机制. 因此 ...

  5. Google Guice 一个轻量级的依赖注入框架

    1.美图 2.概述 2.1 背景 在做项目的时候,看见有段代码直接是使用Google Guice 注入了avaitor表达式. 2.1 官网 Github 主页:https://github.com/ ...

  6. Google Guice使用入门(转)

    本文通过范例简单地介绍Google Guice的使用,通过下面的范例我们可以知道,Google Guice的使用非常简单. Google Guice需要使用JDK1.5以上java环境. 下载Goog ...

  7. Google Guice范例解说之使用入门

    http://www.cnblogs.com/xd502djj/archive/2012/06/25/2561414.html Google Guice范例解说之使用入门 http://code.go ...

  8. 超轻量级DI容器框架Google Guice与Spring框架的区别教程详解及其demo代码片段分享...

    超轻量级DI容器框架Google Guice与Spring框架的区别教程详解及其demo代码片段分享 DI框架 Google-Guice入门介绍 转载于:https://www.cnblogs.com ...

  9. guice 框架_玩! 框架+ Google Guice

    guice 框架 在我目前正在工作的项目中,我们开始使用Google Guice. 对于那些不知道的人, Google Guice是一个依赖项注入框架. 依赖项注入的基本思想是提供一个其依赖的类,而不 ...

最新文章

  1. [Unity UGUI]点击和长按组件
  2. python中怎么把值添加进列表_在Python中为子列表添加值
  3. Push Notification (1)Google C2DM 服务
  4. C++学习笔记-----函数调用时的决议:名字查找,重载决议,可访问性检测
  5. String Stringbuilder StringBuffer的区别和应用
  6. gedit把关键字符替换为回车键
  7. Java DSL简介(收集整理)
  8. CruiseControl.NET与TFS结合的配置文件
  9. 跨域会报40几_总结一下跨域的几种情况
  10. Oracle中 char varchar varchar2的区别
  11. MySQLdb 1031 Error
  12. mysql5.7修改默认编码_mysql5.7设置默认编码
  13. aspf ftp_【解析】文件传输协议:FTP、TFTP、SFTP有什么区别?
  14. 用在线RaxML构建系统发育树
  15. VM虚拟机 Liunx修改ip地址
  16. 第二章 Qt窗体应用------修改标题栏图标
  17. android自定义带进度条的圆形图片
  18. 夜深模拟器调试下载的app
  19. 性能工具之Jmeter脚本python启动
  20. 【课程设计|MFC】人民币大小写转换(含课程报告+源码)

热门文章

  1. Tecnomatix Plant Simulation 14 学习之路(五)
  2. <python开发> python开发 环境搭建(windows)
  3. iOS 10 beta1 安装
  4. PS 套索选区工具(1) 套索工具基础使用
  5. 测试设计-基于规格说明
  6. 进销存软件:销售开单如何设置负库存开单或筛除负库存商品?
  7. Black and white(MST)
  8. 支付清结算之渠道路由
  9. iOS中 支付宝钱包详解
  10. 分享30个小升初简历模板,总有一款适合您