Google Guice Tutorial
Selected Reading
- Guice - Discussion
- Guice - Useful Resources
- Guice - Quick Guide
- Guice - AOP
- Guice - Scopes
- Guice - On-demand Injection
- Guice - Optional Injection
- Guice - Field Injection
- Guice - Method Injection
- Guice - Constructor Injection
- Guice - Just-in-time Bindings
- Guice - Inbuilt Bindings
- Guice - Constructor Bindings
- Guice - Provider Class
- Guice - @Provides Annotation
- Guice - Constant Bindings
- Guice - @Named binding
- Guice - Binding Annotations
- Guice - Linked binding
- Guice - First Application
- Guice - Environment Setup
- Guice - Overview
- Guice - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Guice - Inbuilt Bindings
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; pubpc class GuiceTester { pubpc 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 pubpc TextEditor( Logger logger) { this.logger = logger; } pubpc 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() methodAdvertisements