Hi there,
I have a very weird and irritating problem. I'll try to explain it as clear as possible. The project I was working on was a little more complex but I've recreated the same problem with a very simple example.
I have 4 different classes:
- Starter: This class extends from Thread and calls Runner.startRunning() as soon as it has been started.
- Runner: The runner starts to run for a distance of 150 (let's say miles). Every step() returns a random int that gets added to the total distance.
- Logger: The logger tries to log every step the Runner takes. So every time the Runner.step() method is executed.
- Main: Just a main method to start all the running via the Thread.
AOP looked the best way to solve the logging problem so I started creating a config file. But apparently it isn't possible to make a pointcut of the Runner.step() method! But a pointcut for the Runner.startRunning() method works perfectly. See example code below or eclipse project on this link.
Simple class for the Threading
Code:
package main;
public class Starter extends Thread{
private Runner runner;
public Starter(Runner runner) {
this.runner = runner;
}
@Override
public void run() {
this.runner.startRunning();
}
}
The class with the actual logic
Code:
package main;
import java.util.Random;
public class Runner {
public void startRunning() {
System.out.println("Started Running");
int distance = 0;
do {
distance += step();
} while (distance < 150);
}
public int step() {
return new Random().nextInt(5);
}
}
The logger:
Code:
package main;
public class Logger {
public Logger(){}
public void after() {
System.out.println("Took a step");
}
}
The config:
HTML Code:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.0.xsd">
<bean id="Runner" class="main.Runner"/>
<bean id="Logger" class="main.Logger"/>
<aop:config>
<aop:aspect ref="Logger">
<aop:pointcut expression="execution (* main.Runner.step(..))" id="method-to-log"/>
<aop:after method="after" pointcut-ref="method-to-log" />
</aop:aspect>
</aop:config>
</beans>
The main class:
Code:
package main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-runner.xml");
Runner runner = (Runner) context.getBean("Runner");
Starter starter = new Starter(runner);
starter.start();
}
}
Thanks in advance!