springboot 常量设置

1.在application.yml添加常量

image.png

content:
basic-url: http://localhost:8088/

2.在可扫描包的路径下添加关于常量的实体

package com.baomidou.ant.demo.entity;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Data
//注册到Spring容器
@Component
//指定常量资源路径

@ConfigurationProperties("content")
public class Content {
    //创建相应属性
    //直接使用注解获取常量值
    private String basicUrl;
}

3.使用

声明

  @Autowired
    private Content content;

使用
String basicURL=content.getBasicUrl();

注意事项:使用时可能会出现Could not autowire,解决办法如https://segmentfault.com/a/11...

评论

暂无评论