application.yml实例:
spring:
redis:
database: 0
host: 127.0.0.1
复制代码
获取方法:
/**
* @Auther:WangZiBin
* @Description:
* @Modified By:
*/
@Configuration
public class JedisConfig{
private Logger jedisConfigLogger= LoggerFactory.getLogger(JedisConfig.class);
@Value("${spring.redis.host:#{null}}")
private String host;
@Value("${spring.redis.port:#{null}}")
private Integer port;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
}
复制代码
注意@Configuration注解是必须的,@Component同样适用
@Value("${spring.redis.port:#{null}}")
复制代码
其中
:#{null}
复制代码
作用为在取不到对应配置值时,采用默认值null赋值
我是 dying 搁浅 ,我始终期待与你的相遇。无论你是否期待,潮涨潮落,我仅且就在这里…
我们下期再见~
近期评论