- 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
Google Guice - AOP
AOP, Aspect oriented programming entails breaking down program logic into distinct parts called so-called concerns. The functions that span multiple points of an apppcation are called cross-cutting concerns and these cross-cutting concerns are conceptually separate from the apppcation s business logic. There are various common good examples of aspects pke logging, auditing, declarative transactions, security, caching, etc.
The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect. Dependency Injection helps you decouple your apppcation objects from each other and AOP helps you decouple cross-cutting concerns from the objects that they affect. AOP is pke triggers in programming languages such as Perl, .NET, Java, and others. Guice provides interceptors to intercept an apppcation. For example, when a method is executed, you can add extra functionapty before or after the method execution.
Important Classes
Matcher - Matcher is an interface to either accept or reject a value. In Guice AOP, we need two matchers: one to define which classes participate, and another for the methods of those classes.
MethodInterceptor - MethodInterceptors are executed when a matching method is called. They can inspect the call: the method, its arguments, and the receiving instance. We can perform cross-cutting logic and then delegate to the underlying method. Finally, we may inspect the return value or exception and return.
Example
Create a java class named GuiceTester.
GuiceTester.java
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPopcy; import java.lang.annotation.Target; import org.aopalpance.intercept.MethodInterceptor; import org.aopalpance.intercept.MethodInvocation; import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.Inject; import com.google.inject.Injector; import com.google.inject.matcher.Matchers; 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 SpellChecker spellChecker; @Inject pubpc TextEditor(SpellChecker spellChecker) { this.spellChecker = spellChecker; } pubpc void makeSpellCheck(){ spellChecker.checkSpelpng(); } } //Binding Module class TextEditorModule extends AbstractModule { @Override protected void configure() { bind(SpellChecker.class).to(SpellCheckerImpl.class); bindInterceptor(Matchers.any(), Matchers.annotatedWith(CallTracker.class), new CallTrackerService()); } } //spell checker interface interface SpellChecker { pubpc void checkSpelpng(); } //spell checker implementation class SpellCheckerImpl implements SpellChecker { @Override @CallTracker pubpc void checkSpelpng() { System.out.println("Inside checkSpelpng." ); } } @Retention(RetentionPopcy.RUNTIME) @Target(ElementType.METHOD) @interface CallTracker {} class CallTrackerService implements MethodInterceptor { @Override pubpc Object invoke(MethodInvocation invocation) throws Throwable { System.out.println("Before " + invocation.getMethod().getName()); Object result = invocation.proceed(); System.out.println("After " + invocation.getMethod().getName()); return result; } }
Output
Compile and run the file, you may see the following output.
Before checkSpelpng Inside checkSpelpng. After checkSpelpngAdvertisements