Spring中的@ConfigurationProperties详解

 更新时间:2023年09月25日 09:52:52   作者:骑个小蜗牛  
这篇文章主要介绍了Spring中的@ConfigurationProperties详解,ConfigurationProperties注解主要用于将外部配置文件配置的属性填充到这个Spring Bean实例中,需要的朋友可以参考下

@ConfigurationProperties 

ConfigurationProperties注解主要用于将外部配置文件配置的属性填充到这个** Spring Bean实例 **中。

需要注意:它自己单独使用无效,需要配合其它注解一起使用。且对于Spring Bean才生效,普通的new 对象不生效。

ConfigurationProperties的使用方式:

  1. @ConfigurationProperties + @Component(或其它实例化Bean的注解)注解到bean定义类上
  2. @ConfigurationProperties + @Bean注解到配置类的bean定义方法上
  3. @ConfigurationProperties注解到普通类,然后通过@EnableConfigurationProperties定义为bean

使用方式

配置文件:

sftp:
  host: 127.0.0.1
  port: 22
  username: admin
  password: 123456

1. @ConfigurationProperties + @Component

@ConfigurationProperties + @Component(或其它实例化Bean的注解)注解到bean定义类上

@Data
@Component
// @Configuration
@ConfigurationProperties(prefix = "sftp")
public class Sftp {
    private String host;
    private String port;
    private String username;
    private String password;
}

2. @ConfigurationProperties + @Bean

@ConfigurationProperties + @Bean注解到配置类的bean定义方法上

@Data
public class Sftp {
    private String host;
    private String port;
    private String username;
    private String password;
}
@Configuration
public class BeanConfig {
    @Bean
    @ConfigurationProperties(prefix = "sftp")
    public Sftp sftp() {
        return new Sftp();
    }
}

此种用法遇到的坑:重复使用@ConfigurationProperties 如果将代码改成这样:

@Data
@ConfigurationProperties(prefix = "sftp")
public class Sftp {
    private String host;
    private String port;
    private String username;
    private String password;
}
@Configuration
public class BeanConfig {
    @Bean
    @ConfigurationProperties(prefix = "aaaa")
    public Sftp sftp() {
        return new Sftp();
    }
}

这种方式上有@ConfigurationProperties注解,且类上也有@ConfigurationProperties注解的,只有方法上的@ConfigurationProperties会生效(可以理解为类上的注解方法的给重写了)。

3. @ConfigurationProperties + @EnableConfigurationProperties

@ConfigurationProperties注解到普通类,然后通过@EnableConfigurationProperties定义为bean

@Data
@ConfigurationProperties(prefix = "sftp")
public class Sftp {
    private String host;
    private String port;
    private String username;
    private String password;
}
@SpringBootApplication
@EnableConfigurationProperties(value = {Sftp.class})
public class SpringbootApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplication.class, args);
    }
}

到此这篇关于Spring中的@ConfigurationProperties详解的文章就介绍到这了,更多相关@ConfigurationProperties详解内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java访问控制符原理及具体用法解析

    Java访问控制符原理及具体用法解析

    这篇文章主要介绍了Java访问控制符原理及具体用法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-11-11
  • eclipse中自动生成构造函数的两种方法

    eclipse中自动生成构造函数的两种方法

    下面小编就为大家带来一篇eclipse中自动生成构造函数的两种方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-10-10
  • Spring Boot 添加MySQL数据库及JPA实例

    Spring Boot 添加MySQL数据库及JPA实例

    本篇文章主要介绍了Spring Boot 添加MySQL数据库及JPA,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-03-03
  • 详解springboot项目带Tomcat和不带Tomcat的两种打包方式

    详解springboot项目带Tomcat和不带Tomcat的两种打包方式

    这篇文章主要介绍了详解springboot项目带Tomcat和不带Tomcat的两种打包方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • 详解Java关键字final

    详解Java关键字final

    今天带大家学习Java基础知识,文中对关键字final作了非常详细的介绍,对正在学习Java的小伙伴们很有帮助,需要的朋友可以参考下
    2021-05-05
  • BUUCTF-easy java WEB-INF/web.xml泄露漏洞及其利用方式

    BUUCTF-easy java WEB-INF/web.xml泄露漏洞及其利用方式

    这篇文章主要介绍了BUUCTF-easy java WEB-INF/web.xml泄露漏洞及其利用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-07-07
  • Mybatis-Plus select不列出全部字段的方法

    Mybatis-Plus select不列出全部字段的方法

    这篇文章主要介绍了Mybatis-Plus select不列出全部字段的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • 详解使用Java代码读取并比较本地两个txt文件区别

    详解使用Java代码读取并比较本地两个txt文件区别

    这篇文章主要为大家介绍了使用Java代码读取并比较本地两个txt文件区别详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-07-07
  • Spring @Scheduled的坑及解决

    Spring @Scheduled的坑及解决

    这篇文章主要介绍了Spring @Scheduled的坑及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-08-08
  • springboot实现发送QQ邮箱

    springboot实现发送QQ邮箱

    这篇文章主要为大家详细介绍了springboot实现发送QQ邮箱,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-06-06

最新评论