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,
- Controller calls
BookServiceImpl.getDetailsInfo()
method. getBookDetails()
is called inside this method after some conditions@Before
Aspect is called before thegetBookDetails()
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 !!