0% found this document useful (0 votes)
31 views

AOP Example PRGM

The document defines an aspect class SampleAspectEx that uses annotations to add advice (before advice) to methods in certain packages and classes. It also defines several classes with get/set methods and a configuration file that wires up the aspect and classes. The test class loads the configuration, gets beans, and calls methods to test the aspect advice is triggered properly.

Uploaded by

Sagar Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

AOP Example PRGM

The document defines an aspect class SampleAspectEx that uses annotations to add advice (before advice) to methods in certain packages and classes. It also defines several classes with get/set methods and a configuration file that wires up the aspect and classes. The test class loads the configuration, gets beans, and calls methods to test the aspect advice is triggered properly.

Uploaded by

Sagar Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

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.ClassPathXmlApplicationC
ontext;

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.

You might also like