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赋值

相关文章:

  • 2021-11-10
  • 2022-02-08
  • 2021-08-22
  • 2022-12-23
  • 2022-12-23
  • 2021-12-14
  • 2022-12-23
猜你喜欢
  • 2021-10-12
  • 2022-12-23
  • 2021-06-17
  • 2021-08-28
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案