Google Guice 与 Noear Solon 的简单对照
1. 简介
Google Guice 是一个轻量级的依赖注入框架,它支持Java 5或者更高版本的JDK。Noear Solon 也是一个轻量级的依赖注入框架,它支持Java 8或者更高版本的JDK
本文会通过一些例子来初步的认识一下 Guice 和 Solon 的区别,及互通性。
2. 添加依赖
guice
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
</dependency>
solon
<dependency>
<groupId>org.noear</groupId>
<artifactId>solon</artifactId>
</dependency>
3. 基础用法对照
- (a) 注入
Guice 的项目代码样例(略过 logger 和 communicator 的来源)
public class Communication {
@Inject
private Logger logger;
@Inject
private Communicator communicator;
public boolean sendMessage(String message) {
return communicator.sendMessage(message);
}
public static void main(String[] args){
Injector injector = Guice.createInjector();
Communication comms = injector.getInstance(Communication.class)
comms.sendMessage("软件质量保障");
}
}
Solon 的项目代码样例(略过 logger 和 communicator 的来源)
@Component
public class Communication {
@Inject
private Logger logger;
@Inject
private Communicator communicator;
public boolean sendMessage(String message) {
return communicator.sendMessage(message);
}
public static void main(String[] args){
Solon.start(Communication.class, args);
Communication comms = Solon.context().getBean(Communication.class);
comms.sendMessage("软件质量保障");
}
}
- (b) 反向控制绑定
Guice。此模块实现将Communicator绑定到其默认实现类DefaultCommunicatorImpl上,在找到Communicator的地方都将注入Default CommunicatorImpl的实例。
public class BasicModule extends AbstractModule {
@Override
protected void configure() {
bind(Communicator.class).to(DefaultCommunicatorImpl.class);
}
}
Solon。则通过在 DefaultCommunicatorImpl 类上,添加组件注解。
@Component
public class DefaultCommunicatorImpl extneds Communicator{
}
- (c) by name 注入
Guice
@Inject @Named("DefaultCommunicator")
Communicator communicator;
@Override
protected void configure() {
bind(Communicator.class)
.annotatedWith(Names.named("DefaultCommunicator"))
.to(DefaultCommunicatorImpl.class);
}
Solon
@Inject("DefaultCommunicator")
Communicator communicator;
@Component("DefaultCommunicator")
public class DefaultCommunicatorImpl extneds Communicator{
}
Google Guice 与 Noear Solon 的简单对照的更多相关文章
- 史上最好用的依赖注入框架Google Guice【转】
Guice是Google开发的一个轻量级,基于Java5(主要运用泛型与注释特性)的依赖注入框架(IOC).Guice非常小而且快. (其他的依赖注入框架还有Dagger,Spring) Spring ...
- jdbc框架 commons-dbutils+google guice+servlet 实现一个例子
最近闲着无聊,于是看了一下jdbc框架 commons-dbutils与注入google guice. 我就简单的封装了一下代码,效率还是可以的.... jdbc+google guice+servl ...
- Google Guice学习
学习动力:公司项目使用 官方文档:https://github.com/google/guice/wiki/Motivation 学习阶段:入门 主要部份: 简介 Bindings方式 Scopes设 ...
- 依赖注入框架Google Guice 对象图
GettingStarted · google/guice Wiki https://github.com/google/guice/wiki/GettingStarted sameb edited ...
- Google Guice 之绑定1
绑定和依赖注入区别 绑定,使用时 需要通过 injector 显示获取 依赖注入,只需要显示获取主类,他的依赖是通过@Injector 和 绑定关系 隐式注入的 http://blog.csdn.ne ...
- Solon 最简单demo---Hello World
Solon 的项目地址: https://gitee.com/noear/solon 里面杂七杂八的东西很多...今天的目标是整一个最最简单,最最小巧的 Hello world (一)用 Intell ...
- 【曹工杂谈】Maven IOC容器的下半场:Google Guice
Maven容器的下半场:Guice 前言 在前面的文章里,Maven底层容器Plexus Container的前世今生,一代芳华终落幕,我们提到,在Plexus Container退任后,取而代之的底 ...
- Google Guice 用户指南 - Ⅰ:概览
译者:kefate 原文:https://github.com/google/guice/wiki/Overview 大家好,我是kefate.今天开始我将会把Google Guice的官方文档陆续翻 ...
- 【Cardboard】 体验 - Google Cardboard DIY及完成后简单体验
体验 - Google Cardboard DIY及完成后简单体验 今年的Google I/O最让我感兴趣的除了Material Design以外就是这个Google Cardboard了.据说是Go ...
- google guice
1 google guice是什么 google guice是一个轻量的DI容器. 2 guice和spring对比 spring的配置放在xm文件中,guice的配置放在Module中. guice ...
随机推荐
- flex布局被内容被撑开及flex布局下定宽元素被压缩
实现效果使用flex进行左右布局,左边定宽200px,右边自适应,当右边内容过多,造成右边盒子被撑开,会造成两种问题 左边定宽盒子被压缩解决办法: flex-grow:0;//是否自动增长空间 fle ...
- Codeforces Round 964 (Div. 4) D. Slavic's Exam
题目链接:https://codeforces.com/contest/1999/problem/D 题目描述 Slavic 的考试非常难,需要您的帮助才能通过.以下是他正在努力解决的问题: 存在一个 ...
- bmp位图文件信息结构体
/************************************************* * * file name:BmpInfoStruct.c * author :momolyl@1 ...
- 旧物利用 - 将机顶盒改造为一台Linux开发机!
前言 机顶盒型号:移动魔百盒CM201-2(CH),芯片组: hi3798mv300(hi3798mv3dmm),其他型号类似 理论上适用于以下SOC:Hi3798Mv100 / Hi3798Cv20 ...
- AtCoder Beginner Contest 327 D
AtCoder Beginner Contest 327D D - Good Tuple Problem (atcoder.jp)(种类并查集,二分图染色) 算法学习笔记(7):种类并查集 附上典题: ...
- 让你的C程序,可以自动更新版本信息
一.软件管理 稍微上点规模的软件开发往往周期都非常长, 中间会产生很多临时版本, 这些临时版本往往会有各种各样的bug, 由于项目参与的人员众多.水平参差不齐, 软件分支众多.功能复杂, 经常会有各种 ...
- 这才是java对象正解
这才是 Java 对象正解 在深入讨论对象之前,让我们先明确对对象和实例的理解. 什么是对象? 对象(Object)是内存中分配的实际数据结构,它包含了数据和方法.在 Java 中,对象是类的一个实例 ...
- 速通c++
文章目录 1.什么是c++. 2什么是面向对象,什么又是面向过程. 3.C++的灵魂,c++的类. 4.如何定义一个类. 5.什么是对象. 6.如何定义一个对象. 直接定义: 在堆里面定义. 删除对象 ...
- That's not my Neighbor 之 Chester 问题答案
Q: What is the meaning of life, the universe and everything else? A: 42 参见:生命.宇宙以及任何事情的终极答案 Q: What ...
- git 相关操作
git diff 已经缓存的文件和刚刚修改过的没有缓存的文件的对比 git diff --stage git status 查看本地文件的修改,是否进入缓存 git add 把刚刚修改过的 ...