One of the spring-boot: simple getting started
1. Create a web project in IDE
two。 Add maven dependencies in pom.xml files
Org.springframework.boot
Spring-boot-starter-parent
1.5.9.RELEASE
Org.springframework.boot
Spring-boot-starter-web
3. Create the program entry class Application.java
Package com.xuejia.ad
Import org.springframework.boot.SpringApplication
Import org.springframework.boot.autoconfigure.SpringBootApplication
/ * *
* @ author kehaojian
* @ date 2017-12-28 8:33
* @ since 1.0.0
, /
@ SpringBootApplication
Public class Application {
Public static void main (String [] args) {
SpringApplication.run (Application.class, args)
}
}
4. Create a controller class
Package com.xuejia.ad.controller
Import org.springframework.web.bind.annotation.RequestMapping
Import org.springframework.web.bind.annotation.RestController
/ * *
* @ author kehaojian
* @ date 2017-12-28 8:35
* @ since 1.0.0
, /
@ RestController
Public class StartDemoController {
@ RequestMapping ("/")
String home () {
Return "hello"
}
}
5. Configure 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. Run the main method of Application
6. Enter http://localhost:8888/ in the browser
7. Add spring-boot-starter-redis and spring-boot-starter-test dependency packages to pom.xml
Org.springframework.boot
Spring-boot-starter-test
Org.springframework.boot
Spring-boot-starter-redis
8. Create the redis configuration class RedisConfig
Package com.xuejia.ad.config
Import com.fasterxml.jackson.annotation.JsonAutoDetect
Import com.fasterxml.jackson.annotation.PropertyAccessor
Import com.fasterxml.jackson.databind.ObjectMapper
Import org.slf4j.Logger
Import org.slf4j.LoggerFactory
Import org.springframework.boot.context.properties.ConfigurationProperties
Import org.springframework.context.annotation.Bean
Import org.springframework.context.annotation.Configuration
Import org.springframework.data.redis.connection.RedisConnectionFactory
Import org.springframework.data.redis.connection.jedis.JedisConnectionFactory
Import org.springframework.data.redis.core.RedisTemplate
Import org.springframework.data.redis.core.StringRedisTemplate
Import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer
Import redis.clients.jedis.JedisPoolConfig
/ * *
* @ author kehaojian
* @ date 2017-12-28 9:08
* @ since 1.0.0
, /
@ Configuration
@ ConfigurationProperties (prefix= "spring.redis")
Public class RedisConfig {
Private static Logger logger = LoggerFactory.getLogger (RedisConfig.class)
/ * @ Bean
Public RedisTemplate redisTemplate (RedisConnectionFactory redisConnectionFactory) {
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
Public JedisPoolConfig getRedisConfig () {
JedisPoolConfig config = new JedisPoolConfig ()
Return config
}
@ Bean
Public JedisConnectionFactory getConnectionFactory () {
JedisConnectionFactory factory = new JedisConnectionFactory ()
JedisPoolConfig config = getRedisConfig ()
Factory.setPoolConfig (config)
Logger.info ("JedisConnectionFactory bean init success.")
Return factory
}
@ Bean
Public RedisTemplate getRedisTemplate () {
RedisTemplate template = new StringRedisTemplate (getConnectionFactory ())
Return template
}
}
9. Create a redis simple utility class
Package com.xuejia.ad.common
Import org.springframework.beans.factory.annotation.Autowired
Import org.springframework.data.redis.core.RedisTemplate
Import org.springframework.stereotype.Repository
Import java.util.concurrent.TimeUnit
/ * *
* @ author kehaojian
* @ date 2017-12-28 9:04
* @ since 1.0.0
, /
@ Repository
Public class RedisUtil {
@ Autowired
Private RedisTemplate redisTemplate
Public void add (String key, String value, Long time) {
RedisTemplate.opsForValue () .set (key, value, time, TimeUnit.SECONDS)
}
Public String get (String key) {
Return redisTemplate.opsForValue () get (key)
}
Public void delete (String key) {
RedisTemplate.opsForValue (). GetOperations (). Delete (key)
}
}
10. Create a test class RedisTest in the test directory
Import com.xuejia.ad.common.RedisUtil
Import com.xuejia.ad.config.RedisConfig
Import org.junit.Before
Import org.junit.Test
Import org.junit.runner.RunWith
Import org.springframework.beans.factory.annotation.Autowired
Import org.springframework.test.context.ContextConfiguration
Import org.springframework.test.context.junit4.SpringJUnit4Cla***unner
/ * *
* @ author kehaojian
* @ date 2017-12-28 9:17
* @ since 1.0.0
, /
@ RunWith (SpringJUnit4Cla***unner.class)
@ ContextConfiguration (classes = {RedisConfig.class, RedisUtil.class})
Public class RedisTest {
@ Autowired
Private RedisUtil redisUtil
@ Before
Public void testbefore () {
RedisUtil.add ("kehaojian", "helloworld", 100L)
}
@ Test
Public void testRedis () throws InterruptedException {
Int total = 20
For (int I = 0; I