果汁 DI 介绍
Guice (英音同 'juice[果汁]') 是一个为 JDK8 及以上提供的轻量依赖注入框架.
三级标题
/**
* Animal
*/
interface Animal {
void think(String sth);
}
/**
* Dog
*/
class Dog implements Animal {
@Override
public void think(String sth) {
Logger.getGlobal().info("DOG " + sth);
}
}
/**
* Cat
*/
class Cat implements Animal {
@Override
public void think(String sth) {
Logger.getGlobal().info("CAT " + sth);
}
}
三级标题
var injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(Animal.class).annotatedWith(Names.named("Dog")).to(Dog.class);
bind(Animal.class).annotatedWith(Names.named("Cat")).to(Cat.class);
}
});
四级标题
public class App {
Animal animal;
@Inject
App(@Named("Cat") Animal animal) {
this.animal = animal;
}
}
三级标题
public static void main(String[] args) {
var injector = ...;
var app = injector.getInstance(App.class);
app.animal.think("sth");
}
果汁 DI 介绍的更多相关文章
- [Android]使用Dagger 2依赖注入 - DI介绍(翻译)
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5092083.html 使用Dagger 2依赖注入 - DI介 ...
- 02Spring_Ioc和DI介绍
什么是IOC? IoC: 控制反转, 解决程序对象紧密耦合问题(工厂+反射+ 配置文件), 将程序中原来构造对象的权限,交给IoC容器来构造,当程序需要对象,找IoC容器获取.
- [Android]Dagger2Metrics - 测量DI图表初始化的性能(翻译)
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5098943.html Dagger2Metrics - 测量D ...
- [Solution] DI原理解析及Castle、Unity框架使用
本节目录 DI介绍 DI基本原理 DI框架 Castle Windsor Unity Autofac Spring.Net DI介绍 控制反转(Inversion of Control,英文缩写为Io ...
- Spring 简单使用IoC与DI——XML配置
目录 Spring简介 导入jar包 Spring配置文件 Spring的IoC IoC简介 快速使用IoC Spring创建对象的三种方式 使用构造方法 使用实例工厂 使用静态静态工厂 Spring ...
- Spring中IOC与DI的的区别
依赖注入的前提: 有IOC的环境,也就是将必须对象的创建权交给了Spring. DI 介绍 Dependency Injection 依赖注入.需要有IOC 的环境,Spring 创建这个类的过程中, ...
- Spring框架学习笔记(1)——控制反转IOC与依赖注入DI
Spring框架的主要作用,就是提供了一个容器,使用该容器就可以创建并管理对象.比如说Dao类等,又或者是具有多依赖关系的类(Student类中包含有Teacher类的成员变量) Spring有两个核 ...
- Spring4学习回顾之路02—IOC&DI
IOC&DI介绍 ●IOC:(Inversion of Control) :控制反转(反向获取资源) 其思想是反转资源获取的方向.传统的资源上查找方式要求组件向容器发起请求查找资源,作为回应, ...
- 3、Spring的DI依赖注入
一.DI介绍 1.DI介绍 依赖注入,应用程序运行依赖的资源由Spring为其提供,资源进入应用程序的方式称为注入. Spring容器管理容器中Bean之间的依赖关系,Spring使用一种被称为&qu ...
随机推荐
- RPA-UiPath视频教程1
UiPath下载.安装.激活.第一个案例Helloworld!.参数类型.变量的介绍和使用 https://www.bilibili.com/video/av92816532 RPA直播公开课2020 ...
- jenkins页面一直在Please wait while Jenkins is getting ready to work ...
原因:因为访问官网太慢.我们只需要换一个源,不使用官网的源即可. 1.找到jenkins工作目录 find / -name *.UpdateCenter.xml 2.修改文件中的url,随后重启就行了 ...
- 纪念我逝去的n个小时
纪念我逝去的n个小时 某人的惨案要我擦屁股=.= #include <bits/stdc++.h> using namespace std; template<class T> ...
- 浅学hello world
Hello world 1.随便新建一个文件夹,存放代码 2.新建一个java文件 .后缀名为.java .Hello.java .[注意点]系统没显示后缀名的可以自己手动打开 3.编写代码 publ ...
- 关于 STrAduts
\(\mathbb{No \ hay \ cosa \ mas \ feliz \ en \ el \ mundo \ que \ ver \ tu \ sonrisa \ mi \ [数据删除]}\ ...
- Hadoop - MapReduce 过程
Hadoop - MapReduce 一.MapReduce设计理念 map--->映射 reduce--->归纳 mapreduce必须构建在hdfs之上的一种大数据离线计算框架 在线: ...
- 网格动物UVA1602
题目大意 输入n,w,h(1<=n<=10,1<=w,h<=n).求能放在w*h网格里的不同的n连块的个数(平移,旋转,翻转算一种) 首先,方法上有两个,一是打表,dfs构造连 ...
- 第二十一天python3 python的正则表达式re模块学习
python的正则表达式 python使用re模块提供了正则表达式处理的能力: 常量 re.M re.MULTILINE 多行模式 re.S re.DOTALL 单行模式 re.I re.IGNORE ...
- YII地址切换
以/开头表示跳出当前控制器 例如 return $this->render('/code/login'// 跳出当前控制器,进入Code下login视图 ,['model' => $mod ...
- Chapter 02 - Let's Get Started(C#篇)
详细解释,书上有哈.直接上代码和结果. Xcode下的自定义类 (通过new file-> cocoa class创建,保持和书中名字一样RandomController),自定义的fields ...