二、SpringBoot配置绑定的两种方式

简介: 代码如下

二、SpringBoot配置绑定的两种方式

演示文件

bean

public class Student {
    private String name;
    private Integer age;
    public Student(){}
    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

properties

2345_image_file_copy_53.jpg

方式一(@Component+@ConfigurationProperties)

将@Component和@ConfigurationProperties都标在Student类上,@ConfigurationProperties(prefix = “stu”)中的prefix = "stu"表示将从配置文件中前缀为stu的配置项开始找。

2345_image_file_copy_54.jpg

方式二(@ConfigurationProperties+@EnableConfigurationProperties)

@ConfigurationProperties标在Student上,@EnableConfigurationProperties标在配置类上,Student在配置类中造。

Student类

2345_image_file_copy_55.jpg

配置类

2345_image_file_copy_56.jpg

相关文章
|
4天前
|
缓存 Java 数据库
springboot数据库及缓存常用依赖及配置
springboot数据库及缓存常用依赖及配置
32 9
|
1天前
|
IDE Java Maven
SpringBoot自定义starter及自动配置
SpringBoot自定义starter及自动配置
|
5天前
|
存储 缓存 NoSQL
SpringBoot配置第三方专业缓存框架j2cache
SpringBoot配置第三方专业缓存框架j2cache
14 5
|
1天前
|
NoSQL Java Redis
【源码解析】自动配置的这些细节都不知道,别说你会 springboot
【源码解析】自动配置的这些细节都不知道,别说你会 springboot
|
5天前
|
存储 缓存 NoSQL
SpringBoot配置第三方专业缓存技术Redis
SpringBoot配置第三方专业缓存技术Redis
14 4
|
5天前
|
Java 应用服务中间件 Maven
浅谈后端整合Springboot框架后操作基础配置
浅谈后端整合Springboot框架后操作基础配置
11 3
|
9天前
|
消息中间件 Java Kafka
集成Kafka到Spring Boot项目中的步骤和配置
集成Kafka到Spring Boot项目中的步骤和配置
43 7
|
4天前
|
缓存 监控 NoSQL
SpringBoot配置第三方专业缓存技术jetcache方法缓存方案
SpringBoot配置第三方专业缓存技术jetcache方法缓存方案
15 1
|
4天前
|
存储 缓存 监控
SpringBoot配置第三方专业缓存技术Ehcache
SpringBoot配置第三方专业缓存技术Ehcache
13 1
|
9天前
|
druid Java 关系型数据库
在Spring Boot中集成Druid实现多数据源有两种常用的方式:使用Spring Boot的自动配置和手动配置。
在Spring Boot中集成Druid实现多数据源有两种常用的方式:使用Spring Boot的自动配置和手动配置。
75 5