前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >《Spring实战》摘录 - 21

《Spring实战》摘录 - 21

作者头像
用户1335799
发布2019-06-18 17:00:13
3770
发布2019-06-18 17:00:13
举报

201

问题: #12.1.4-1 | Neo4j是什么

回答: 一种很流行的图数据库。

202

问题: #12.3.1 | Spring Data Redis为四种Redis客户端实现提供了连接工厂

回答:

  • JedisConnectionFactory
  • JredisConnectionFactory
  • LettuceConnectionFactory
  • SrpConnectionFactory

203

问题: #12.3.3 | 当某个条目保存到Redis key-value存储的时候,key和value都会使用Redis的序列化器(serializer)进行序列化。Spring Data Redis提供了多个这样的序列化器,包括:

回答:

  • GenericToStringSerializer:使用Spring转换服务进行序列化;
  • JacksonJsonRedisSerializer:使用Jackson 1,将对象序列化为JSON;
  • Jackson2JsonRedisSerializer:使用Jackson 2,将对象序列化为JSON;
  • JdkSerializationRedisSerializer:使用Java序列化;
  • OxmSerializer:使用Spring O/X映射的编排器和解排器(marshaler和unmarshaler)实现序列化,用于XML序列化;
  • StringRedisSerializer:序列化String类型的key和value。

204

问题: #13.1-1 | 通过使用@EnableCaching启用注解驱动的缓存

回答:

package com.habuma.cachefun;import net.sf.ehcache.CacheManager;import org.springframework.cache.annotation.EnableCaching:import org.springframework.cache.concurrent.ConcurrentMapCacheManager; import org.springframework.context.annotation.Bean;import org.springframework.contextannotation.Configuration;
@Configuration@Enablecaching  //启用缓存public class CachingConfig(    @Bean    public CacheManager cacheManager()( //声明缓存管理器      return new ConcurrentMapCacheManager();    }}

205

问题: #13.1-2 | 通过使用启用注解驱动的缓存

回答:

<?xml version="1.0" encoding="UTF-8"><beans xmlns="http://www.spring.org/schema/beans"    xmins:xsi="http://www.w3.org/20/XMLSchema-instance"    xmins:cache="http://www.springframework.org/schema/cache"    xsi:schemaLocation="      http://www.springframework.org/schema/beans      http://www.springframework.org/sch/beans/spring-beansxsd      http://www.springframework.org/schema/cache      http://www.springframework.org/sc/cache/spring-cachexsd">
  <cache:annotation-driven/> //启用缓存
  <bean id="cacheManager" class=      "org.springframework.cache.concurrent.ConcurrentMapCacheMan" /> //声明缓存管理器  </beans></beans>

206

问题: #13.1.1-1 | Spring 3.1内置了五个缓存管理器实现,如下所示:

回答:

  • SimpleCacheManager
  • NoOpCacheManager
  • ConcurrentMapCacheManager
  • CompositeCacheManager
  • EhCacheCacheManager

207

问题: #13.1.1-2 | 以Java配置的方式设置EhCacheCacheManager

回答:

package com.habuma.cachefun;import net.sf.ehcache.CacheManager;import org.springframework.cache.annotation.Enablecaching:import org.springframework.cache.ehcache.EhCachecacheManager;import org.springframework.cache.ehcache.EhCacheManagerFactoryBean; import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.io.classPathResource;@Configuration@Enablecachingpublic class CachingConfig{ 
  @Bean  public EhCachecacheManager cacheManager(CacheManager cm){//配置EhCachecacheManager      return new EhCachecacheManage(cm);  }   @Bean  public EhCacheManagerFactoryBear ehcache(){//配置EhCacheManagerFactoryBear      EhCacheManagerFactoryBean ehcacheFactoryBean = new EhCacheManagerFactoryBean();      ehCacheFactoryBean.setConfigLocation(new ClassPathResource("com/habuma/spittr/cache/ehcache.xml"));      return ehCacheFactoryBean;  }}

208

问题: #13.2-1 | Spring提供了四个注解来声明缓存规则

回答:

  • @Cacheable --- 表明Spring在调用方法之前,首先应该在缓存中查找方法的返回值。如果这个值能够找到,就会返回缓存的值。否则的话,这个方法就会被调用,返回值会放到缓存之中
  • @CachePut --- 表明Spring应该将方法的返回值放到缓存中。在方法的调用前并不会检查缓存,方法始终都会被调用
  • @CacheEvict --- 表明Spring应该在缓存中清除一个或多个条目
  • @Caching --- 这是一个分组的注解,能够同时应用多个其他的缓存注解

209

问题: #13.2.1-1 | @Cacheable和@CachePut有一些共有的属性

回答:

  • value --- String[] --- 要使用的缓存名称
  • condition --- String --- SpEL表达式,如果得到的值是false的话,不会将缓存应用到方法调用上
  • key --- String --- SpEL表达式,用来计算自定义的缓存key
  • unless --- String --- SpEL表达式,如果得到的值是true的话,返回值不会放到缓存之中

210

问题: #13.2.2-2 | @CacheEvict注解的属性,指定了哪些缓存条目应该被移除掉

回答:

  • value --- String [] --- 要使用的缓存名称
  • key --- String --- SpEL表达式,用来计算自定义的缓存key
  • condition --- String --- SpEL表达式,如果得到的值是false的话,缓存不会应用到方法调用上
  • allEntries --- boolean --- 如果为true的话,特定缓存的所有条目都会被移除掉
  • beforeInvocation --- boolean --- 如果为true的话,在方法调用之前移除条目。如果为false(默认值)的话,在方法成功调用之后再移除条目
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-06-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 怀英的自我修炼 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
文件存储
文件存储(Cloud File Storage,CFS)为您提供安全可靠、可扩展的共享文件存储服务。文件存储可与腾讯云服务器、容器服务、批量计算等服务搭配使用,为多个计算节点提供容量和性能可弹性扩展的高性能共享存储。腾讯云文件存储的管理界面简单、易使用,可实现对现有应用的无缝集成;按实际用量付费,为您节约成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档