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 ...
随机推荐
- 生态兼容性进一步提升!白鲸开源 WhaleStudio 与火山引擎ByteHouse完成产品互认
数据作为新型生产要素,已快速融入生产.分配.流通.消费和社会服务管理等各环节,深刻改变着生产方式.生活方式和治理方式.越来越多企业也在尝试充分利用数据要素,开辟全新发展路径,进一步实现业务价值提升. ...
- Sentry 产品指南文档(附:详细脑图整理)
Sentry 基础知识 https://docs.sentry.io/product/ https://docs.sentry.io/product/sentry-basics/ 问题 https:/ ...
- typeof、offsetof、container_of
typeof 用于获取一个对象的类型,比如: unsigned int a = 1; // typeof (a) is unsigned int short b = 2; // typeof (b ...
- LLM应用实战: 产业治理多标签分类
1. 背景 许久未见,甚是想念~ 近期本qiang~换了工作,处于新业务适应期,因此文章有一段时间未更新,理解万岁! 现在正在着手的工作是产业治理方面,主要负责其中一个功能模块,即按照产业治理标准体系 ...
- 2023 PTA天梯赛补题(L1 & L2)
2023 天梯赛 L1 & L2 补题 L1 L1-089 最好的文档 输入输出题 #include<bits/stdc++.h> using namespace std; int ...
- 1分钟掌握变速效果,让你的视频快慢自如----蓝松视频编辑SDK
2. 变速调整默认速度是1X就是正常播放速度,可以通过调节滑块,实现视频中的慢镜头动作 3.只需一行代码设置播放速度/** 视频的播放速度; 范围是 0.1---10.0 默认1.0; 正 ...
- Gmail 别名
Gmail 有一个很少人知道但是非常实用的功能,那就是别名.Gmail 允许用户通过在基本邮箱地址中添加特定符号和文本来创建多个别名.这些别名都指向同一个 Gmail 账户,方便用户进行邮件管理.过滤 ...
- Go channel 介绍
Go 语言(Golang)中的 chan 是通道(channel)的缩写,用于在不同的 goroutine 之间进行通信.通道允许你在 goroutine 之间传递数据,从而实现同步和共享内存.下面是 ...
- sipp模拟uas发送update
概述 freeswitch是一款简单好用的VOIP开源软交换平台. 但是fs在处理update消息时候有BUG,为了复现问题,使用sipp模拟uas,发送update并发送DTMF码. 本文档记录si ...
- CentOS 7 yum无法使用解决方法Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=
在centos7中使用yum命令时候报错: Loading mirror speeds from cached hostfile Could not retrieve mirrorlist http: ...