- 谷歌指南
- Guice - 主页
- Guice - 概述
- Guice - 环境设置
- Guice - 首次申请
- 绑定示例
- Guice - 链接绑定
- Guice - 绑定注释
- Guice - @Named 绑定
- Guice - 常量绑定
- Guice - @Provides Annotation
- Guice - 提供者类
- Guice - 构造函数绑定
- Guice - 内置绑定
- Guice - 即时绑定
- 注射实例
- Guice - 构造函数注入
- Guice - 方法注入
- Guice - 现场注射
- Guice - 可选注射
- Guice - 按需注射
- 其他例子
- Guice - 范围
- Guice-AOP
- 有用的资源
- Guice - 快速指南
- Guice - 有用的资源
- Guice - 讨论
Google Guice - 范围
Guice 每次提供一个值作为其默认Behave时都会返回一个新实例。它可以通过范围进行配置。以下是 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://localhost: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