spring4 + redis 零配置文件

来源:互联网 发布:linux中ping命令 编辑:程序博客网 时间:2024/06/06 03:00

非常不喜欢pring中各种各样的xml配置文件,更加倾向于springboot 中无配置文件,不过好在现在spring也支持无配置文件实现各种功能了,只需要通过各种注解实现:
接下来看看spring + redis 无配置文件的实现方法:
1.依赖:

<dependency>      <groupId>org.springframework.data</groupId>      <artifactId>spring-data-redis</artifactId>      <version>1.5.2.RELEASE</version>    </dependency>    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-context</artifactId>      <version>4.3.7.RELEASE</version>    </dependency>    <dependency>      <groupId>redis.clients</groupId>      <artifactId>jedis</artifactId>      <version>2.7.0</version>    </dependency>    <dependency>      <groupId>commons-logging</groupId>      <artifactId>commons-logging</artifactId>      <version>1.1.1</version>    </dependency>

2.配置类:

package com.mjlf.MVC.config;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.PropertySource;import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.GenericToStringSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer;@Configuration@PropertySource("classpath:redis.properties")public class RedisConfig {    private @Value("${redis.host}") String redisHost;    private @Value("${redis.port}") int redisPort;    private @Value("${redis.pass}") String redisPass;    @Bean    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {        return new PropertySourcesPlaceholderConfigurer();    }    @Bean    JedisConnectionFactory jedisConnectionFactory() {        JedisConnectionFactory factory = new JedisConnectionFactory();        factory.setHostName(redisHost);        factory.setPort(redisPort);        factory.setPassword(redisPass);        factory.setUsePool(true);        return factory;    }    @Bean    RedisTemplate< String, Object > redisTemplate() {        final RedisTemplate< String, Object > template =  new RedisTemplate< String, Object >();        template.setConnectionFactory( jedisConnectionFactory() );        template.setKeySerializer( new StringRedisSerializer() );        template.setHashValueSerializer( new GenericToStringSerializer< Object >( Object.class ) );        template.setValueSerializer( new GenericToStringSerializer< Object >( Object.class ) );        return template;    }}

3.server类:

package com.mjlf.MVC.server;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.stereotype.Service;import java.util.concurrent.TimeUnit;@Servicepublic class RedisService {    @Autowired    private RedisTemplate< String, Object > template;    public Object getValue(final String key) {        return template.opsForValue().get(key);    }    public void setValue(final String key, final String value) {        template.opsForValue().set(key, value);        // set a expire for a message        template.expire(key, 5, TimeUnit.SECONDS);    }}
原创粉丝点击