- Google Guice 教程
- Guice - 主页
- Guice - 概述
- Guice - 环境设置
- Guice - 第一个应用程序
- 绑定示例
- Guice - 链接绑定
- Guice - 绑定注解
- Guice - @Named 绑定
- Guice - 常量绑定
- Guice - @Provides 注解
- Guice - 提供程序类
- Guice - 构造函数绑定
- Guice - 内置绑定
- Guice - 即时绑定
- 注入示例
- Guice - 构造函数注入
- Guice - 方法注入
- Guice - 域注入
- Guice - 可选注入
- Guice - 按需注入
- 杂项示例
- Guice - 作用域
- Guice - AOP
- Guice 有用资源
- Guice - 快速指南
- Guice - 有用资源
- Guice - 讨论
Google Guice - 作用域
Guice 在每次提供值时会返回一个新实例作为其默认行为。它可通过作用域进行配置。以下是 Guice 支持的作用域
@Singleton - 在应用程序生命周期内单一实例。@Singleton 对象需要是线程安全的。
@SessionScoped - 在 Web 应用程序的某个特定会话内单一实例。@SessionScoped 对象需要是线程安全的。
@RequestScoped - 在 Web 应用程序的某个特定请求内单一实例。@RequestScoped 对象不需要是线程安全的。
应用作用域的方法。
以下是应用作用域的方法。
在类级别
@Singleton
class SpellCheckerImpl implements SpellChecker {
public SpellCheckerImpl(){}
@Override
public void checkSpelling() {
System.out.println("Inside checkSpelling." );
}
}
在配置级别
bind(SpellChecker.class).to(SpellCheckerImpl.class).in(Singleton.class);
在方法级别
@Provides @Singleton
public SpellChecker provideSpellChecker(){
String dbUrl = "jdbc:mysql://:5326/emp";
String user = "user";
int timeout = 100;
SpellChecker SpellChecker = new SpellCheckerImpl(dbUrl, user, timeout);
return SpellChecker;
}
示例
我们来看看类级别作用域的实际应用。
使用 @Singleton 注解的结果
创建一个名为 GuiceTester 的 Java 类。
GuiceTester.java
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Singleton;
public class GuiceTester {
public static void main(String[] args) {
Injector injector = Guice.createInjector(new TextEditorModule());
SpellChecker spellChecker = new SpellCheckerImpl();
injector.injectMembers(spellChecker);
TextEditor editor = injector.getInstance(TextEditor.class);
System.out.println(editor.getSpellCheckerId());
TextEditor editor1 = injector.getInstance(TextEditor.class);
System.out.println(editor1.getSpellCheckerId());
}
}
class TextEditor {
private SpellChecker spellChecker;
@Inject
public void setSpellChecker(SpellChecker spellChecker){
this.spellChecker = spellChecker;
}
public TextEditor() { }
public void makeSpellCheck(){
spellChecker.checkSpelling();
}
public double getSpellCheckerId(){
return spellChecker.getId();
}
}
//Binding Module
class TextEditorModule extends AbstractModule {
@Override
protected void configure() {
bind(SpellChecker.class).to(SpellCheckerImpl.class);
}
}
interface SpellChecker {
public double getId();
public void checkSpelling();
}
@Singleton
class SpellCheckerImpl implements SpellChecker {
double id;
public SpellCheckerImpl(){
id = Math.random();
}
@Override
public void checkSpelling() {
System.out.println("Inside checkSpelling." );
}
@Override
public double getId() {
return id;
}
}
编译并运行文件,你可能会看到相同数字的以下输出。
0.3055839187063575 0.3055839187063575
不使用 @Singleton 注解的结果
创建一个名为 GuiceTester 的 Java 类。
GuiceTester.java
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
public class GuiceTester {
public static void main(String[] args) {
Injector injector = Guice.createInjector(new TextEditorModule());
SpellChecker spellChecker = new SpellCheckerImpl();
injector.injectMembers(spellChecker);
TextEditor editor = injector.getInstance(TextEditor.class);
System.out.println(editor.getSpellCheckerId());
TextEditor editor1 = injector.getInstance(TextEditor.class);
System.out.println(editor1.getSpellCheckerId());
}
}
class TextEditor {
private SpellChecker spellChecker;
@Inject
public void setSpellChecker(SpellChecker spellChecker){
this.spellChecker = spellChecker;
}
public TextEditor() { }
public void makeSpellCheck(){
spellChecker.checkSpelling();
}
public double getSpellCheckerId(){
return spellChecker.getId();
}
}
//Binding Module
class TextEditorModule extends AbstractModule {
@Override
protected void configure() {
bind(SpellChecker.class).to(SpellCheckerImpl.class);
}
}
interface SpellChecker {
public double getId();
public void checkSpelling();
}
class SpellCheckerImpl implements SpellChecker {
double id;
public SpellCheckerImpl(){
id = Math.random();
}
@Override
public void checkSpelling() {
System.out.println("Inside checkSpelling." );
}
@Override
public double getId() {
return id;
}
}
输出
编译并运行文件,你可能会看到不同数字的以下输出。
0.556007079571739 0.22095011760351602
广告