跳至主要内容

博文

目前显示的是标签为“Spring4”的博文

Caching with Google Guava

Spring Caching with Guava In Spring core, it also provides Guava Cache support. Enable Caching Add   @EnableCaching(mode=AdviceMode.ASPECTJ)   annotation on the configuration class if you are using Java based configuration. @Configuration ... @EnableCaching(mode=AdviceMode.ASPECTJ) public class JpaConfig {...} Then, specify a   CacheManager   provider in your configuration. There is a   GuavaCacheManager   provided by Spring. @Override @Bean public CacheManager cacheManager() { return new GuavaCacheManager(); } @Override @Bean public KeyGenerator keyGenerator() { return new SimpleKeyGenerator(); } Do not forget to add the   guava   dependency in your Maven pom.xml file. <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> </dependency> Code sample Check out the sample codes. https://github.com/hantsy/spring-sandbox4 And explore the   spring-cache-guava...

Caching with EhCache

Spring Caching with EhCache Alternatively, you can use Ehcache and use the Ehcache specific   CacheManager   as cache provider. Enable Caching Add   @EnableCaching(mode=AdviceMode.ASPECTJ)   annotation on the configuration class if you are using Java based configuration. @Configuration ... @EnableCaching(mode=AdviceMode.ASPECTJ) public class JpaConfig {...} Then, specify a   CacheManager   provider in your configuration. There is a   EhCacheCacheManager   provided by Spring. @Bean public CacheManager cacheManager() { return new EhCacheCacheManager(ehcache().getObject()); } @Bean public EhCacheManagerFactoryBean ehcache() { EhCacheManagerFactoryBean ehcache = new EhCacheManagerFactoryBean(); //ehcache.setConfigLocation(new ClassPathResource("ehcache.xml")); return ehcache; } Do not forget to add the   ehcache   dependency in your Maven pom.xml file. <dependency> <groupId>net.sf.ehcache</groupId> ...

Spring Caching

Spring cache abstraction Spring provides its own cache solution in 3.x. And it defines a   CacheManager   SPI for third party cache solutions. By default, in the Spring core, it provides two built-in implementations, which based on JDK   ConcurrentMap ,   Ehcache ,   Guava , etc. ConcurrentMap   is an in-memory cache solution, it is useful in the development stage. Enable Spring Caching Add   @EnableCaching(mode=AdviceMode.ASPECTJ)   annotation on the configuration class if you are using Java based configuration. @Configuration ... @EnableCaching(mode=AdviceMode.ASPECTJ) public class JpaConfig {...} Then, specify a   CacheManager   provider in your configuration. @Override @Bean public CacheManager cacheManager() { return new ConcurrentMapCacheManager(); } @Override @Bean public KeyGenerator keyGenerator() { return new SimpleKeyGenerator(); } Code sample Spring provides some annotations for applying the cache in ...

Spring 4: Groovy DSL bean definition

Spring 4: Groovy DSL bean definition In Spring 2.x, script language is supported in Spring via the Java scripting engine. In Spring 4.0, Groovy is a first class citizen in Spring. Groovy can be used as an alternative for configuring bean definition. Groovy DSL bean definition As a Spring developer, you could be very familiar with XML based or/and Java annotation based bean definition. The following is an example of XML bean definition. <jdbc:embedded-database id="dataSource" type="H2"> </jdbc:embedded-database> <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory"> <property name="persistenceUnitName" value="persistenceUnit" /> <property name="dataSource" ref="dataSource" /> <property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence"></property> ...