Spring AOP Example
It’s a simple implementation of Spring
AOP.
Project Contents:
|
File Name
|
Use
|
|
service.xml
|
Configuration
File.
|
|
ServiceClass
|
Target
Class on which we will apply AOP.
|
|
AdviceClass
|
Advisor
class which will be applied on target class.
|
|
SeviceUtilizer
|
It will
control the overall process.
|
service.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.5.xsd">
<bean
id="serviceClass" class="aop.ServiceClass">
<property
name="name" value="Hemant Kumar" />
<property
name="url" value="http://www.hemant.com" />
</bean>
<bean
id="advice" class="aop.AdviceClass" />
<bean
id="myProxy"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property
name="target" ref="serviceClass" />
<property
name="interceptorNames">
<list>
<value>advice</value>
</list>
</property>
</bean>
</beans>
package aop;
public class ServiceClass
{
private
String name;
private
String url;
public
void setName(String name)
{
this.name
= name;
}
public
void setUrl(String url)
{
this.url
= url;
}
public
void printName()
{
System.out.println("Customer
name : " + this.name);
}
public
void printURL()
{
System.out.println("Customer
website : " + this.url);
}
public
void printThrowException()
{
throw
new IllegalArgumentException();
}
}
AdviceClass
package aop;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.ThrowsAdvice;
public class AdviceClass implements MethodBeforeAdvice,
AfterReturningAdvice, ThrowsAdvice
{
@Override
public
void before(Method method, Object[] args, Object clazz) throws Throwable
{
System.out.println("Going
into Class Name: " + clazz.getClass().getSimpleName() + " Method Name: " + method.getName());
}
@Override
public
void afterReturning(Object obj, Method method, Object[] arg2,Object clazz)
throws Throwable
{
System.out.println("Returning
from Class Name: "+ clazz.getClass().getSimpleName() + " Method Name: " + method.getName());
}
public
void afterThrowing(Method method,Object arg[],Object clazz,Exception exception)
throws Throwable
{
System.out.println(exception.getClass().getSimpleName()
+ " thrown from Class Name: " + clazz.getClass().getSimpleName() +
" Method Name: " +
method.getName());
}
}
SeviceUtilizer
package aop;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public class SeviceUtilizer
{
public
static void main(String[] args)
{
ApplicationContext
appContext = new
ClassPathXmlApplicationContext(new String[] { "service.xml" });
ServiceClass
obj = (ServiceClass) appContext.getBean("myProxy");
obj.printName();
obj.printURL();
try
{
obj.printThrowException();
}
catch
(Exception e) { }
}
}
Comments
Post a Comment