1.在IDE中創(chuàng)建一個web項(xiàng)目
東風(fēng)網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián)公司,東風(fēng)網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為東風(fēng)近千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)公司要多少錢,請找那個售后服務(wù)好的東風(fēng)做網(wǎng)站的公司定做!
2.在pom.xml文件中增加maven依賴
3.創(chuàng)建程序入口類Application.java
packagecom.xuejia.ad;
importorg.springframework.boot.SpringApplication;
importorg.springframework.boot.autoconfigure.SpringBootApplication;
/**
*@authorkehaojian
*@date2017-12-28 8:33
*@since1.0.0
*/
@SpringBootApplication
public classApplication {
public static voidmain(String[] args) {
SpringApplication.run(Application.class, args);
}
}
4.創(chuàng)建一個controller類
packagecom.xuejia.ad.controller;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RestController;
/**
*@authorkehaojian
*@date2017-12-28 8:35
*@since1.0.0
*/
@RestController
public classStartDemoController {
@RequestMapping("/")
String home(){
return"hello";
}
}
5.配置application.yml
server:
port:8888
tomcat:
uri-encoding:utf-8
spring:
redis:
host:localhost
port:6379
pool:
max-active:8
min-idle:0
max-idle:8
max-wait:-1
5.運(yùn)行Application的main方法
6.在瀏覽器中輸入http://localhost:8888/
7.在pom.xml增加spring-boot-starter-redis以及spring-boot-starter-test依賴包
8.創(chuàng)建redis配置類RedisConfig
packagecom.xuejia.ad.config;
importcom.fasterxml.jackson.annotation.JsonAutoDetect;
importcom.fasterxml.jackson.annotation.PropertyAccessor;
importcom.fasterxml.jackson.databind.ObjectMapper;
importorg.slf4j.Logger;
importorg.slf4j.LoggerFactory;
importorg.springframework.boot.context.properties.ConfigurationProperties;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.data.redis.connection.RedisConnectionFactory;
importorg.springframework.data.redis.connection.jedis.JedisConnectionFactory;
importorg.springframework.data.redis.core.RedisTemplate;
importorg.springframework.data.redis.core.StringRedisTemplate;
importorg.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
importredis.clients.jedis.JedisPoolConfig;
/**
*@authorkehaojian
*@date2017-12-28 9:08
*@since1.0.0
*/
@Configuration
@ConfigurationProperties(prefix="spring.redis")
public classRedisConfig {
private staticLoggerlogger= LoggerFactory.getLogger(RedisConfig.class);
/*@Bean
public RedisTemplate
StringRedisTemplate template = new StringRedisTemplate(redisConnectionFactory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
*/
@Bean
publicJedisPoolConfig getRedisConfig(){
JedisPoolConfig config =newJedisPoolConfig();
returnconfig;
}
@Bean
publicJedisConnectionFactory getConnectionFactory(){
JedisConnectionFactory factory =newJedisConnectionFactory();
JedisPoolConfig config = getRedisConfig();
factory.setPoolConfig(config);
logger.info("JedisConnectionFactory bean init success.");
returnfactory;
}
@Bean
publicRedisTemplate, ?> getRedisTemplate(){
RedisTemplate,?> template =newStringRedisTemplate(getConnectionFactory());
returntemplate;
}
}
9.創(chuàng)建redis簡單工具類
packagecom.xuejia.ad.common;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.data.redis.core.RedisTemplate;
importorg.springframework.stereotype.Repository;
importjava.util.concurrent.TimeUnit;
/**
*@authorkehaojian
*@date2017-12-28 9:04
*@since1.0.0
*/
@Repository
public classRedisUtil {
@Autowired
privateRedisTemplate
public voidadd(String key, String value, Long time){
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
}
publicString get(String key) {
returnredisTemplate.opsForValue().get(key);
}
public voiddelete(String key) {
redisTemplate.opsForValue().getOperations().delete(key);
}
}
10.在test目錄下創(chuàng)建一個測試類RedisTest
importcom.xuejia.ad.common.RedisUtil;
importcom.xuejia.ad.config.RedisConfig;
importorg.junit.Before;
importorg.junit.Test;
importorg.junit.runner.RunWith;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.test.context.ContextConfiguration;
importorg.springframework.test.context.junit4.SpringJUnit4Cla***unner;
/**
*@authorkehaojian
*@date2017-12-28 9:17
*@since1.0.0
*/
@RunWith(SpringJUnit4Cla***unner.class)
@ContextConfiguration(classes = {RedisConfig.class, RedisUtil.class})
public classRedisTest {
@Autowired
privateRedisUtilredisUtil;
@Before
public voidtestbefore() {
redisUtil.add("kehaojian","helloworld",100L);
}
@Test
public voidtestRedis()throwsInterruptedException {
inttotal =20;
for(inti =0; i <20; i++){
System.out.println(redisUtil.get("kehaojian"));
Thread.sleep(5000);
}
}
}
11.運(yùn)行測試類,結(jié)果如下
18:57:26.975 [main] DEBUG org.springframework.data.redis.core.RedisConnectionUtils - Opening RedisConnection
18:57:26.975 [main] DEBUG org.springframework.data.redis.core.RedisConnectionUtils - Closing Redis Connection
helloworld
18:57:31.976 [main] DEBUG org.springframework.data.redis.core.RedisConnectionUtils - Opening RedisConnection
18:57:31.976 [main] DEBUG org.springframework.data.redis.core.RedisConnectionUtils - Closing Redis Connection
helloworld
18:57:36.976 [main] DEBUG org.springframework.data.redis.core.RedisConnectionUtils - Opening RedisConnection
18:57:36.976 [main] DEBUG org.springframework.data.redis.core.RedisConnectionUtils - Closing Redis Connection
helloworld
18:57:41.976 [main] DEBUG org.springframework.data.redis.core.RedisConnectionUtils - Opening RedisConnection
18:57:41.976 [main] DEBUG org.springframework.data.redis.core.RedisConnectionUtils - Closing Redis Connection
12.打包,在pom.xml增加打包插件配置
13.執(zhí)行package操作
網(wǎng)站標(biāo)題:spring-boot之一:簡單入門
文章源于:http://www.ef60e0e.cn/article/jeseod.html