Springboot加载配置的几种方法

获取配置信息我们使用到的注解:@Value、@ConfigurationProperties、@PropertySource

项目结构:

application.properties默认配置文件

application.properties、application.yml是springboot的默认配置文件,springboot会从下面两个路径默认加载配置文件

src/main/resources

src/main/resources/config

在springboot项目中我们通常会添加许多springcloud全家桶的组件,比如zuul、eureka、feign等,我们都可以在application.properties中修改默认配置

以下是application.properties配置信息:

#服务端口号
server.port=8762
#服务注册地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
#是否开启熔断策略
feign.hystrix.enabled=true

#自定义配置
contextKey=it's value
user.username=jack
user.age=18

在上面配置中我们自定义了三个key/value信息,下面我们主要使用@value、@ConfigurationProperties两个注解来获取配置信息

@Value注解获取配置信息

@RestController
@RequestMapping("hello")
public class Hello {
    @Value("${contextKey}")
    private String contextKey;

    @GetMapping("hello")
    public String hello(){
        return contextKey;
    }
}

在上面我们做了一个简单的demo,浏览器中通过访问  http://localhost:8762/hello/hello 可以得到返回信息:it's value

@Value的使用很简单,在日常中也是我们使用频次最多的,但当项目中有一个对象它有很多个属性值要配置,如果使用@Value会在java类中定义许多个对象接收,不同类中都需要这个对象的参数,则每个类中都要声明,这样做实在是太繁琐!那我们有没有一种方法可以用对象做到一次接收,在每个类中引用该对象呢?当然有:@ConfigurationProperties

@ConfigurationProperties获取配置信息

上述配置中我们定义了一个对象user,它有两个属性值:username、age,我们可以使用上面的@Value方式获取他们,接下来我们看@ConfigurationProperties有什么不同之处:

@Component
@ConfigurationProperties(prefix = "user")
public class UserConfig {
    private String username;
    private int age;

    getter...

    setter...
}

​​​

@RestController
@RequestMapping("hello")
public class Hello {
    @Resource
    private UserConfig userConfig;

    @GetMapping("hello")
    public String hello(){
        return JSON.toJSONString(userConfig);
    }
}

做完这些,我们在通过浏览器访问 http://localhost:8762/hello/hello 返回结果:{"age":18,"username":"jack"}

项目启动时系统通过@ConfigurationProperties把自定义对象user的各个属性值赋值给UserConfig,当我们类中需要这个对象,通过直接引用@Resource  UserConfig就可以了,因此也消除了重复开发。

以上获取配置信息我们都是在application.properties中实现的,有些时候我们都会自定义一些业务配置文件与系统配置文件做隔离,这样我们又要怎么做呢?下面来看下@PropertySource注解

@PropertySource注解获取自定义配置文件

根据上面的项目结构可以看到,我们在resources目录下创建了config文件夹来存放我们自定义的配置文件:account.properties、service.properties,这里我们以service.properties配置文件为例:


student.name=jack
student.age=18
student.clazz=3
student.addr=shanghai

teacher.name=vick
teacher.age=35
@Component
@PropertySource("classpath:config/service.properties")
@ConfigurationProperties(prefix = "student")
public class Student {

    private String name;
    private int age;
    private String clazz;
    private String addr;

    getter...

    setter...
}
@RestController
@RequestMapping("hello")
public class Hello {
    @Resource
    private UserConfig userConfig;
    @Resource
    private Student student;

    @GetMapping("hello")
    public String hello(){
        return JSON.toJSONString(userConfig);
    }

    @GetMapping("showStudent")
    public String showStudent(){
        return JSON.toJSONString(student);
    }
}

在上面service.properties配置文件中我们定义了两个对象student、teacher,在Student类中我们使用了@ConfigurationProperties、@PropertySource注解来加载制定前缀的对象值,在Hello类中我们调用showStudent方法来展示是否获取到了对象值:http://localhost:8762/hello/showStudent :{"addr":"shanghai","age":18,"clazz":"3","name":"jack"}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值