Announcement Announcement Module
No announcement yet.
First method works the second doesn't. Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • First method works the second doesn't.

    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
    package main;
    public class Starter extends Thread{
    	private Runner runner;
    	public Starter(Runner runner) {
    		this.runner = runner;
    	public void run() {
    The class with the actual logic
    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:
    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"?>
    	<bean id="Runner" class="main.Runner"/>
    	<bean id="Logger" class="main.Logger"/>
    		<aop:aspect ref="Logger">
    			<aop:pointcut expression="execution (* main.Runner.step(..))" id="method-to-log"/>
    			<aop:after method="after" pointcut-ref="method-to-log" />
    The main class:
    package main;
    import org.springframework.context.ApplicationContext;
    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);
    Thanks in advance!

  • #2

    AOP doesn't work on the step() method because you don't call this method externally but call it only inside the class itself. Spring AOP is proxy-based so internal method calls can't be advised. You need tu set up full-blown AspectJ for that, or change your application logic so that the method step() is called from another class.

    Aside from that, there are also many other problems with the code you submitted:

    1) In a Spring application, you should never spawn threads the "Java way" but use the @Async annotation which is the "Spring way" to accomplish just the same. By doing this, you have the guarantee that Spring's additional features (like AOP) will work as expected. By spawning threads yourself, you are working against the framework so there is no guarantee that everything will work as expected;

    2) Another way in which you go against the framework, is that you don't use dependency injection, which is the foundation of Spring. If you use spring correctly, the "new" java keyword should be banned. You should inject your runner inside your starter and, in the main class, you should get directly the starter class;

    3) Also, respect naming conventions. Ids of Spring beans should start lowercase.

    All this urges me to suggest a more careful study of the Spring documentation.