Guice2.0的变化——第一部分 新的特性(上)
http://superleo.iteye.com/blog/314816
Private Modules
PrivateModules 用于创建并不需要对外可见的绑定对象。当然,这样会使得封装变得更加简单,还避免了冲突。
作者没有写关于 PrivateModules 的内容,有可能还没有更新完吧。我们就简单看一个例子:
- public class FooBarBazModule extends PrivateModule {
- protected void configurePrivateBindings() {
- bind(Foo.class).to(RealFoo.class);
- expose(Foo.class); //expose方法用于向外部暴露内部的绑定Foo
- install(new TransactionalBarModule()); //install方法允许将本PrivateModule模块嵌入到TransactionalBarModule模块中去。
- expose(Bar.class).annotatedWith(Transactional.class); //expose方法用于向外部暴露内部的绑定Bar,并令其标注为Transactional
- bind(SomeImplementationDetail.class);
- install(new MoreImplementationDetailsModule());
- }
- //向外部暴露的方法
- @Provides @Exposed
- public Baz provideBaz() {
- return new SuperBaz();
- }
- }
A module whose configuration information is hidden from its environment by default. Only bindings that are explicitly exposed will be available to other modules and to the users of the injector. This module may expose the bindings it creates and the bindings of the modules it installs.
A private module can be nested within a regular module or within another private module using install()
. Its bindings live in a new environment that inherits bindings, type converters, scopes, and interceptors from the surrounding ("parent") environment. When you nest multiple private modules, the result is a tree of environments where the injector's environment is the root.
Guice EDSL bindings can be exposed with expose()
. @Provides
bindings can be exposed with the @Exposed
annotation:
public class FooBarBazModule extends PrivateModule {
protected void configure() {
bind(Foo.class).to(RealFoo.class);
expose(Foo.class); install(new TransactionalBarModule());
expose(Bar.class).annotatedWith(Transactional.class); bind(SomeImplementationDetail.class);
install(new MoreImplementationDetailsModule());
} @Provides @Exposed
public Baz provideBaz() {
return new SuperBaz();
}
}
Private modules are implemented using parent injectors
. When it can satisfy their dependencies, just-in-time bindings will be created in the root environment. Such bindings are shared among all environments in the tree.
The scope of a binding is constrained to its environment. A singleton bound in a private module will be unique to its environment. But a binding for the same type in a different private module will yield a different instance.
A shared binding that injects the Injector
gets the root injector, which only has access to bindings in the root environment. An explicit binding that injects the Injector
gets access to all bindings in the child environment.
To promote a just-in-time binding to an explicit binding, bind it:
bind(FooImpl.class);
- Since:
- 2.0
- Author:
- jessewilson@google.com (Jesse Wilson)
Guice2.0的变化——第一部分 新的特性(上)的更多相关文章
- [C#] 回眸 C# 的前世今生 - 见证 C# 6.0 的新语法特性
回眸 C# 的前世今生 - 见证 C# 6.0 的新语法特性 序 目前最新的版本是 C# 7.0,VS 的最新版本为 Visual Studio 2017 RC,两者都尚未进入正式阶段.C# 6.0 ...
- Swift2.3 --> Swift3.0 的变化
Swift3.0语法变化 首先和大家分享一下学习新语法的技巧: 用Xcode8打开自己的Swift2.3的项目,选择Edit->Convert->To Current Swift Synt ...
- Swift3.0语法变化
写在前面 首先和大家分享一下学习新语法的技巧:用Xcode8打开自己的Swift2.3的项目,选择Edit->Convert->To Current Swift Syntax- 让Xcod ...
- Netty 4.0 新的特性及需要注意的地方
Netty 4.0 新的特性及需要注意的地方 这篇文章和你一起过下Netty的主发行版本的一些显著的改变和新特性,让你在把你的应用程序转换到新版本的时候有个概念. 项目结构改变 Netty的包名从or ...
- atitit.Servlet2.5 Servlet 3.0 新特性 jsp2.0 jsp2.1 jsp2.2新特性
atitit.Servlet2.5 Servlet 3.0 新特性 jsp2.0 jsp2.1 jsp2.2新特性 1.1. Servlet和JSP规范版本对应关系:1 1.2. Servlet2 ...
- WEBGL 2D游戏引擎研发系列 第一章 <新的开始>
WEBGL 2D游戏引擎研发系列 第一章 <新的开始> ~\(≥▽≤)/~HTML5游戏开发者社区(群号:326492427) 转载请注明出处:http://html5gamedev.or ...
- ArcGIS API for JavaScript 4.2学习笔记[0] AJS4.2概述、新特性、未来产品线计划与AJS笔记目录
放着好好的成熟的AJS 3.19不学,为什么要去碰乳臭未干的AJS 4.2? 4.2全线基础学习请点击[直达] 4.3及更高版本的补充学习请关注我的博客. ArcGIS API for JavaScr ...
- CMMI 2.0术语变化
过程域 vs. 实践域 “过程域”(Process Areas,PAs)在CMMI 2.0中变成了“实践域(Practice Areas,PAs)”.这样的改变,强调了CMMI 2.0是最佳实践的集合 ...
- vue.js学习:1.0到2.0的变化(区别)
一.生命周期 1.1.0的生命周期: 周期 解释 init 组件刚刚被创建,但Data.method等属性还没被计算出来 created 组件创建已经完成,但DOM还没被生成出来 beforeComp ...
随机推荐
- PE格式示意图
- NOIP2018普及&提高题解
第一次考$NOIP$的我已经自闭了 $CCF$告诉我们了一件事情,要对自己写的程序有信仰,相信$CCF$的数据是水的 普及组: 分数:$100+100+30+100=330$ $1.titile$: ...
- 20181022 考试记录&高级数据结构
题目 W神爷的题解 高级数据结构 T1: 其实是一道easy题,$O(n^3log n)$ 也是能卡过去的,本着要的70分的心态,最后尽然A了. 如果是正解则是$O(n^3)$,当确定你要选择的列时, ...
- mysql语句进阶
1.null mysql> create table worker(id int not null,name varchar(8) not null,pass varchar(20) not n ...
- 阿里云配置redis
一.redis redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sor ...
- acm1217教训
能用容器去做的用容器做,尽量少用数组,即使自己明确其数量的上届: #include<iostream> #include<cstring> #include<map> ...
- mysql 事务,锁,与四大隔离级别
概念 事务 原子性:事务必须是一个自动工作的单元,要么全部执行,要么全部不执行. 一致性:事务结束的时候,所有的内部数据都是正确的. 隔离性:并发多个事务时,各个事务不干涉内部数据,处理的都是另外一个 ...
- [洛谷P3401] 洛谷树
洛谷题目连接:洛谷树 题目背景 萌哒的Created equal小仓鼠种了一棵洛谷树! (题目背景是辣鸡小仓鼠乱写的QAQ). 题目描述 树是一个无环.联通的无向图,由n个点和n-1条边构成.树上两个 ...
- 冒泡排序Bubble sort
原理是临近的数字两两进行比较,按照从小到大或者从大到小的顺序进行交换, 这样一趟过去后,最大或最小的数字被交换到了最后一位, 然后再从头开始进行两两比较交换,直到倒数第二位时结束,其余类似看例子 例子 ...
- NSObject class和NSObject protocol的关系(抽象基类与协议)
[转载请注明出处] 1.接口的实现 对于接口这一概念的支持,不同语言的实现形式不同.Java中,由于不支持多重继承,因此提供了一个Interface关键词.而在C++中,通常是通过定义抽象基类的方式来 ...