Aspect Oriented Programming :- Without changing business logic,
a new service can be added to existed layer, using Aspect and
advices we can construct services.
Aspect : it is service class.
Advice: It represents a business method
Pointcut: It is expression, it will select business methods to be
bounded with advices
Aspect:
package com.app;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class SampleAspectEx {
@Before("execution(* com.one.*.*.get*(..))")
public void showMessage(){
System.out.println("Hello I'm from aspect");
}
}
Circle:
package com.one.two;
public class Circle {
private String name;
private int type;
public String getName() {
System.out.println("In-Circle-In-getName():String");
return name;
}
public String getName(int id) {
System.out.println("In-Circle-In-getName(int):String");
return name;
}
public void setName(String name) {
this.name = name;
}
public int getType() {
System.out.println("In-Circle-In-getType():int");
return type;
}
public int getType(int id) {
System.out.println("In-Circle-In-getType(int):int");
return type;
}
public void setType(int type) {
this.type = type;
}
Shape:
package com.one.three;
public class Shape {
private String name;
private int type;
public String getName() {
System.out.println("In-Shape-In-getName():String");
return name;
}
public String getName(int id) {
System.out.println("In-Shape-In-getName(int):String");
return name;
}
public void setName(String name) {
this.name = name;
}
public int getType() {
System.out.println("In-Shape-In-getType():int");
return type;
}
public int getType(int id) {
System.out.println("In-Shape-In-getType(int):int");
return type;
}
public void setType(int type) {
this.type = type;
}
Student:
package com.app.module;
public class Student {
private String name;
private int type;
public String getName() {
System.out.println("In-Student-In-getName():String");
return name;
}
public String getName(int id) {
System.out.println("In-Student-In-getName(int):String");
return name;
}
public void setName(String name) {
this.name = name;
}
public int getType() {
System.out.println("In-Student-In-getType():int");
return type;
}
public int getType(int id) {
System.out.println("In-Student-In-getType(int):int");
return type;
}
public void setType(int type) {
this.type = type;
}
}
Rectangle:
package com.one;
public class Rectangle {
private String name;
private int type;
public String getName() {
System.out.println("In-Rectangle-In-getName():String");
return name;
}
public String getName(int id) {
System.out.println("In-Rectangle-In-
getName(int):String");
return name;
}
public void setName(String name) {
this.name = name;
}
public int getType() {
System.out.println("In-Rectangle-In-getType():int");
return type;
}
public int getType(int id) {
System.out.println("In-Rectangle-In-getType(int):int");
return type;
}
public void setType(int type) {
this.type = type;
}
Employee:
package com.app;
public class Employee {
private String name;
private int type;
public String getName() {
System.out.println("In-Employee-In-getName():String");
return name;
}
public String getName(int id) {
System.out.println("In-Employee-In-
getName(int):String");
return name;
}
public void setName(String name) {
this.name = name;
}
public int getType() {
System.out.println("In-Employee-In-getType():int");
return type;
}
public int getType(int id) {
System.out.println("In-Employee-In-getType(int):int");
return type;
}
public void setType(int type) {
this.type = type;
}
}
Design:
package com.one.two;
public class Design {
private String name;
private int type;
public String getName() {
System.out.println("In-Design-In-getName():String");
return name;
}
public String getName(int id) {
System.out.println("In-Design-In-getName(int):String");
return name;
}
public void setName(String name) {
this.name = name;
}
public int getType() {
System.out.println("In-Design-In-getType():int");
return type;
}
public int getType(int id) {
System.out.println("In-Design-In-getType(int):int");
return type;
}
public void setType(int type) {
this.type = type;
}
Config.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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context
"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/be
ans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-
context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<aop:aspectj-autoproxy/>
<bean class="com.app.Employee" name="emp">
<property name="name">
<value>employee</value>
</property>
<property name="type">
<value>10</value>
</property>
</bean>
<bean class="com.one.two.Circle" name="c1">
<property name="name">
<value>circle</value>
</property>
<property name="type">
<value>20</value>
</property>
</bean>
<bean class="com.one.Rectangle" name="r1">
<property name="name">
<value>rectangle</value>
</property>
<property name="type">
<value>30</value>
</property>
</bean>
<bean class="com.one.three.Shape" name="s1">
<property name="name">
<value>shape</value>
</property>
<property name="type">
<value>40</value>
</property>
</bean>
<bean class="com.app.module.Student"
name="std">
<property name="name">
<value>student</value>
</property>
<property name="type">
<value>50</value>
</property>
</bean>
<bean class="com.one.two.Design" name="d1">
<property name="name">
<value>design</value>
</property>
<property name="type">
<value>60</value>
</property>
</bean>
<bean class="com.app.SampleAspectEx"></bean>
</beans>
Test class:
package com.app;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationCon
text;
import com.app.module.Student;
import com.one.Rectangle;
import com.one.three.Shape;
import com.one.two.Circle;
import com.one.two.Design;
public class Test {
public static void main(String[] args) {
ApplicationContext context=new
ClassPathXmlApplicationContext("config.xml");
Employee e=context.getBean("emp",Employee.class);
Student std=context.getBean("std",Student.class);
Shape s1=context.getBean("s1",Shape.class);
Circle c1=context.getBean("c1",Circle.class);
Rectangle r1=context.getBean("r1",Rectangle.class);
Design d1=context.getBean("d1",Design.class);
//(* com.one.Rectangle.get*())
c1.getName(10);
d1.getName(20);
d1.getName();
c1.getName();
r1.getName();
r1.getType();
r1.getName(10);
r1.getType(10);
JARS:
AOP JARS(only):
http://www.mediafire.com/download/2szvk5sjxgoqkdp/Maven.rar
Also include cglib jar.
http://central.maven.org/maven2/cglib/cglib/3.1/cglib-3.1.jar