Springboot 之 配置

1.application.yml 配置文件
my:
name: forezp
age: 12
number: ${random.int}
uuid: ${random.uuid}
max: ${random.int(10)}
value: ${random.value}
greeting: hi,i'm ${my.name}

读取配置文件内容

@ConfigurationProperties(prefix = "my")  //读取配置文件去掉前缀,自动注入
@Component
@Data
public class ConfigBean {
    private String name;
    private int age;
    private int number;
    private String uuid;
    private int max;
    private String value;
    private String greeting;
}
读取yml 文件

@Value("${my.name}")
private String name;
@Value("${my.age}")
private int age;

@RequestMapping(value = "/miya")
public String miya(){
    return name+":"+age;
}

2.配置文件读取properties 文件

test.properties 文件
com.forezp.name=forezp
com.forezp.age=12

@Configuration
@PropertySource(value = "classpath:test.properties")
@ConfigurationProperties(prefix = "com.forezp")
@Data
public class User {
    private String name;
    private String age;

}

@PropertySource 注解需要引入

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>
Controller 层使用配置文件类
 
@RestController
@EnableConfigurationProperties({ConfigBean.class, User.class})
public class LucyController {
    @Autowired
    ConfigBean configBean;

    @RequestMapping(value = "/lucy")
    public String miya(){
        return configBean.getGreeting()+">>>>>"+configBean.getName()+">>>>"+configBean.getUuid()+">>>>"+configBean.getMax();
    }
    @Autowired
    User user;

    @RequestMapping(value = "/user")
    public String user(){
        return user.getName()+user.getAge();
    }
}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值