2

I have following method;

    @Cacheable(value = "psgSiteToMap", key = "'P2M_'.concat(#siteName)")
    public Map getSiteDetail(String siteName) {
        Map map = new HashMap();
        .....
        //construct map variable here
        .......
        return map;
    }

While project startup, cannot autowire class this method belongs to. If i change above method as following;

    @Cacheable(value = "psgSiteToMap", key = "'P2M_'.concat(#siteName)")
    private Map getSiteDetail(String siteName) {
        Map map = new HashMap();
        .....
        //construct map variable here
        ................
        return map;
    }

    public Map getSiteDetailPublic(String siteName) {
         return this.getSiteDetail(siteName);
    }

it works. Is there any restriction on @Cacheable annotation for public methods?

Thanks in advance

2
  • I have used it in my project for public methods, it just works fine. Also the Ehcache documentation doesn't say anything of that sort. The example shown in the link uses a public method. Commented Aug 12, 2013 at 11:02
  • @ShashankKadne In other class, i also use for public methods and it works fine. However, in this class it doesn't work for method getSiteDetail(String siteName) Commented Aug 12, 2013 at 11:05

1 Answer 1

5

Spring AOP works only on public methods by default. You'd need AspectJ and load time or compile time weaving to make it work on private methods.

So it works in your case means that when you move the @Cacheable to the private method the proxy is not created at all and that works is autowireing, but not caching.

You probably have not set proxy-target-class property in your XML configuration or its equivalent annotation attribute. Can you please add the Spring configuration you're using and the class definition line. I'm interested if it implements any interfaces? Than I'll expand my answer with more details.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.