Posts Tagged ‘Spring AOP’

Simple caching with Spring AOP

September 18, 2009 1 comment

It’s recommended to read Simple caching with AspectJ before you continue. Only difference is in using Spring AOP.
At first we need to create spring-config.xml and define Spring beans and enable Spring AOP autoproxy for AspectJ:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

	<aop:aspectj-autoproxy />
	<bean class="caching.springaop.CacheAspect" />
	<bean id="calc" class="caching.springaop.Calculator" />


Now we can use Calculator bean created by Spring:

ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-config.xml");
Calculator calc = (Calculator) ctx.getBean("calc");

// result will be calculated and stored in cache"1 + 2 = " + calc.sum(1, 2));

// result will be retrieved from cache"1 + 2 = " + calc.sum(1, 2));

Download full source code as a zip or tar.gz file or browse code online on GitHub.