一、Spring集成第三方框架
1.1 XML
我看下如果使用Spring的XML方式集成redis的话,我们会这么集成:
<!-- 配置JedisPoolConfig--> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="50"/> <property name="maxTotal" value="100"/> <property name="maxWaitMillis" value="20000"/> </bean> <!--配置JedisConnectionFactory--> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="localhost"/> <property name="port" value="6379"/> <property name="poolConfig" ref="jedisPoolConfig"/> </bean> <!--使用字符串进行序列化--> <bean id="stringReadisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" /> <!--使用JDK的序列化器进行转化--> <bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> <!--配置Spring RedisTemplate--> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="connectionFactory"/> <property name="keySerializer" ref="stringReadisSerializer"/> <property name="valueSerializer" ref="stringReadisSerializer"/> </bean>
这个配置想必大家都很熟了吧。
1.2 Java注解版本
上面是通过是通过配置文件XML的方式,我们使用java的注解注入呐?
@Bean public JedisPoolConfig jedisPoolConfig() { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxTotal(100); jedisPoolConfig.setMaxIdle(50); return jedisPoolConfig; } @Bean public JedisConnectionFactory jedisConnectionFactory() { JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(); jedisConnectionFactory.setHostName("localhost"); jedisConnectionFactory.setPort("6379"); jedisConnectionFactory.setPoolConfig(jedisPoolConfig()); } @Bean public RedisTemplate redisTemplate() { RedisTemplate redisTemplate = new RedisTemplate(); redisTemplate.setConnectionFactory(jedisConnectionFactory)()); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new StringRedisSerializer()); return redisTemplate; }
@Bean注解的方式,就是对应的配置文件的<bean>了,这个应该很好理解。
二、Spring理解Spring Boot
在Spring Boot中,我们好像不需要进行配置了。其实并非不用配置,而是spring boot帮我们进行了自动配置呢,对应的就是RedisAutoConfiguration:
@Configuration(proxyBeanMethods = false)@ConditionalOnClass(RedisOperations.class)@EnableConfigurationProperties(RedisProperties.class)@Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })public class RedisAutoConfiguration {
@Bean @ConditionalOnMissingBean(name = "redisTemplate") public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { RedisTemplate<Object, Object> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); return template; }
@Bean @ConditionalOnMissingBean public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { StringRedisTemplate template = new StringRedisTemplate(); template.setConnectionFactory(redisConnectionFactory); return template; }
}
Spring Boot在启动的时候,会加载此类,初始化我们的redisTemplate。
这就是Spring Boot的一点核心特性:就是自动装配。