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); }}
阅读全文
0 0
- spring4 + redis 零配置文件
- Spring4.X + Spring MVC + Mybatis3 零配置应用开发框架搭建详解(5) - Redis缓存配置
- Spring4.X + Spring MVC + Mybatis3 零配置应用开发框架搭建详解(5) - Redis缓存配置
- Spring4+hibernate4+struts2配置文件
- spring4的配置文件模板
- spring4配置文件详解
- spring4配置文件详解
- spring4配置文件详解
- Spring4 整合 Redis
- spring4.2整合hibernate5配置文件
- spring4.0 @PropertySource读取配置文件
- Spring4+MyBatis3整合配置文件详解
- spring4+redis+maven免配置
- spring4 注解方式集成redis
- Spring4 + Tiles3 web.xml零配置
- redis配置文件
- redis配置文件
- redis 配置文件
- 4. 编写函数,使字符串中尾部的*号不得多于n个
- Ubuntu用户ssh远程连接问题
- 【Java】一次Eclipse插件修改经历
- Windows and GPT FAQ
- return 在Java中作用
- spring4 + redis 零配置文件
- 调用windows打印机
- 准确率与召回率(Precision & Recall)、F-score
- c++的一些记不住的特性= =
- Halcon学习(五)手眼标定之眼在手外
- HTML初涉
- mysql添加表注释、字段注释、查看与修改注释
- RMQ问题 在线算法-ST算法
- hibernate创建数据库