Reputation: 131
I am trying to log a method using AOP in Spring. I tried the following code with just System.out.println()
but it's not getting called.
Created annotation:
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = ElementType.METHOD)
public @interface Loggable {
}
Created Aspect
@Aspect
public class MethodLogger {
@Around("execution(* *(..)) && @annotation(Loggable)")
public Object around(ProceedingJoinPoint point) throws Throwable {
System.out.println("this is called");
return result;
}
}
Used logging in a method in service layer
@Service("RegionService")
@Transactional(readOnly = false)
public class RegionService implements IRegionService{
@Loggable
@Override
public List<> fetch() {
return dao.xyz();
}
}
Spring configuration
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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
>
<context:component-scan base-package="com.sst.tender.spring"/>
<context:annotation-config/>
<context:spring-configured/>
</beans>
Upvotes: 1
Views: 1757
Reputation: 1438
Add @Component
to MethodLogger
class. Also you have to enable AspectJ like one of the following ways:
@EnableAspectJAutoProxy
to your configuration bean class. (annotation driven approach)<aop:aspectj-autoproxy />
to application context file. (XML driven approach)Upvotes: 2