Spring Boot 属性配置

  • 常规属性配置
  • 类型安全的属性配置

常规属性配置

在Spring-Boot中,我们在application.properties文件中定义属性,直接使用@Value注入即可。这个定义属性的配置文件在/src/main/resources/目录下。下面是一个例子:
首先我们在application.properties配置文件中增加属性:

book.author=yubuyun
book.name=spring boot

接下来修改入口类,获取上一步配置的属性。

package com.yun.app;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class AppApplication {

    @Value("${book.author}")
    private String bookAuthor;
    @Value("${book.name}")
    private String bookName;

    @RequestMapping("/")
    String index(){
        return "book name is:"+bookName+" and book author is:"+bookAuthor;
    }

    public static void main(String[] args) {
        SpringApplication.run(AppApplication.class, args);
    }
}

打开浏览器看结果显示为:
book name is:spring boot and book author is:yubuyun
由上述代码可见,通过@Value即可注入属性。

类型安全的属性配置

在之前的例子中每次引入属性都要使用@Value的形式声明,比较麻烦。Spring Boot还提供了基于类型安全的属性配置方式,通过@ConfigurationProperties将properties属性和一个Bean及其属性关联,从而实现类型安全的配置。下面是几个例子:
首先我们在application.properties配置文件中增加属性:

book.author=yubuyun
book.name=spring boot

接下来编写一个属性类,类的属性和配置文件中的属性一一对应。

package com.yun.app;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "book")
public class AuthorSettings {
    private String name;
    private String author;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }


}

@ConfigurationProperties(prefix = "book")这句声明表示这个类和配置文件的属性进行关联,它的属性注入了前缀为book的属性。这就要求属性名要一样才能相互匹配。
最后我们将这个类注入到入口类中进行测试:

package com.yun.app;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class AppApplication {

    @Autowired
    private AuthorSettings authorSettings;

    @RequestMapping("/")
    String index(){
        return "book name is:"+authorSettings.getName()+" and book author is:"+authorSettings.getAuthor();
    }

    public static void main(String[] args) {
        SpringApplication.run(AppApplication.class, args);
    }
}

页面打开后效果也是一样的:
book name is:spring boot and book author is:yubuyun

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值