- 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
廣告