1

I have a controller class which further calls service class method. An AOP @Before aspect is applied on the service class method.

package com.example;

@RestController
public class BookController {
    @Autowired
    BookService bookService;

    @RequestMapping(value = "/getBookDetails", method = RequestMethod.GET, 
    @RequestBody BookRequest bookRequest))
    public String processBookDetails() {
        System.out.println("Inside controller class");
        String details = bookService.getBookDetailsInfo(bookRequest,bookName);        
    }
}

Service class

package com.example;

@Service
public class BookServiceImpl implements BookService {       

@Override
    public String getBookDetailsInfo(BookRequest bookRequest,String bookName) {
        //some code
       // call getBookDEtails
        getBookDetails(bookInfoObj)
        returns "Book details";

}

@CallBookInfo-- custom annotation for aspect which needs to be executed before getBookDetails is called

getBookDetails(BookInfoObj obj){ }

An aspect is written to be executed @Before the method getBookDetails() of BookServiceImpl

//Custom annotation

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CallBookInfo{}

//Aspect

@Aspect
@Configuration
public class BookAspect {
   @Before("@annotation(com.example.CallBookInfo)") 
   public Object beforeBookAdvice(JoinPoint joinpoint) {
       System.out.println("Start aspect");
       Object result= null;
       try { 
          BookRequest obj= joinpoint.getArgs();
          System.out.println("End aspect");
       }
       catch(Exception e) {}
    return result;
   }
}

Execution goes as below,

  1. Controller calls BookServiceImpl.getDetailsInfo() method.
  2. getBookDetails() is called inside this method after some conditions
  3. @Before Aspect is called before the getBookDetails() due to the custom annotation @CallBookInfo

How to get the BookRequest object which was passed from the Controller class to the service class and after some processing return it back to the service class from the aspect

Thank you in advance !!

MamJsr
  • 11
  • 2
  • It looks like you need a Decorator, not an aspect. Anyway, if you want to use an aspect take a look at the [documentation here](https://docs.spring.io/spring/docs/4.3.15.RELEASE/spring-framework-reference/html/aop.html#aop-ataspectj-advice-params-passing) that describe how to pass parameters to advice – Federico Piazza May 22 '20 at 20:09
  • Please edit your question so as to show your application class and how it is being called. This is how it works on StackOverflow. Speculating about invisible code is neither helpful for the community trying to answer your question nor for yourself, getting imprecise or even plain wrong answers. Thank you very much. – kriegaex May 23 '20 at 08:37
  • Hi Edited my question to add the code snippets to give more context to the question. Thank you – MamJsr May 25 '20 at 18:32
  • Is `getBookDetails()` a method within `BookServiceImpl` ? If yes , internal method calls cannot be intercepted by Spring AOP . Details [here](https://stackoverflow.com/q/13564627/4214241) – R.G May 26 '20 at 04:12
  • Your code is pseudo code. Exactly the crucial part, the definition of the method you want to intercept, you left out. Instead you wrote forbidden syntax like `@CallBookInfo getBookDetails(BookInfo obj)` into a method body, so nobody knows if you just have a syntax error in your code or want to tell us that this is the method you wanted to call. Let your code speak for itself, don't cripple it by omissions in the wrong places. As R.G speculated, I also think you want to do self-invocation from one method within a class to another one, which is not supported by Spring AOP. – kriegaex May 26 '20 at 07:36

0 Answers0