Reputation: 663
I am using Java 8, spring 4.3 and AspectJ 1.8.9. Why I am getting the below error for the below code. If I use the @Before("com.beans.Student.addCustomer()") without pointcut I am getting this error at 0 can't find referenced pointcut. When using @Before with a pointcut I am not getting the error.
Beans:
@Aspect
public class Beforeaspect {
/*
* @Pointcut("execution(* com.beans.Student.addCustomer(..))") public void
* log(){
* }
*/
// @Before("log()")
@Before("com.beans.Student.addCustomer()")
public void logBefore(JoinPoint jp) {
System.out.println("logbefore");
System.out.println("method " + jp.getSignature().getName());
}
}
Student:
package com.beans;
public class Student implements Studentimpl {
public void addCustomer() {
System.out.println("addcustomer");
}
public String addCustomername(String stud) {
System.out.println("addcustomername");
return "hello";
}
}
Spring xml file:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<aop:aspectj-autoproxy />
<bean id="stud" class="com.beans.Student" />
<bean class="com.beans.Beforeaspect" />
</beans>
Upvotes: 1
Views: 3585
Reputation: 44398
You have the wrong syntax used for the method execution. Your annotation should be:
@Before("execution(* com.beans.Student.addCustomer(..))")
public void logBefore(JoinPoint jp) {
System.out.println("logbefore");
System.out.println("method " + jp.getSignature().getName());
}
Or use the XML Bean:
<aop:aspectj-autoproxy />
<bean id="logAspect" class="nch.spring.aop.aspectj.LoggingAspect" />
<aop:config>
<aop:aspect id="aspectLoggging" ref="logAspect">
<aop:pointcut id="pointCutBefore" expression="execution(* com.beans.Student.addCustomer(..)))" />
<aop:before method="logBefore" pointcut-ref="pointCutBefore" />
<aop:aspect/>
<aop:config>
Upvotes: 2