- Google Guice Tutorial
- 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
Google Guice - Inbuilt Bindings
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. See the example below.
Example
Create a java class named GuiceTester.
GuiceTester.java
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.
Dec 20, 2017 12:51:05 PM TextEditor makeSpellCheck INFO: In TextEditor.makeSpellCheck() method
Advertisements