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");
}
}
package decodejava;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class MarathonTimeTracker
{
//Creating the first PointCut
@Pointcut("execution(public void MarathonRunner.marathonBegins())")
public void trackingBeginsPointCut1()
{
}
//Creating the second PointCut
@Pointcut ("execution(void MarathonRunner.marathonEnds())")
public void trackingEndsPointCut2()
{
}
@Before("trackingBeginsPointCut1()")
public void trackingBegins(JoinPoint jp)
{
System.out.println("Marathon has started - " + jp.getSignature());
System.out.println("Tracking the marathon has begun");
}
@AfterReturning("trackingEndsPointCut2()")
public void trackingEnds(JoinPoint jp)
{
System.out.println("Marathon has ended - " + jp.getSignature());
System.out.println("Tracking the marathon has ended");
}
}
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("MarathonRunnerBean", 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"
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
">
<bean id="MarathonRunnerBean" class="decodejava.MarathonRunner">
<property name="name" value="Runner1"></property>
<property name="age" value="27"></property>
</bean>
<bean id="MarathonTimeTrackerBean" class="decodejava.MarathonTimeTracker">
<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"></bean>>
</beans>
Aug 03, 2018 6:14:47 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@1eb44e46: startup date [Fri Aug 03 18:14:47 2018]; root of context hierarchy
Aug 03, 2018 6:14:48 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [config.beans.xml]
Name of the Marathon Runner - Runner1
Age of the Marathon Runner - 27
Marathon has started - void decodejava.MarathonRunner.marathonBegins()
Tracking the marathon has begun
Marathon runner has started the race
Marathon runner has finished the race
Marathon has ended - void decodejava.MarathonRunner.marathonEnds()
Tracking the marathon has ended
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement