1

springboot 常量设置

 2 years ago
source link: https://segmentfault.com/a/1190000040759082
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

springboot 常量设置

发布于 今天 14:32

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;
}
  @Autowired
    private Content content;

使用
String basicURL=content.getBasicUrl();

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK