Question

Can someone pinpoint what am I doing wrong? How can I make my Aspect run?

I have written this code following some examples:

@Aspect
public class MethodLogger {

    private Logger log = Logger.getLogger(getClass().getName());

    @Pointcut("execution(* setHeight(..))")
    public void log(JoinPoint point) {
        log.info(point.getSignature().getName() + " called...");

    }
}

My simple test class:

public class ChairImpl implements Chair {
    int height;
    public ChairImpl(){}

    public int getHeight() {
        return height;
    }
    public void setHeight(int height) {
        this.height = height;
    }


}

My Spring xml config:

<?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:p="http://www.springframework.org/schema/p"
        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-3.0.xsd 
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
        default-destroy-method="destroy"
        default-init-method="afterPropertiesSet"
        default-autowire="byName"
>

<aop:aspectj-autoproxy>
        <aop:include name="MethodLogger"/>
    </aop:aspectj-autoproxy>

    <bean id="logger1" class="lab.MethodLogger"/>

    <bean id="chair1" 
    class="lab.test.ChairImpl"
    >
        <property name="height" value="10"/>
    </bean>

</beans>

And my main method:

public class Main {
    public static void main(String[] args){
        ApplicationContext context = new ClassPathXmlApplicationContext("spec.xml");
        ((AbstractApplicationContext) context).close();

    }
}

So before running my project, Eclipse gives me this error(it marks red void word for log method):

Pointcuts without an if() expression should have an empty method body

When I run, my program runs without errors, because it looks like log method never ran. So how could I fix it so it would run and output log? I tried simply to print test text from that method, but it never does, so it means it never runs. What am I missing here?

In documentation it only writes vague examples like:

@Pointcut("execution(* transfer(..))")// the pointcut expression
private void anyOldTransfer() {}// the pointcut signature

But I find it hard to understand how to actually use it to see results.

Was it helpful?

Solution

You can try an anonymous pointcut this way:

@Before("execution(* setHeight(..))")
public void log(JoinPoint point) {
    log.info(point.getSignature().getName() + " called...");

}

or give your pointcut a name and use it this way:

@Pointcut("execution(* setHeight(..))")
public void setters() {}

@Before("setters()") 
public void log(JoinPoint point) {
...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top