下列代码在项目启动时获取配置文件中的值
import javax.servlet.Filter; import java.util.*; @Configuration public class ShiroConfiguration { @Value("${spring.redis.host}") private String redisHost; @Value("${spring.redis.port}") private String redisPort; @Value("${spring.redis.password}") private String redisPassword; @Value("${spring.redis.database}") private Integer redisDatabase; @Value("${spring.redis.timeout}") private Integer redisTimeOut; @Bean public RedisManager redisManager(){ RedisManager redisManager = new RedisManager(); redisManager.setHost(redisConfig.getRedisHost() +":"+ redisConfig.getRedisPort()); redisManager.setPassword(redisPassword); redisManager.setDatabase(RedisDatabase); redisManager.setTimeout(RedisTimeOut); return redisManager; } }
改为以下方式
import com.dataqin.web.config.IgnoredUrlsProperties; import com.dataqin.web.config.RedisConfig; @Configuration public class ShiroConfiguration { @Bean public RedisManager redisManager(RedisConfig redisConfig){ RedisManager redisManager = new RedisManager(); redisManager.setHost(redisConfig.getRedisHost() +":"+ redisConfig.getRedisPort()); redisManager.setPassword(redisConfig.getRedisPassword()); redisManager.setDatabase(redisConfig.getRedisDatabase()); redisManager.setTimeout(redisConfig.getRedisTimeOut()); return redisManager; } }
package com.dataqin.web.config; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.Data; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration @Data public class RedisConfig{ @Value("${spring.redis.host}") private String redisHost; @Value("${spring.redis.port}") private String redisPort; @Value("${spring.redis.password}") private String redisPassword; @Value("${spring.redis.database}") private Integer redisDatabase; @Value("${spring.redis.timeout}") private Integer redisTimeOut; }