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 ...
随机推荐
- 洛谷 P1979 华容道 解题报告
P1979 华容道 题目描述 小\(B\)最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面, 华容道是否根本就无法完成,如果能完成, 最少需要多少时 ...
- BAT-Python面试题
Python语言特性 1 Python的函数参数传递 看两个如下例子,分析运行结果: 代码一: a = 1 def fun(a): a = 2 fun(a) print(a) # 1 代码二: a = ...
- solr单元测试
package com.taotao.rest.solr; import java.io.IOException; import org.apache.solr.client.solrj.SolrQu ...
- HDU1260DP
Tickets Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- 基于Bootstrap的遮罩层,带有加载提示
1.body中的html <div class="modal fade" id="loadingModal"> <div style=&quo ...
- Mybatis(4) 映射文件-参数处理
参数处理: 单参数处理: mybatis 不会做任何特殊处理. #{key} : key 可以写任何字段取出参数值. 测试方法: mapper接口: mapper.xml: 控制台: 多参数处理: m ...
- nginx 安装 lua-nginx-module
nginx增加lua模块 yum install -y gcc g++ gcc-c++ zlib zlib-devel openssl openssl-devel pcre pcre-devel wg ...
- 【BZOJ】1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名
[题意]给定n头牛和m对大小关系,求最坏情况下至少还需要比较几对奶牛的大小(在未确定顺序的奶牛对中随机比较) [算法]floyd求传递闭包 [题解]可达说明大小已知,则不可达点对数量就是最少比较次数. ...
- 【51NOD-0】1046 A^B Mod C
[算法]快速幂运算 [题解]快速幂的原理是把幂用二进制表示,从最低位a,次低位a2,次次低位(a2)2. #include<cstdio> long long quick_pow(long ...
- Shuffle Cards(牛客第三场+splay)
题目: 题意:将1~n的数进行m次操作,每次操作将第pi位到pi+si-1位的数字移到第一位,求最后的排列. 思路:现在还没不会写splay,在知道这是splay模板题后找了一波别人的模板,虽然过了, ...