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;
public class MarathonTimeTracker
{
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");
}
}
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"
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="MarathonTimeTrackerBean" class="decodejava.MarathonTimeTracker"></bean>
<aop:config>
<aop:pointcut id ="trackingBeginsPointCut1"
expression ="execution(public void decodejava.MarathonRunner.marathonBegins())"/>
<aop:pointcut id ="trackingEndsPointCut2"
expression ="execution(public void decodejava.MarathonRunner.marathonEnds())"/>
<aop:aspect ref="MarathonTimeTrackerBean">
<aop:before method="trackingBegins()" pointcut-ref="trackingBeginsPointCut1"/>
<aop:after-returning method="trackingEnds()" pointcut-ref="trackingEndsPointCut2"/>
</aop:aspect>
</aop:config>
</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]
Marathon has started
Tracking the marathon has begun
Marathon runner has started the race
Marathon runner has finished the race
Marathon has ended
Tracking the marathon has ended
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement