user2930538
user2930538

Reputation: 131

@Around not getting called in Spring AOP

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

Answers (1)

Efe Kahraman
Efe Kahraman

Reputation: 1438

Add @Component to MethodLogger class. Also you have to enable AspectJ like one of the following ways:

  • Adding @EnableAspectJAutoProxy to your configuration bean class. (annotation driven approach)
  • Adding <aop:aspectj-autoproxy /> to application context file. (XML driven approach)

Upvotes: 2

Related Questions