Google Guice 之绑定1
绑定和依赖注入区别
绑定,使用时 需要通过 injector 显示获取
依赖注入,只需要显示获取主类,他的依赖是通过@Injector 和 绑定关系 隐式注入的
http://blog.csdn.net/jing_le/article/category/1403309
http://blog.csdn.net/java_le/article/details/24850355
- Injector in = Guice.createInjector(new MyModule()) ;
- Hello hello = in.getInstance(Hello.class)
Google Guice 绑定2
http://blog.csdn.net/java_le/article/details/24850777
上一章简单介绍了,如何使用Guice框架来完成接口(父类)和具体的实现类(子类)之间的关联。通过MyModule中:
binder.bind(Hello.class).to(HelloImpl.class).in(Singleton.class) ;
来实现关联关系,这节介绍通过注解来实现关联关系
(1)首先还是Hello.java接口(注意类头上的注解,描述了这个接口要绑定的实现类)
- @ImplementedBy(HelloImpl.class)
- public interface Hello {
- void sayHello() ;
- }
Hello.java这个接口的具体实现类:HelloImpl.java(类头上的注解,这类是单列还是动态创建)
- @Singleton
- public class HelloImpl implements Hello{
- @Override
- public void sayHello() {
- System.out.println("HelloImpl say hello");
- }
- }
我们的MyModule实现类(具体的绑定关系被注释了)
- package guice.binder;
- import com.google.inject.Binder;
- import com.google.inject.Module;
- public class MyModule implements Module{
- @Override
- public void configure(Binder binder) {
- //将借口hello.java 绑定到实现类HelloImpl.java上
- //Singleton:表示是scope是单列的,Guice中类的scope只有两类,单例,或者每次都是new的一个新的对象
- //<span style="white-space:pre"> </span>binder.bind(Hello.class).to(HelloImpl.class).in(Singleton.class) ;
- }
- }
- Injector in = Guice.createInjector(new MyModule()) ;
- Hello hello = in.getInstance(Hello.class) ;//hello -->HelloImpl实例
输出的结果依然还是HelloImpl say hello,
Google Guice 注入(@Inject注解)
上一节介绍通过注解来实现绑定关系,这一节介绍如何通过@Inject来完成依赖注入的功能:
(1)属性注入
(2)构造方法注入
(3)setter方法注入
一.属性注入
(1)创建一个接口Hello.java和一个实现类HelloImpl
创建一个HelloCaller(包含一个Hello属性)
- package guice.di;
- import com.google.inject.Inject;
- //HelloCaller将会去调用Hello这个接口提供的服务
- public class HelloCaller {
- //通过@Inject,来完成属性的注入
- @Inject
- private Hello hello ;
- //调用Hello的sayHello方法(实际上就是去调用HelloImpl的sayHello,因为我们将Hello的实现指定是HelloImpl)
- public void sayHello(){
- hello.sayHello() ;
- }
- }
通过@Inject注解将Hello属性的实例注入进来
- Injector in = Guice.createInjector(new Module(){
- @Override
- public void configure(Binder arg0) {
- //什么也不写
- }
- }) ;
- //得到HelloCaller的实例
- HelloCaller helloCaller = in.getInstance(HelloCaller.class) ;
- //调用sayHello方法
- helloCaller.sayHello() ;
构造方法注入
(1)Hello和HelloImpl类还是上面那样不用变,唯一变化的是HelloCaller类
- package guice.di;
- import com.google.inject.Inject;
- public class HelloCaller1 {
- //包含一个Hello属性
- private Hello hello ;
- //@Inject注解写在构造方法上,通过构造方法的方式注入属性hello
- @Inject
- public HelloCaller1(Hello hello){
- this.hello = hello ;
- }
- public void sayHello(){
- hello.sayHello() ;
- }
- }
setter方式注入
(1)Hello和HelloImpl类还是上面那样不用变,唯一变化的是HelloCaller类
- //通过setter方法来注入hello属性
- @Inject
- public void setHello(Hello hello) {
- this.hello = hello;
Google Guice 之绑定1的更多相关文章
- Google Guice结合模式
于Guice于,喷油器装配工作是一个对象图,当请求类型实例,喷油器根据推断对象如何映射到创建的实例.解决依赖.要确定如何解决的依赖就需要配置喷油器结合的方式. 要创建绑定(Binding)对象,能够继 ...
- Google Guice学习
学习动力:公司项目使用 官方文档:https://github.com/google/guice/wiki/Motivation 学习阶段:入门 主要部份: 简介 Bindings方式 Scopes设 ...
- google guice
1 google guice是什么 google guice是一个轻量的DI容器. 2 guice和spring对比 spring的配置放在xm文件中,guice的配置放在Module中. guice ...
- 【曹工杂谈】Maven IOC容器的下半场:Google Guice
Maven容器的下半场:Guice 前言 在前面的文章里,Maven底层容器Plexus Container的前世今生,一代芳华终落幕,我们提到,在Plexus Container退任后,取而代之的底 ...
- jdbc框架 commons-dbutils+google guice+servlet 实现一个例子
最近闲着无聊,于是看了一下jdbc框架 commons-dbutils与注入google guice. 我就简单的封装了一下代码,效率还是可以的.... jdbc+google guice+servl ...
- Guice 依赖绑定
Guice 依赖绑定 连接绑定(Linked Bingdings) 连接绑定是 Guice 最基本的一种绑定方式.这种绑定方式我们需要在自己定义的 Module 的 configure() 中编写绑定 ...
- 依赖注入框架Google Guice 对象图
GettingStarted · google/guice Wiki https://github.com/google/guice/wiki/GettingStarted sameb edited ...
- 史上最好用的依赖注入框架Google Guice【转】
Guice是Google开发的一个轻量级,基于Java5(主要运用泛型与注释特性)的依赖注入框架(IOC).Guice非常小而且快. (其他的依赖注入框架还有Dagger,Spring) Spring ...
- google guice @inject comments
refer this document: http://blog.chinaunix.net/uid-20749563-id-718418.html @Inject注入方式,用@Inject来标识那个 ...
随机推荐
- ArrayList动态扩容机制
初始化:有三种方式 1.默认的构造器,将会以默认的大小来初始化内部的数组:public ArrayList(); 2.用一个ICollection对象来构造,并将该集合的元素添加到ArrayList: ...
- Visual Studio 2010如何利用宏
最近在做后台代码的拆分,由于机器升级,原来装的添加注释的插件不能用了. 看来只有自己想办法了,看了下利用宏添加注释与把项目展开.折叠的方式: 参考了以下几个内容: 1.Visual Studio 20 ...
- 【神仙DP】【单调队列】【模拟题】区间覆盖
传送门 Description 给出数轴上的n个线段,保留最多k条线段,问这些被保留下来的线段的并集长度为最多为多少. Input 第一行两个数n和k 接下来n行,每行两个数,表示一条线段的左右端点. ...
- Android IntentService分析
IntentService其实是一个很通用的知识点,最近看了下阿里巴巴Android开发手册,再次记录下 阿里巴巴Android开发手册 [强制]避免在 BroadcastReceiver#onRec ...
- CentOS 下安装 LEMP 服务(nginx、MariaDB/MySQL 和 php)
转载自:https://linux.cn/article-4314-1.html 编译自:http://xmodulo.com/install-lemp-stack-centos.html 作者: D ...
- Difference between Netbios and Host name
Hostnames or NetBIOS names were used to provide a friendlier means of identifying servers or worksta ...
- python---aiohttp的使用
1.aiohttp的简单使用(配合asyncio模块) import asyncio,aiohttp async def fetch_async(url): print(url) async with ...
- ZooKeeper入门(四)
入门:使用ZooKeeper的协调分布式应用 这个文档使你对ZooKeeper快速入门,它主要针对想尝试它的开发者.并且包含简单的单机的ZooKeeper服务的安装说明,一些验证是否运行的命令,和一个 ...
- truncate与delete以及drop
truncate:删除整个表,但不删除定义(删除了整个表的数据,但表结构还在) drop:删除整个表,表数据和表结构都删除了 delete:删除表中数据 比较delete和drop 1.truncat ...
- 解决SpringSecurity限制iframe引用页面的问题
使用Spring Security的过程中,需要使用iframe来引入其他域的页面,页面会报X-Frame-Options的错误,试了好几种方法一直未能很好的解决这个问题. 这里涉及到Spring S ...