1

I am trying to create a spring boot library with Custom annotation and Spring AOP. When I used this library with new spring boot application. Then Its not working. Even I am not getting any error.

Library Sample -

Custom Annotation

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 HttpLogger {
}

Spring AOP class

@Aspect
class LoggingAspect {
@Around("@annotation(com.demo.commonlogging.aspect.HttpLogger)")
    public Object inControllers(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        return loggingAdvice(proceedingJoinPoint); // Method for implementation
    }
}

pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

Using mvn clean install for creating library

Now new library is imported in springboot application.

And new Custom annotation is used in controllers

Controllers

@RestController
@RequestMapping(value = "/rest/test")
public class RestApiTestControllers {
    @GetMapping
    @HttpLogger
    public String get(){
        return "Hello !";
    }
}

Please help here.

theBittor
  • 786
  • 1
  • 11
  • 20
vineet
  • 59
  • 1
  • 4
  • There are so many possible root causes for your problem, without a full [MCVE](https://stackoverflow.com/help/mcve) the best anyone can do is speculate, and that is not nice. My best first shot would be that you forgot the `@Component` annotation on your aspect class, hence the aspect is never picked up by component scan. But you already said that is not working either. An MCVE, ideally on GitHub, would be much better. Otherwise nobody will be able to analyse + answer your question. – kriegaex Feb 28 '20 at 02:19

1 Answers1

0

Seems like you are missing @Component from LoggingAspect also make call to proceed proceedingJoinPoint.proceed(); and return it's value.

So your code should look like:


@Aspect
@Component
class LoggingAspect {
    @Around("@annotation(com.demo.commonlogging.aspect.HttpLogger)")
    public Object inControllers(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("Before call");
        Object returned = proceedingJoinPoint.proceed();
        System.out.println("After call");
        return returned;
    }
}

Hope this helps!

  • I am trying to use this custom annotation as a library. – vineet Feb 27 '20 at 14:55
  • @vineet what _Neetesh_ suggested should work. please update the question with the complete package structure for your library and the class with `@SpringBootApplication` annotation. It seems the components are not auto detected – R.G Feb 28 '20 at 01:59
  • @vineet, did it work for you? Because I have the same problem and adding Component annotation didn't help :-/ – Kevin Macejko Apr 18 '22 at 13:41