In Aspect Oriented Programming (AOP) Advice define the what and when we call . The Pointcuts define where.A pointcut definition matches one or more jointpoints .Here discuss a simple example to use pointcut -
Directory Structure-
Circle Class-
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
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"
xmlns:context="http://www.springframework.org/schema/context">
<aop:aspectj-autoproxy/>
<bean id="triangle" class="com.test.App.Triangle">
<property name="name" value="triangle drawn"></property>
</bean>
<bean id="circle" class="com.test.App.Circle">
<property name="name" value="circle drawn"></property>
</bean>
<bean id="shape" class="com.test.service.Shape" autowire="byName">
</bean>
<bean id="log" class="com.test.aspect.LogAspect"></bean>
</beans>
Output
Directory Structure-
Circle Class-
package com.test.App;
public class Circle {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Triangle Class-
public class Circle {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.test.App;
public class Triangle {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Class shape-
public class Triangle {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
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;
}
}
Class LogAspect-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;
}
}
package com.test.aspect;
//import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class LogAspect {
@Before("allGetters() && allcircle()")
public void loggingAdvice()
{
System.out.println("Wel Come With....logging advice");
}
@Pointcut("execution(* get*())")
public void allGetters()
{
System.out.println("Wel Come With....allgetters");
}
@Pointcut("within(com.test.App.Circle)")
public void allcircle()
{
System.out.println("Wel Come With....circle");
}
}
Class Main-
//import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class LogAspect {
@Before("allGetters() && allcircle()")
public void loggingAdvice()
{
System.out.println("Wel Come With....logging advice");
}
@Pointcut("execution(* get*())")
public void allGetters()
{
System.out.println("Wel Come With....allgetters");
}
@Pointcut("within(com.test.App.Circle)")
public void allcircle()
{
System.out.println("Wel Come With....circle");
}
}
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());
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
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"
xmlns:context="http://www.springframework.org/schema/context">
<aop:aspectj-autoproxy/>
<bean id="triangle" class="com.test.App.Triangle">
<property name="name" value="triangle drawn"></property>
</bean>
<bean id="circle" class="com.test.App.Circle">
<property name="name" value="circle drawn"></property>
</bean>
<bean id="shape" class="com.test.service.Shape" autowire="byName">
</bean>
<bean id="log" class="com.test.aspect.LogAspect"></bean>
</beans>
Output
triangle drawn
Wel Come With....logging advice
circle drawn
Here logginAdvice() combind all type of getter and setter method and call the function.Method allCircle() call when Circle class's methods calls.so all other methods indirectly pass to logginAdvice().
Wel Come With....logging advice
circle drawn
For Further Reading,
0 comments:
Post a Comment