spring-boot 和 redis 集成的一个小例子

转自:http://blog.csdn.net/a67474506/article/details/52595053

在网上看到好多的spring-boot和redis集成的,弄到本地一直报Error resolving template "get", template might not exist or might not be accessible by any of the configured Template Resolvers错误,大概是因为模板什么的没有找到,网上找了好多方法都是不奏效。

然后找其他的方式,找到一个亲测能用的方法。

项目的结构:

spring-boot 和 redis 集成的一个小例子_第1张图片


pom.xml

  
    4.0.0  
    自己命名  
    自己命名  
    0.0.1-SNAPSHOT  
  
      
        UTF-8  
        1.3.5.RELEASE  
      
      
          
            org.springframework.boot  
            spring-boot-starter-web  
            ${boot.version}  
          
          
            org.springframework.boot  
            spring-boot-starter-test  
            ${boot.version}  
            test  
          
          
            redis.clients  
            jedis  
            2.8.2  
          
      
 

application.yml

jedis :  
  pool :  
    host : 127.0.0.1  
    port : 6379  
    password : 自己的密码
    timeout : 60000
    config : 
      maxTotal: 100  
      maxIdle: 10  
      maxWaitMillis : 100000  
  
server :  
  port : 8082  

app.java

package com.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.first.domain.RedisClient;  
  
/** 
 * 是Spring Boot项目的核心注解,主要是开启自动配置 
 */  
@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan  
@RestController  
public class App {  
      
    @Autowired  
    private RedisClient redisClinet;  
      
    public static void main(String[] args) {  
        SpringApplication.run(App.class, args);  
    }  
      
    @RequestMapping("/set")  
    public String set(String key, String value) throws Exception{  
        redisClinet.set(key, value);  
        return "success";  
    }  
      
    @RequestMapping("/get")  
    public String get(String key) throws Exception {  
        return redisClinet.get(key);  
    }  
      
}  

redisClient.java

package com.example.first.domain;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;


@Component
public class RedisClient {

	@Autowired
	private JedisPool jedisPool;

	public void set(String key, String value) throws Exception {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.set(key, value);
		} finally {
			// 返还到连接池
			jedis.close();
		}
	}

	public String get(String key) throws Exception {

		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			return jedis.get(key);
		} finally {
			// 返还到连接池
			jedis.close();
		}
	}

}


redisConfiguration.java

package com.example.first.domain;
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.beans.factory.annotation.Qualifier;  
import org.springframework.beans.factory.annotation.Value;  
import org.springframework.context.annotation.Bean;  
import org.springframework.context.annotation.Configuration;  
  
import redis.clients.jedis.JedisPool;  
import redis.clients.jedis.JedisPoolConfig;  
  
@Configuration  
public class RedisConfiguration {  
      
    @Bean(name= "jedis.pool")  //加载的applicant.yml中的参数
    @Autowired  
    public JedisPool jedisPool(@Qualifier("jedis.pool.config") JedisPoolConfig config,   
                @Value("${jedis.pool.host}")String host,   
                @Value("${jedis.pool.port}")int port,
                @Value("${jedis.pool.timeout}") int timeout,
                @Value("${jedis.pool.password}") String password) {  
        return new JedisPool(config, host, port,timeout,password);  
    }  
      
    @Bean(name= "jedis.pool.config")  
    public JedisPoolConfig jedisPoolConfig (@Value("${jedis.pool.config.maxTotal}")int maxTotal,  
                                @Value("${jedis.pool.config.maxIdle}")int maxIdle,  
                                @Value("${jedis.pool.config.maxWaitMillis}")int maxWaitMillis) {  
        JedisPoolConfig config = new JedisPoolConfig();  
        config.setMaxTotal(maxTotal);  
        config.setMaxIdle(maxIdle);  
        config.setMaxWaitMillis(maxWaitMillis);  
        return config;  
    }  
      
}  

要说一下的是,如果redis设置了密码启动的时候要用 redis-server.exe  redis.window.conf 启动,用来启用密码。









你可能感兴趣的:(spring-boot 和 redis 集成的一个小例子)