0

Here is the interface code. Here the abstract method getItemById is called inside findById. This was causing the spring proxy to not work as intented.

  @Nullable
  T getItemById(@NotNull final String id);

  @NotNull
  default Optional<T> findById(@Nullable final String id) {
    return isBlank(id) ? Optional.empty() : Optional.ofNullable(getItemById(id));
  }

The following is an implementation of of the interface :

      @Override
      @Nullable
      @Cacheable(value = "cartCache")
      public Cart getItemById(@NotNull final String id) {
        //get data from DB
      }

When I call the method from the autowired class directly service.getItemById() then it would work. But when I call service.findById()it isn't working due to aspect not getting applied for the method.I have read tht aspectj being a solution but I don't know it's working very well and how to implement it in the following case.

I have came across a post which uses @Scope(proxyMode = ScopedProxyMode.TARGET_CLASS) annotation. But it is on a class and an object of the service is also create inside itself. As I coulnd't do this in my interface, I don't know how to proceed.

arunken
  • 415
  • 3
  • 15
  • Where is your full, minimal, reproducible example? I cannot compile and debug an incoherent set of snippets. There are simply too many options what you could have done wrong, and this is not a guessing game. – kriegaex Mar 08 '22 at 09:52
  • Looking at this question again, I guess it is simply a duplicate of [that one](https://stackoverflow.com/a/56616248/1082681). – kriegaex Mar 09 '22 at 01:43

0 Answers0