1 year ago

#264295

test-img

arunken

Spring cache proxy issue when method called within the same class

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.

java

caching

spring-aop

aspect

0 Answers

Your Answer

Accepted video resources