Aspect Oriented Programming basically provide alternative way to achive the inheritance and the delegation.In AOP define the comman fuctionality in one place,but you can define where and how this functionlity is applied. The basic example of the aspect oriented programming is describe here-
Directory Structure-
Triangle.java-
Main.java
<beans xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<aop:aspectj-autoproxy>
<bean class="com.test.App.Triangle" id="triangle">
<property name="name" value="triangle drawn"></property>
</bean>
<bean class="com.test.App.Circle" id="circle">
<property name="name" value="circle drawn"></property>
</bean>
<bean autowire="byName" class="com.test.service.Shape" id="shape">
</bean>
<bean class="com.test.aspect.LogAspect" id="log"></bean>
</aop:aspectj-autoproxy></beans>
In this example LogAspect is advice class which contain the jointpoint method-loggingAdvice().This method execute Before all method which contain the prototype -public String getName().the code <aop:aspectj-autoproxy/> which written in xml create proxy(or aop objects).
The process of AOP is also called Weaving.The LogAspect class is also called Advice Class.
Output
Directory Structure-
Circle.java-
package com.test.App;
public class Circle
{
String name;
public String getName()
{ return name; }
public void setName(String name)
{ this.name = name; }
}
public class Circle
{
String name;
public String getName()
{ return name; }
public void setName(String name)
{ this.name = name; }
}
Triangle.java-
package com.test.App;
public class Triangle
{
String name;
public String getName()
{ return name; }
public void setName(String name)
{ this.name = name; }
}
LogAspect.java
public class Triangle
{
String name;
public String getName()
{ return name; }
public void setName(String name)
{ this.name = name; }
}
package com.test.aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class LogAspect {
@Before("execution(public String getName())")
public void loggingAdvice()
{
System.out.println("Wel Come With....");
}
}
Shape.java
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class LogAspect {
@Before("execution(public String getName())")
public void loggingAdvice()
{
System.out.println("Wel Come With....");
}
}
package com.test.service;
import com.test.App.Circle;
import com.test.App.Triangle;
public class Shape
{
Circle circle;
Triangle triangle;
public Circle getCircle()
{ return circle; }
public void setCircle(Circle circle)
{ this.circle = circle; }
public Triangle getTriangle()
{ return triangle; }
public void setTriangle(Triangle triangle)
{ this.triangle = triangle; }
}
import com.test.App.Circle;
import com.test.App.Triangle;
public class Shape
{
Circle circle;
Triangle triangle;
public Circle getCircle()
{ return circle; }
public void setCircle(Circle circle)
{ this.circle = circle; }
public Triangle getTriangle()
{ return triangle; }
public void setTriangle(Triangle triangle)
{ this.triangle = triangle; }
}
Main.java
package com.test.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.test.service.Shape;
public class Main
{
public static void main(String[] args)
{
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
Shape sh=context.getBean("shape", Shape.class);
System.out.println(sh.getTriangle().getName());
System.out.println(sh.getCircle().getName());
}
}
spring.xml
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.test.service.Shape;
public class Main
{
public static void main(String[] args)
{
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
Shape sh=context.getBean("shape", Shape.class);
System.out.println(sh.getTriangle().getName());
System.out.println(sh.getCircle().getName());
}
}
<beans xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<aop:aspectj-autoproxy>
<bean class="com.test.App.Triangle" id="triangle">
<property name="name" value="triangle drawn"></property>
</bean>
<bean class="com.test.App.Circle" id="circle">
<property name="name" value="circle drawn"></property>
</bean>
<bean autowire="byName" class="com.test.service.Shape" id="shape">
</bean>
<bean class="com.test.aspect.LogAspect" id="log"></bean>
</aop:aspectj-autoproxy></beans>
The process of AOP is also called Weaving.The LogAspect class is also called Advice Class.
Wel Come With....
triangle drawn
Wel Come With....
circle drawn
triangle drawn
Wel Come With....
circle drawn
For Further Reading,
0 comments:
Post a Comment