title: How Spring's Cacheable Annotation can work for class initailized through new Keyword. (In a Class Constructor, initialized through Bean)
body: <p>In our service, we are initializing a bean (say &quot;A&quot;) and that internally constructing a <code>CacheableService</code> Object by using - <code>new CacheableService()</code>. And as I know spring's <code>@Cacheable</code> annotations won't work on class method if the class is initialized using &quot;new&quot; Keyword.</p>
  <p>Then what is an alternative or a way to cache method response?</p>
  <p><strong>Scenario :</strong></p>
  <pre class="lang-xml prettyprint-override"><code>&lt;bean class=&quot;com.package.src.A&quot;/&gt;
  </code></pre>
  <pre class="lang-java prettyprint-override"><code>public class A {
      
      Map&lt;String, CacheableService&gt; map;
      public CacheableService2() {
          map = new HashedMap();
          map.put(&quot;a&quot;, new CacheableService());
      }
  }
  </code></pre>
  <pre class="lang-java prettyprint-override"><code>import org.springframework.cache.annotation.Cacheable;
      
  public class CacheableService {
      
      
      @Cacheable(value = &quot;entityCount&quot;, key = &quot;#criteria.toString()&quot;)
      public int someEntityCount(final String criteria) {
          System.out.println(&quot;Inside function : &quot; + criteria);
          return 5;
      }
  }
  </code></pre>