Warren Goldman
Warren Goldman

Reputation: 125

Spring AOP Custom annotation not firing for my controller

The annotation seems to have no affect. Added more text here to satisfy the editor that this site has requiring a certain amount of verbosity.

My Pom entry

<dependency> 
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-api</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>jcl-over-slf4j</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>jul-to-slf4j</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>log4j-over-slf4j</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>logback-classic</artifactId>
                    <groupId>ch.qos.logback</groupId>
                </exclusion>
            </exclusions>
        </dependency>

In applicationContext.xml (where other beans are defined)

<bean id="myAspect" class="com.myapp.MyAspect" lazy-init="false"/>

My aspect

 package com.myapp;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.springframework.stereotype.Component;
    @Aspect
    @Component
    public class MyAspect {
        @Around("@annotation(LogArguments)")
        public Object logArguments(ProceedingJoinPoint joinPoint) throws Throwable {
            System.err.println("put breakpoint here, never stops here");
            return joinPoint.proceed();
        }
    }

The annotation

package com.myapp;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogArguments {
}

THIS CODE IS INSIDE OF MY CONTROLLER

@RequestMapping(value = "/search", method = RequestMethod.POST)
    @LogArguments
    public @ResponseBody SearchResult performSearch(@RequestBody SearchForm 
    searchForm, HttpServletRequest request) throws Exception {
    LOG.debug("If I put a break point here it stops here, but not in the aspects code:" + searchForm);
    }

Upvotes: 3

Views: 3718

Answers (2)

Indra Basak
Indra Basak

Reputation: 7394

Changes to Aspect

  • Remove @Component annotation
  • Modify the @Around annotation and the logArguments method signature to make it work. The below example should work,

    @Aspect
    public class MyAspect {
    
        @Around("@annotation(annotation) || @within(annotation)")
        public Object logArguments(ProceedingJoinPoint joinPoint,
            LogArguments annotation) throws Throwable {
            System.out.println("put breakpoint here, never stops here");
    
            return joinPoint.proceed();
        }
    }
    

Changes to applicationContext.xml

  • Make sure to add <aop:aspectj-autoproxy />
  • I don't think you need to specify lazy-init

    <aop:aspectj-autoproxy />
    
    <bean id="myAspect" class="com.myapp.MyAspect"/>
    

Upvotes: 1

John Humphreys
John Humphreys

Reputation: 39304

Have you made sure both the location you are defining the annotation in is component scanned and that the location you are using it in is component scanned?

Also, I'm not sure if this matters; but I've generally seen people/examples put full package qualifiers in @around (on LogArguments in this case).

Upvotes: 0

Related Questions