Advertisement
package decodejava;
public class MarathonRunner
{
String name;
int age;
//Getter method for age
public int getAge() {
return age;
}
//Setter method for age
public void setAge(int age) {
this.age = age;
}
//Getter method for name
public String getName()
{
return name;
}
//Setter method for name
public void setName(String name)
{
this.name = name;
}
public void marathonBegins()
{
System.out.println("Marathon runner has started the race");
}
public void marathonEnds()
{
System.out.println("Marathon runner has finished the race");
}
}
Interface | Description |
---|---|
MethodBeforeAdvice | This interface is used to specify before advice. |
AfterReturningAdvice | This interface is used to specify after-returning advice. |
MethodInterceptor | This interface is used to intercept a call to joinpoint. |
package decodejava;
import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
public class MarathonTimeTracker implements MethodInterceptor, MethodBeforeAdvice, AfterReturningAdvice
{
MethodInvocation me;
public void trackingBegins()
{
System.out.println("Marathon has started");
System.out.println("Tracking the marathon has begun");
}
public void trackingEnds()
{
System.out.println("Marathon has ended");
System.out.println("Tracking the marathon has ended");
}
@Override
public void afterReturning(Object arg0, Method method, Object[] arg2, Object arg3) throws Throwable
{
if("marathonEnds".equals(me.getMethod().getName()))
{
System.out.println("After Method - " + method.getName());
trackingEnds();
}
}
@Override
public void before(Method method , Object[] arg1, Object arg2) throws Throwable
{
if("marathonBegins".equals(me.getMethod().getName()))
{
System.out.println("Before Method - " + method.getName());
trackingBegins();
}
}
@Override
public Object invoke(MethodInvocation me) throws Throwable
{
this.me = me;
return me.proceed();
}
}
Advertisement
package decodejava;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class Utility
{
public static void main(String[] ar)
{
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:config.beans.xml");
MarathonRunner mr = context.getBean("MarathonBean", MarathonRunner.class);
System.out.println("Name of the Marathon Runner - " + mr.getName());
System.out.println("Age of the Marathon Runner - " + mr.getAge());
mr.marathonBegins();
mr.marathonEnds();
}
}
<?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:util="springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<bean id="MarathonRunnerBean" class="decodejava.MarathonRunner">
<property name="name" value="Runner1"></property>
<property name="age" value="27"></property>
</bean>
<bean id="MarathonTimeTrackerAdvisorBean" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="mappedNames" value="marathonEnds,marathonBegins"></property>
<property name="advice">
<bean class="decodejava.MarathonTimeTracker"></bean>
</property>
</bean>
<bean id="MarathonBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="MarathonRunnerBean"></property>
<property name="interceptorNames" value="MarathonTimeTrackerAdvisorBean"></property>
</bean>
</beans>
Class | Description |
---|---|
ProxyFactoryBean | This class allows us to create a proxy class in whic we have defined the pointcut and advices. |
NameMatchMethodPointcut | This class takes the name of the methods as pointcut. |
Aug 07, 2018 12:00:47 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@5e91993f: startup date [Tue Aug 07 12:00:47 2018]; root of context hierarchy
Aug 07, 2018 12:00:48 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [config.beans.xml]
Before Method - marathonBegins
Marathon has started
Tracking the marathon has begun
Marathon runner has started the race
Marathon runner has finished the race
After Method - marathonEnds
Marathon has ended
Tracking the marathon has ended
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement