title: Spring @Cachable method within the same class (self-invocation, proxy issue) - What is the best way to solve it?
body: <p>I'm trying to call a @Cacheable method from within the same class.</p>
  <p>And it didn't work. Because of:</p>
  <blockquote>
  <p>In proxy mode (the default), only external method calls coming in through the proxy are intercepted. This means that self-invocation (in effect, a method within the target object that calls another method of the target object) does not lead to actual caching at runtime even if the invoked method is marked with @Cacheable. Consider using the aspectj mode in this case. Also, the proxy must be fully initialized to provide the expected behavior, so you should not rely on this feature in your initialization code (that is, @PostConstruct).</p>
  </blockquote>
  <p>It means, @Cachable(also @Transactional) works by proxy classes which is Spring AOP in. a internal call in the same class make call by 'this' instead of proxy classes.</p>
  <p>To solve the problem, I should call a method by proxy or using AspectJ(another AOP).
  So, I found 4 solutions.</p>
  <h2>What is your choice? and why others are not recommended?</h2>
  <p>Please, share your opinion!</p>
  <ol>
  <li>using AspectJ (another AOP)</li>
  <li>get the Bean from ApplicationContext and use it</li>
  </ol>
  <pre><code>@Service
  public class UserService implements Service {
  
      @Autowired
      private ApplicationContext applicationContext;
  
      private Service self;
  
      @PostConstruct
      private void init() {
          self = applicationContext.getBean(UserService.class);
      }
  }
  </code></pre>
  <ol start="3">
  <li>self-autowiring using @Resource //since Spring 4.3</li>
  </ol>
  <pre><code>@Component
  @CacheConfig(cacheNames = &quot;SphereClientFactoryCache&quot;)
  public class CacheableSphereClientFactoryImpl implements SphereClientFactory {
  
      /**
       * 1. Self-autowired reference to proxified bean of this class.
       */
      @Resource
      private SphereClientFactory self;
  
      @Override
      @Cacheable(sync = true)
      public SphereClient createSphereClient(@Nonnull TenantConfig tenantConfig) {
          // 2. call cached method using self-bean
          return self.createSphereClient(tenantConfig.getSphereClientConfig());
      }
  
      @Override
      @Cacheable(sync = true)
      public SphereClient createSphereClient(@Nonnull SphereClientConfig clientConfig) {
          return CtpClientConfigurationUtils.createSphereClient(clientConfig);
      }
  }
  </code></pre>
  <ol start="4">
  <li>make the Bean scope of the class as 'prototype' instead of 'singleton'</li>
  </ol>
  <pre><code>@Service
  @Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
  class AService {
  
      private final AService _aService;
  
      @Autowired
      public AService(AService aService) {
          _aService = aService;
      }
  
      @Cacheable(&quot;employeeData&quot;)
      public List&lt;EmployeeData&gt; getEmployeeData(Date date){
          ..println(&quot;Cache is not being used&quot;);
          ...
      }
  
      public List&lt;EmployeeEnrichedData&gt; getEmployeeEnrichedData(Date date){
          List&lt;EmployeeData&gt; employeeData = _aService.getEmployeeData(date);
          ...
      }
  }
  </code></pre>
  <p>I'm a newbie in spring :)</p>
  <p>Actually, I choose the 4th solution, but I felt it isn't a good way. because I just need to call the caching method by proxy, and it make several beans to achieve it.</p>
  <p>After reading articles, I think AspectJ is the best choice. It looks cool, Spring recommends it, and many people also recommend too.</p>
  <p>But I don't understand how to AspectJ works (I will study) and I also don't know why others is not recommended.</p>
  <h3>references</h3>
  <ul>
  <li><a href="https://stackoverflow.com/questions/16899604/spring-cache-cacheable-not-working-while-calling-from-another-method-of-the-s/34091265#34091265">Spring Cache @Cacheable - not working while calling from another method of the same bean</a></li>
  <li><a href="https://stackoverflow.com/questions/12115996/spring-cache-cacheable-method-ignored-when-called-from-within-the-same-class/34090850#34090850">Spring cache @Cacheable method ignored when called from within the same class</a></li>
  <li><a href="https://spring.io/blog/2012/05/23/transactions-caching-and-aop-understanding-proxy-usage-in-spring" rel="nofollow noreferrer">https://spring.io/blog/2012/05/23/transactions-caching-and-aop-understanding-proxy-usage-in-spring</a></li>
  <li><a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/integration.html#cache" rel="nofollow noreferrer">https://docs.spring.io/spring/docs/current/spring-framework-reference/integration.html#cache</a></li>
  </ul>