1.配置文件类
package chengf.spring.boot.config.test;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Hello world!
*
*/
@ConfigurationProperties
public class AppBootConfig {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
2.业务代码引用
package chengf.spring.boot.config.test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@SpringBootApplication
@EnableConfigurationProperties(AppBootConfig.class)
public class SampleApplication implements CommandLineRunner {
@Autowired
private AppBootConfig appBootConfig;
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
System.out.println(appBootConfig.getName());
}
}
配置文件
name=chengf
age=20
执行结果
证明正确将数据注入到bean中了
- 大小: 13.7 KB
分享到:
相关推荐
在Spring Boot中注解@ConfigurationProperties有三种使用场景,而通常情况下我们使用的最多的只是其中的一种场景。本篇文章带大家了解一下三种场景的使用情况。 1.2 场景一 使用@ConfigurationProperties和@...
Spring Boot技术知识点:如何理解@ConfigurationProperties注解
@ConfigurationProperties注解使用方法(源代码) 目录 @ConfigurationProperties注解使用方法 前言 一、Spring配置方式 1.1 第一阶段:xml配置 1.2 第二阶段:注解配置 1.3 第三阶段:Java配置(java config) 二、@...
主要介绍了Spring Boot2.0 @ConfigurationProperties使用详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
在Spring Boot中测试@ConfigurationProperties 该项目显示了如何在Spring Boot中测试@ConfigurationProperties。 您可以在以下位置找到有关此内容的博客文章:
SpringBoot中,将类中的属性和配置文件中的配置在yml文件中进行绑定时出现以下的问题: 例如: person: lastName: hello age: 18 boss: false birth: 2020/02/20 maps: {k1: v1, k2: v2} ... org.spring
@ConfigurationProperties , @Controller , @CrossOrigin , @EnableWebMvc , @GetMapping , @Import , @JsonDeserialize , @JsonIgnore , @JsonIgnoreProperties , @JsonIgnoreType , @JsonInclude , @JsonProperty...
5 @ConfigurationProperties 四、 HTTP请求相关 1 @GetMapping 2 @PostMapping 3 @RequestMapping 五、 前后端传值相关 1 @PathVariable 2 @RequestParam 3 @RequestBody 4 @Responsebody 六、 全局异常处理相关 1 @...
有时候有这样子的情景,我们想把配置文件的信息,读取并自动封装成实体类,这样子,我们在代码里面使用就轻松方便多了,这时候,我们就可以使用@ConfigurationProperties,它可以把同类的配置信息自动封装成实体类 ...
介绍一下@PropertySource注解的用法,通过上一篇博客的知识,可以知道@Value和@ConfigurationProperties都可以用于获取配置文件的属性值,不过有个细节容易被忽略,那就是, 这两个注解在Springboot项目中都是获取...
spring-boot-properties 注解ConfigurationProperties的使用例子 博客相关 (1) (2) (3) (4) (5) 我的公众号(woshigcs) 有问题可关注我的公众号留言咨询
api-藻类食品 涵盖了Spring Rest专家课程(进行中)... 使用Spring概要文件,按概要文件配置application.properties,属性@Value和@ConfigurationProperties。 带有OpenAPI的JPA,Hibernate和Flyway域驱动设计(DDD)文
这是springboot配置的一部分示例,涉及@ConfigurationProperties和@value的使用,@PropertySource和@ImportResource的使用
Spring Boot @ConfigurationProperties示例
能够使用Spring Boot提供的@ConfigurationProperties ( spring.datasource.hikari.* , spring.datasource.dbcp2.* ) 通过部署属性decorator.datasource.enabled=true/false禁用装饰 通过spring属性applica
,使用@ConfigurationProperties注释的类时,将自动完成yaml文件中的配置属性 还支持简短形式搜索和内部深层元素搜索。 即, sp.d将向您显示spring.data , spring.datasource ,并且port还将显示server.port作为...
spring boot中文文档,从安装到部署。 I. Spring Boot文件 1.关于文档 2.获得帮助 3.第一步 4.使用Spring Boot 5.了解Spring Boot功能 6.转向生产 7.高级主题 II。入门 8.介绍Spring Boot ...
6. 引用application.properties中的属性的方式:@ConfigurationProperties(prefix = "spring.mail") + @Component + setter + getter 7. 引用其他自定义配置文件中的属性的方式: - @Component - ## 项目备注 1...
2.5 @ConfigurationProperties与@Value的区别 25 2.6 配置@PropertySource、@ImportResource、@Bean 27 2.7 配置文件占位符 30 2.8 Profile多环境支持 31 2.9 配置文件的加载位置 33 2.10 外部配置加载顺序 36 2.11 ...
Spring Boot Documentation 1. About the Documentation 2. Getting Help 3. First Steps 4. Working with Spring Boot 5. Learning about Spring Boot Features 6. Moving to Production 7. Advanced Topics II. ...