关于后端:springboot-常量设置

1.在application.yml增加常量

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…

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理