<div>
<h2><b>1)传统ssm整合redis的时候 需要在xml的配置文件中 进行大量的配置Bean</b></h2>
<p> 我们在这里使用springboot来代替ssm的整合,只是通过xml的形式来整合redis</p>
<p><b>第一步:加入配置</b></p>
<div class="blockcode">
<pre class="blockcode"><code><dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency></code></pre>
</div>
<p><b>第二步: 配置xml的bean的配置</b></p>
<div class="blockcode">
<pre class="blockcode"><code> //配置连接池
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="minIdle" value="10"></property>
<property name="maxTotal" value="20"></property>
</bean>
//配置连接工厂
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="47.104.128.12"></property>
<property name="password" value="123456"></property>
<property name="database" value="0"></property>
<property name="poolConfig" ref="poolConfig"></property>
</bean>
//配置 redisTemplate 模版类
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"/>
<!--如果不配置Serializer,那么存储的时候缺省使用String,如果用User类型存储,那么会提示错误User can't cast to String!! -->
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
</property>
</bean></code></pre>
</div>
<p><b>第三步:导入配置</b></p>
<p><b>@ImportResource(locations = "classpath:beans.xml") 可以导入xml的配置文件</b></p>
<div class="blockcode">
<pre class="blockcode"><code>@SpringBootApplication
@ImportResource(locations = "classpath:beans.xml")
@RestController
public class TulingOpenAutoconfigPrincipleApplication {
@Autowired
private RedisTemplate redisTemplate;
public static void main(String[] args) {
SpringApplication.run(TulingOpenAutoconfigPrincipleApplication.class, args);
}
@RequestMapping("/testRedis")
public String testRedis() {
redisTemplate.opsForValue().set("smlz","smlz");
return "OK";
}
}</code></pre>
</div>
<p><b>2)综上所述 我们发现,若整合redis的时候通过传统的整合,进行了大量的配置,那么我们来看下通过springboot自动装配整合的对比</b></p>
<p><b>导入依赖:</b></p>
<div class="blockcode">
<pre class="blockcode"><code> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency></code></pre>
</div>
<p><b>修改yml配置文件</b></p>
<div class="blockcode">
<pre class="blockcode"><code>spring.redis.host=47.104.128.12
spring.redis.port=6379
spring.redis.password=123456</code></pre>
</div>
<p><b>直接使用(下述代码可以不要配置,为了解决保存使用jdk的序列方式才配置的)</b></p>
<div class="blockcode">
<pre class="blockcode"><code> @Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setDefaultSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));
template.setConnectionFactory(redisConnectionFactory);
return template;
}</code></pre>
</div>
<p><b>3)传统整合和springboot自动装配 优劣势分析。。。。。。。。。。。。</b></p>
<p><b>4)自动装配原理前的不得不说的几个注解</b></p>
<p><b>4.1)通过@Import注解来导入ImportSelector组件</b></p>
<p><b>①:写一个配置类在配置类上标注一个@Import的注解,</b></p>
<div class="blockcode">
<pre class="blockcode"><code>@Configuration
@Import(value = {Tuling |
|