Hüseyin BABAL
Hüseyin BABAL

Reputation: 15550

Spring @Cacheable doesn't cache public methods

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

Upvotes: 2

Views: 3969

Answers (1)

Roadrunner
Roadrunner

Reputation: 6801

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.

Upvotes: 4

Related Questions