From 0c09ec3bd1e89b8a33032aec6091a2bbce19e2fa Mon Sep 17 00:00:00 2001 From: Mic Date: Thu, 14 Feb 2013 11:12:36 +0800 Subject: [PATCH] added @Cacheable support with ehcache --- .springBeans | 3 +- pom.xml | 12 + .../jdbc/JdbcVetRepositoryImpl.java | 37 +- .../repository/jpa/JpaVetRepositoryImpl.java | 2 + src/main/resources/ehcache.xml | 17 + src/main/resources/ehcache.xsd | 418 ++++++++++++++++++ src/main/resources/log4j.dtd | 0 src/main/resources/log4j.xml | 1 - .../{jmx-aop-config.xml => tools-config.xml} | 20 +- src/main/webapp/WEB-INF/web.xml | 2 +- 10 files changed, 479 insertions(+), 33 deletions(-) create mode 100644 src/main/resources/ehcache.xml create mode 100644 src/main/resources/ehcache.xsd mode change 100755 => 100644 src/main/resources/log4j.dtd rename src/main/resources/spring/{jmx-aop-config.xml => tools-config.xml} (66%) diff --git a/.springBeans b/.springBeans index 2f9dc4c20..1dea40264 100644 --- a/.springBeans +++ b/.springBeans @@ -11,6 +11,7 @@ src/main/webapp/WEB-INF/mvc-view-config.xml src/main/resources/spring/dao-config.xml src/main/resources/spring/datasource-config.xml + src/main/resources/spring/tools-config.xml @@ -22,7 +23,7 @@ src/main/webapp/WEB-INF/mvc-view-config.xml src/main/resources/spring/dao-config.xml src/main/resources/spring/datasource-config.xml - src/main/resources/spring/jmx-aop-config.xml + src/main/resources/spring/tools-config.xml diff --git a/pom.xml b/pom.xml index 82da93b2f..a6d787106 100644 --- a/pom.xml +++ b/pom.xml @@ -31,6 +31,11 @@ + + org.springframework + spring-context-support + ${spring.version} + org.springframework spring-orm @@ -204,6 +209,13 @@ test + + net.sf.ehcache + ehcache + 2.5.2 + pom + + joda-time joda-time diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVetRepositoryImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVetRepositoryImpl.java index d97f48465..79b3e5a0b 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVetRepositoryImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVetRepositoryImpl.java @@ -6,14 +6,12 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.Cacheable; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper; import org.springframework.jdbc.core.simple.ParameterizedRowMapper; -import org.springframework.jmx.export.annotation.ManagedOperation; import org.springframework.samples.petclinic.model.Specialty; import org.springframework.samples.petclinic.model.Vet; import org.springframework.samples.petclinic.repository.VetRepository; @@ -28,16 +26,13 @@ import org.springframework.stereotype.Repository; * @author Sam Brannen * @author Thomas Risberg * @author Mark Fisher + * @author Michael Isvy */ @Repository public class JdbcVetRepositoryImpl implements VetRepository { - private final Logger logger = LoggerFactory.getLogger(getClass()); - private JdbcTemplate jdbcTemplate; - private final List vets = new ArrayList(); - @Autowired public JdbcVetRepositoryImpl(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; @@ -47,14 +42,11 @@ public class JdbcVetRepositoryImpl implements VetRepository { * Refresh the cache of Vets that the ClinicService is holding. * @see org.springframework.samples.petclinic.model.service.ClinicService#findVets() */ - @ManagedOperation - public void refreshVetsCache() throws DataAccessException { - synchronized (this.vets) { - this.logger.info("Refreshing vets cache"); - - // Retrieve the list of all vets. - this.vets.clear(); - this.vets.addAll(this.jdbcTemplate.query( + @Cacheable(value="vets") + public Collection findAll() throws DataAccessException { + List vets = new ArrayList(); + // Retrieve the list of all vets. + vets.addAll(this.jdbcTemplate.query( "SELECT id, first_name, last_name FROM vets ORDER BY last_name,first_name", ParameterizedBeanPropertyRowMapper.newInstance(Vet.class))); @@ -64,7 +56,7 @@ public class JdbcVetRepositoryImpl implements VetRepository { ParameterizedBeanPropertyRowMapper.newInstance(Specialty.class)); // Build each vet's list of specialties. - for (Vet vet : this.vets) { + for (Vet vet : vets) { final List vetSpecialtiesIds = this.jdbcTemplate.query( "SELECT specialty_id FROM vet_specialties WHERE vet_id=?", new ParameterizedRowMapper() { @@ -77,17 +69,6 @@ public class JdbcVetRepositoryImpl implements VetRepository { vet.addSpecialty(specialty); } } + return vets; } - } - - public Collection findAll() throws DataAccessException { - synchronized (this.vets) { - if (this.vets.isEmpty()) { - refreshVetsCache(); - } - return this.vets; - } - } - - } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVetRepositoryImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVetRepositoryImpl.java index 63a4e729c..82bfb7e98 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVetRepositoryImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVetRepositoryImpl.java @@ -5,6 +5,7 @@ import java.util.Collection; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; +import org.springframework.cache.annotation.Cacheable; import org.springframework.samples.petclinic.model.Vet; import org.springframework.samples.petclinic.repository.VetRepository; import org.springframework.stereotype.Repository; @@ -27,6 +28,7 @@ public class JpaVetRepositoryImpl implements VetRepository { private EntityManager em; + @Cacheable(value="vets") @SuppressWarnings("unchecked") public Collection findAll() { return this.em.createQuery("SELECT vet FROM Vet vet join fetch vet.specialties ORDER BY vet.lastName, vet.firstName").getResultList(); diff --git a/src/main/resources/ehcache.xml b/src/main/resources/ehcache.xml new file mode 100644 index 000000000..8167066b1 --- /dev/null +++ b/src/main/resources/ehcache.xml @@ -0,0 +1,17 @@ + + + + + + + diff --git a/src/main/resources/ehcache.xsd b/src/main/resources/ehcache.xsd new file mode 100644 index 000000000..2a539199f --- /dev/null +++ b/src/main/resources/ehcache.xsd @@ -0,0 +1,418 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/log4j.dtd b/src/main/resources/log4j.dtd old mode 100755 new mode 100644 diff --git a/src/main/resources/log4j.xml b/src/main/resources/log4j.xml index c39d78d74..f865a8473 100755 --- a/src/main/resources/log4j.xml +++ b/src/main/resources/log4j.xml @@ -10,7 +10,6 @@ - diff --git a/src/main/resources/spring/jmx-aop-config.xml b/src/main/resources/spring/tools-config.xml similarity index 66% rename from src/main/resources/spring/jmx-aop-config.xml rename to src/main/resources/spring/tools-config.xml index 5b2cfb9b1..cf204ba2a 100644 --- a/src/main/resources/spring/jmx-aop-config.xml +++ b/src/main/resources/spring/tools-config.xml @@ -4,8 +4,10 @@ --> - + - + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index aa7171e44..2817b8996 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -23,7 +23,7 @@ id="WebApp_ID" version="2.5"> --> contextConfigLocation - classpath:spring/dao-config.xml, classpath:spring/jmx-aop-config.xml + classpath:spring/dao-config.xml, classpath:spring/tools-config.xml