- Guice - Home
- Guice - Overview
- Guice - Environment Setup
- Guice - First Application
- Bindings Examples
- Guice - Linked binding
- Guice - Binding Annotations
- Guice - @Named binding
- Guice - Constant Bindings
- Guice - @Provides Annotation
- Guice - Provider Class
- Guice - Constructor Bindings
- Guice - Inbuilt Bindings
- Guice - Just-in-time Bindings
- Injection Examples
- Guice - Constructor Injection
- Guice - Method Injection
- Guice - Field Injection
- Guice - Optional Injection
- Guice - On-demand Injection
- Miscellaneous Examples
- Guice - Scopes
- Guice - AOP
- Guice Useful Resources
- Guice - Quick Guide
- Guice - Useful Resources
- Guice - Discussion
Guice - Inbuilt Bindings
Introduction
Guice provides inbuilt binding for java.util.logging.Logger class. Logger's name is automatically set to the name of the class into which the Logger is injected.
class TextEditor {
private Logger logger;
@Inject
public TextEditor( Logger logger) {
this.logger = logger;
}
public void makeSpellCheck(){
logger.info("In TextEditor.makeSpellCheck() method");
}
}
See the complete example below.
Example - Usage of Inbuilt Binding
GuiceTester.java
package com.tutorialspoint;
import java.util.logging.Logger;
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());
TextEditor editor = injector.getInstance(TextEditor.class);
editor.makeSpellCheck();
}
}
class TextEditor {
private Logger logger;
@Inject
public TextEditor( Logger logger) {
this.logger = logger;
}
public void makeSpellCheck(){
logger.info("In TextEditor.makeSpellCheck() method");
}
}
//Binding Module
class TextEditorModule extends AbstractModule {
@Override
protected void configure() {
}
}
Output
Compile and run the file, you will see the following output −
Aug 20, 2025 11:39:57 AM com.tutorialspoint.TextEditor makeSpellCheck INFO: In TextEditor.makeSpellCheck() method
Advertisements