What is AOP?
- Aspect-Oriented Programming (AOP) complements Object-Oriented Programming (OOP) by providing another way of thinking about program structure.
- In addition to classes, AOP gives you aspects.
- Aspects enable modularization of concerns such as transaction management that cut across multiple types and objects. (Such concerns are often termed crosscutting concerns.)
- One of the key components of Spring is the AOP framework.
- While the Spring IoC container does not depend on AOP, meaning you do not need to use AOP if you do not want to, AOP complements Spring IoC to provide a very capable middleware solution.
Aspect Oriented Programming provides for simplified application of cross-cutting concerns.
Example of cross-cutting concerns
-
Logging
-
Transaction Management
-
Security
-
Auditing
-
Locking
-
Event Handling
AOP Concepts
- JOINPOINT:
- Is Well defined point during the execution of an application.
- we can insert additional logic at join point’s.
- Example of Join points:
- Method invocation
- Class Initialization
- Object initialization
- ADVICE:
- The code that is executed at a particular Joinpoint.
- Types of Advice
- Before Advice => which executes before Joinpoint
- After Advice => which executes after Joinpoin.
- Around Advice => which executes around Joinpoint
- POINTCUTS:
- A Collection of Joinpoint that is used to define when advice should be executed.
- By creating pointcuts, we can gain fine-grained control over how we can apply to the components.
- Example
- A typical JoinPoint is a method invocation
- A typical pointcut is a collection of all method invocation at a particular class.
- Pointcuts can be composed In complex relationships to further constrain when advice is executed.
- ASPECTS:
- An Aspect is a combination of pointcut and advice.
Hello.Java
package com.javaskool;
public interface Hello {
public void sayHello();
}
HelloImpl.java
package com.javaskool;
public class HelloImpl implements Hello {
public void sayHello()
{
System.out.println("James Bond");
}
}
MyBeforeAspect1.java
package com.javaskool.aspect;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class MyBeforeAspect1 implements MethodBeforeAdvice{
@Override
public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
System.out.print("Hello!! Mr.");
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="obj" class="com.javaskool.HelloImpl"/>
<bean id="advice1" class="com.javaskool.aspect.MyBeforeAspect1"/>
<bean id="aspect1" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="advice1"/>
<property name="pattern" value=".*Hello"/>
</bean>
<bean id="advicedObj" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.javaskool.Hello</value>
</property>
<property name="target" ref="obj"/>
<property name="interceptorNames">
<list>
<value>aspect1</value>
</list>
</property>
</bean>
</beans>
<!--
In AOP always we need to create a spring bean in the form of interface and implementation class
only, because the IOC container internally creates proxy class by implementing that interface
with the help of ProxyFactoryBean.
In spring configuration file, we need to configure a predefined bean class called
ProxyFactoryBean, this class is used to configure our bean class and
its required advices as a group.
-->
<!--
public class RegexpMethodPointcutAdvisor extends AbstractGenericPointcutAdvisor
It is Convenient class for regexp method pointcuts that hold an Advice,
making them an Advisor.
Help You to Configure this class using the "pattern" and "patterns" pass-through properties.
These are analogous to the pattern and patterns properties of
AbstractRegexpMethodPointcut.
-->
TestDrive.java
package com.javaskool;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class TestDrive {
public static void main(String[] args) {
BeanFactory beans=new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
Hello p=(Hello)beans.getBean("advicedObj");
p.sayHello();
}
}
Output
Hello!! Mr.James Bond
Hello.Java
package com.javaskool;
public interface Hello {
public String sayHello();
}
HelloImpl.java
package com.javaskool;
public class HelloImpl implements Hello {
public String sayHello()
{
System.out.print("<font color="#FF3300">James Bond</font> ");
return "Hi this is sayHello()";
}
}
MyAfterAspect1.java
package com.javaskool.aspect;
import java.lang.reflect.Method;
import java.util.Arrays;
import org.springframework.aop.AfterReturningAdvice;
public class MyAfterAspect1 implements <font color="#FF0000">AfterReturningAdvice</font>{
public void afterReturning(Object arg0,Method arg1, Object[] arg2,Object arg3) throws Throwable
{
System.out.println("<font color="#0000FF">Let's Go</font>");
System.out.println("Other Info from After \n"+"Returning Mesg : "+arg0);
System.out.println("Method Name :"+arg1.getName());
System.out.println("Method Arg "+Arrays.toString(arg2));
System.out.println("Class Name "+arg3.getClass());
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="<em>obj</em>" class="com.javaskool.HelloImpl"/>
<bean id="<font color="#800000">advice1</font>" class="com.javaskool.aspect.MyAfterAspect1"/>
<bean id="<em>aspect1</em>" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="<font color="#800000">advice1</font>"/>
<property name="pattern" value=".*Hello"/>
</bean>
<bean id="advicedObj" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.javaskool.Hello</value>
</property>
<property name="target" ref="<em>obj</em>"/>
<property name="interceptorNames">
<list>
<value><em>aspect1</em></value>
</list>
</property>
</bean>
</beans>
<!--
In AOP always we need to create a spring bean in the form of interface and implementation class
only, because the IOC container internally creates proxy class by implementing that interface
with the help of ProxyFactoryBean.
In spring configuration file, we need to configure a predefined bean class called
ProxyFactoryBean, this class is used to configure our bean class and
its required advices as a group.
-->
<!--
public class RegexpMethodPointcutAdvisor extends AbstractGenericPointcutAdvisor
It is Convenient class for regexp method pointcuts that hold an Advice,
making them an Advisor.
Help You to Configure this class using the "pattern" and "patterns" pass-through properties.
These are analogous to the pattern and patterns properties of
AbstractRegexpMethodPointcut.
-->
TestDrive.java
package com.javaskool;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class TestDrive {
public static void main(String[] args) {
BeanFactory beans=new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
Hello p=(Hello)beans.getBean("advicedObj");
String s=p.sayHello();
System.out.println("Value of s : "+s);
}
}
Output
<font color="#FF3300">James Bond</font>
<font color="#0000FF">Let's Go</font>
Other Info from After
Returning Mesg : Hi this is sayHello()
Method Name :sayHello
Method Arg []
Class Name class com.javaskool.HelloImpl
<font color="#FFFF00">Value of s : Hi this is sayHello() </font>
Hello.Java
package com.javaskool;
public interface Hello {
public String sayHello();
}
HelloImpl.java
package com.javaskool;
public class HelloImpl implements Hello {
public String sayHello()
{
System.out.println("James Bond");
return "Hi this is sayHello()";
}
}
MyAroundAspect1.java
package com.javaskool.aspect;
import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.IntroductionInterceptor;
import org.springframework.aop.MethodBeforeAdvice;
public class MyAroundAspect1 implements IntroductionInterceptor{
public Object invoke(MethodInvocation arg0) throws Throwable
{
System.out.println("Starting");
Object o1=arg0.proceed();
System.out.println("o1 :- "+o1);
System.out.println("End");
return o1;
}
public boolean implementsInterface(Class arg0) {
// TODO Auto-generated method stub
return false;
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="obj" class="com.javaskool.HelloImpl"/>
<bean id="advice1" class="com.javaskool.aspect.MyAroundAspect1"/>
<bean id="aspect1" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="advice1"/>
<property name="pattern" value=".*Hello"/>
</bean>
<bean id="advicedObj" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.javaskool.Hello</value>
</property>
<property name="target" ref="obj"/>
<property name="interceptorNames">
<list>
<value>aspect1</value>
</list>
</property>
</bean>
</beans>
TestDrive.java
package com.javaskool;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class TestDrive {
public static void main(String[] args) {
BeanFactory beans=new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
Hello p=(Hello)beans.getBean("advicedObj");
String s=p.sayHello();
System.out.println("Value of s : "+s);
}
}
Output
Starting
James Bond
o1 :- Hi this is sayHello()
End
Value of s : Hi this is sayHello()
Hello.Java
package com.javaskool;
public interface Hello {
public String sayHello(String name);
}
HelloImpl.java
package com.javaskool;
public class HelloImpl implements Hello {
public String sayHello(String name)
{
System.out.println("i am in SayHello");
String s2=null;
System.out.println(s2.length());//NPE
return "Hello!! I am "+name;
}
}
MyThrowsAspect1.java
package com.javaskool.aspect;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.IntroductionInterceptor;
import org.springframework.aop.ThrowsAdvice;
public class MyThrowsAspect1 implements ThrowsAdvice{
public void afterThrowing(NullPointerException e)
{
System.out.println("NPE Occured");
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="obj" class="com.javaskool.HelloImpl"/>
<bean id="advice1" class="com.javaskool.aspect.MyThrowsAspect1"/>
<bean id="aspect1" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="advice1"/>
<property name="pattern" value=".*Hello"/>
</bean>
<bean id="advicedObj" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.javaskool.Hello</value>
</property>
<property name="target" ref="obj"/>
<property name="interceptorNames">
<list>
<value>aspect1</value>
</list>
</property>
</bean>
</beans>
TestDrive.java
package com.javaskool;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class TestDrive {
public static void main(String[] args) {
BeanFactory beans=new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
Hello p=(Hello)beans.getBean("advicedObj");
String s1=p.sayHello("Tom");
System.out.println("Value of s1 : "+s1);
}
}
Output
Exception in thread "main" i am in SayHello
NPE Occured
java.lang.NullPointerException
at com.javaskool.HelloImpl.sayHello(HelloImpl.java:9)
Hello.Java
package com.javaskool;
public interface Hello {
public String sayHello(String name);
}
HelloImpl.java
package com.javaskool;
public class HelloImpl implements Hello {
public String sayHello(String name)
{
System.out.println("i am in SayHello");
return "Hello!! I am "+name;
}
}
MyAllAspect1.java
package com.javaskool.aspect;
import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.IntroductionInterceptor;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.ThrowsAdvice;
public class MyAllAspect1 implements MethodBeforeAdvice,AfterReturningAdvice,IntroductionInterceptor, ThrowsAdvice{
public void before(Method arg0, Object[] arg1,Object arg2) throws Throwable
{
System.out.println("before Called");
}
public void afterReturning(Object arg0,Method arg1, Object[] arg2,Object arg3) throws Throwable
{
System.out.println("After Returning");
}
public Object invoke(MethodInvocation arg0) throws Throwable
{
System.out.println("Invoke Start");
Object o1=arg0.proceed();
System.out.println("Invoke End");
return o1;
}
public boolean implementsInterface(Class arg0) {
// TODO Auto-generated method stub
return false;
}
public void afterThrowing(ClassCastException e)
{
System.out.println("CCE Occured");
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="obj" class="com.javaskool.HelloImpl"/>
<bean id="advice1" class="com.javaskool.aspect.MyAllAspect1"/>
<bean id="aspect1" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="advice1"/>
<property name="pattern" value=".*Hello"/>
</bean>
<bean id="advicedObj" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.javaskool.Hello</value>
</property>
<property name="target" ref="obj"/>
<property name="interceptorNames">
<list>
<value>aspect1</value>
</list>
</property>
</bean>
</beans>
TestDrive.java
package com.javaskool;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class TestDrive {
public static void main(String[] args) {
BeanFactory beans=new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
Hello p=(Hello)beans.getBean("advicedObj");
String s=p.sayHello("Tom");
System.out.println("Value of s : "+s);
}
}
Output
Invoke Start
before Called
i am in SayHello
After Returning
Invoke End
Value of s : Hello!! I am Tom
Download Examples
Click Below to download the Examples
Recent Comments