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 ...
随机推荐
- 为什么大部分的 PHP 程序员转不了 Go 语言?
大家好,我是码农先森. 树挪死,人挪活,这个需求我做不了,换个人吧.大家都有过这种经历吧,放在编程语言身上就是 PHP 不行了,赶紧转 Go 语言吧.那转 Go 语言就真的行了?那可不见得,我个人认为 ...
- Java静态相关问题
问题1: public class Demo01_StaticTest { private static Demo01_StaticTest st = new Demo01_StaticTest(); ...
- Unity编辑器批量设置图片格式
在游戏开发中,经常需要批量设置图片的格式为Sprite类型,手动设置太麻烦,下面的编辑器脚本实现选中文件夹右键/Texture/SetAllImagesToSpriteType实现批量设置图片格式,具 ...
- 6. 从0开始学ARM-异常及中断处理、异常向量表、swi
一.异常(Exception) 异常是理解CPU运转最重要的一个知识点,几乎每种处理器都支持特定异常处理,中断是异常中的一种. 有时候我们衡量一个操作系统的时候实时性就是看os最短响应中断时间以及单位 ...
- 前端使用 Konva 实现可视化设计器(21)- 绘制图形(椭圆)
本章开始补充一些基础的图形绘制,比如绘制:直线.曲线.圆/椭形.矩形.这一章主要分享一下本示例是如何开始绘制一个图形的,并以绘制圆/椭形为实现目标. 请大家动动小手,给我一个免费的 Star 吧~ 大 ...
- 记录_玩客云v1.0大坑!!!
刷机 短接后刷入uboot固件, 制作U盘镜像启动会出现莫名其妙的内存写入失败!!!!!!!!!!! 但是这并没坏 拆机 , 短接刷armbian v5.67 内核 3.10, 这个版本刷完后什么特 ...
- .NET 8 Moq mock GetRequiredKeyedService Setup报错
.NET 8 Moq mock GetRequiredKeyedService Setup报错 代码有地方用到了IServiceProvider.GetRequiredKeyedService来解析服 ...
- CEIT算法训练-双指针部分题解(全12题)
代码宏定义以及框架约定 #include <bits/stdc++.h> using namespace std; #define IOS ios_base::sync_with_stdi ...
- ansible rpm包下载
Ansible2.9.18版本下载链接:https://pan.baidu.com/s/1dKlwtLWSOKoMkanW900n9Q 提取码:ansi 将软件上传至系统并解压安装: # tar -z ...
- wxpython开发gui界面基础
wxpython 开发gui 基础知识 一 .前言 记录使用wxpython开发gui工具吧.gui界面主要就是先布局,每个模块都是一个对象. 二.基础知识 import wx class MyFra ...