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 ...
随机推荐
- 使用BizyAir,没有显卡,也能玩AI绘图
或许很多人跟我一样,没有显卡,但又很想玩AI绘图,但本地绘图怕是无缘了,只能借助云GPU的方式了. 今天跟大家分享一下一个简单目前可白嫖无门槛的方法实现无显卡也能玩AI绘图. 方案就是ComfyUI+ ...
- ApplicationRunner的讲解
在开发中可能会有这样的情景.需要在容器启动的时候执行一些内容.比如读取配置文件,数据库连接之类的.SpringBoot给我们提供了两个接口来帮助我们实现这种需求.这两个接口分别为CommandLine ...
- CH07_指针
CH07_指针 指针的基本概念 作用:可以通过指针间接访问内存 描述: 内存编号是从0开始记录的,一般用十六进制数字表示 可以利用指针变量保存地址 指针变量的定义和使用 语法: 数据类型 * 变量名; ...
- zuul集成apollo动态刷新配置
zuul集成apollo实现路由配置的动态刷新 import com.ctrip.framework.apollo.model.ConfigChangeEvent; import com.ctrip. ...
- Linux内核如何判断地址是否位于用户空间?
一. 问题描述 access_ok函数是什么原理? 二.问题分析 我们在内核空间和用户空间进行数据拷贝的时候必须判断用户空间地址是否合法. 主要通过偶函数access_ok来判断. 1. Linux用 ...
- bfs优化
层次单调性 走地图 双重bfs 1.模块性 2.方案:外层bfs逆推,内层bfs重新跑 A.每次代价0/1:双端队列bfs B.每次代价任意数值:优先队列bfs(dijikstra).迭代(SPFA) ...
- 线性dp:LeetCode674. 最长连续递增序列
LeetCode674. 最长连续递增序列 阅读本文之前,需要先了解"动态规划方法论",这在我的文章以前有讲过 链接:动态规划方法论 本文之前也讲过一篇文章:最长递增子序列,这道题 ...
- 【Java】之获取CSV文件数据以及获取Excel文件数据
一.获取CSV文件数据 import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Sheet; impor ...
- C++ | 每一个C++程序员都应该知道的RAII
导读:RAII是C++中一种管理资源.避免资源泄漏的惯用法,利用栈的特点来实现.本文较为详细介绍了RAII的原理.使用方法和优点,并且通过实例讲解了RAII在C++ STL中的应用,如智能指针和互斥锁 ...
- EF Core报错“Format of the initialization string does not conform to specification starting at index 0.”
问题分析: 今天在EF Core数据库迁移的过程中无意中发现此错误,我的项目仅仅复制黏贴了配置文件而已,自此发现是数据库配置文件json在作祟. 对比了下发现是.json文件没有被设置"复制 ...