How to use Spring’s cache in Alfresco

In this post, we are going to explain how to use Spring’s cache in Alfresco. To implement this feature could be very frustrating if some details are unknown.

First, you should change your context. If you have an amp, go to \src\main\amp\config\alfresco\module\alfresco-repo\module-context.xml
and add the following code to add the cacheManager bean:

<bean id="cacheManager" class="">
	<property name="caches">
			<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" name="cacheName" />

At this point, if you try to use the cache, you will get these exceptions:

org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 4 in XML document from class path resource [alfresco/module/alfresco-repo/context/service-context.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 8; cvc-elt.1.a: Cannot find the declaration of element 'beans'.
nested exception is org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 8 in XML document from class path resource [alfresco/module/alfresco-repo/context/service-context.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 112; Attribute "xmlns" must be declared for element type "beans".

So, secondly, comment this line

<!--  <!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' ''> -->

and replace the tag <bean> with

<beans xmlns="" 

Now, Spring’s cache is ready.

To use the cache with a particular method, the class must implement an interface, and we should add the tag @Cacheable(“cacheName”) to the method. For instance:

public class SecurityService implements ISecurityService {
	 /**gets group's permissions list
	 * The result is cached using the key (groupId).
	 * @param groupId
	 * @return
	@Cacheable("userService")//Cache this method in userService cache
	public Set<String> getGroupPermissions(String groupId) {

Note: The cache is not used when a method is called within the same class. we will show a trick to solve this issue later.

It’s important to know that if we want to use our bean, we should inject it as an interface, otherwise we will get the following exception (Spring works with a proxy which implements the interface, therefore it can’t cast the implementations with the interface):

java.lang.ClassCastException: com.sun.proxy.$Proxy158 cannot be cast to info.SecurityService

To solve that, inject the bean as follow:

 @Qualifier("id.bean.securityService ") 
 private ISecurityService securityService ;

Finally, if we want to use the cache with methods within the same class, we can apply a similar solution to this code:

public class SettingServiceImpl implements SettingService {

private SettingRepository settingRepository;

private ApplicationContext applicationContext;

public String findValue(String name) {
    Setting setting = settingRepository.findOne(name);
    if(setting == null){
        return null;
    return setting.getValue();

public Boolean findBoolean(String name) {
    String value = getSpringProxy().findValue(name);
    if (value == null) {
        return null;
    return Boolean.valueOf(value);

 * Use proxy to hit cache 
private SettingService getSpringProxy() {
    return applicationContext.getBean(SettingService.class);

Leave a Reply

Your email address will not be published. Required fields are marked *