zoukankan      html  css  js  c++  java
  • spring boot@ConfigurationProperties

    有时候有这样子的情景,我们想把配置文件的信息,读取并自动封装成实体类,这样子,我们在代码里面使用就轻松方便多了,这时候,我们就可以使用@ConfigurationProperties,它可以把同类的配置信息自动封装成实体类

    首先在配置文件里面,这些信息是这样子滴

    connection.username=admin
    connection.password=kyjufskifas2jsfs
    connection.remoteAddress=192.168.1.1

    这时候我们可以定义一个实体类在装载配置文件信息

    @Component
    @ConfigurationProperties(prefix="connection")
    public class ConnectionSettings {
    
        private String username;
        private String remoteAddress;
        private String password ;
    
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public String getRemoteAddress() {
            return remoteAddress;
        }
        public void setRemoteAddress(String remoteAddress) {
            this.remoteAddress = remoteAddress;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
    
    }

    我们还可以把@ConfigurationProperties还可以直接定义在@bean的注解上,这是bean实体类就不用@Component和@ConfigurationProperties了

    @SpringBootApplication
    public class DemoApplication{
    
        //...
    
        @Bean
        @ConfigurationProperties(prefix = "connection")
        public ConnectionSettings connectionSettings(){
            return new ConnectionSettings();
    
        }
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }

    然后我们需要使用的时候就直接这样子注入

    @RestController
    @RequestMapping("/task")
    public class TaskController {
    
    @Autowired ConnectionSettings conn;
    
    @RequestMapping(value = {"/",""})
    public String hellTask(){
        String userName = conn.getUsername();     
        return "hello task !!";
    }
    
    }

    如果发现@ConfigurationPropertie不生效,有可能是项目的目录结构问题,

    你可以通过@EnableConfigurationProperties(ConnectionSettings.class)来明确指定需要用哪个实体类来装载配置信息

    @Configuration
    @EnableConfigurationProperties(ConnectionSettings.class)
     public class MailConfiguration { 
        @Autowired private MailProperties mailProperties; 
    
        @Bean public JavaMailSender javaMailSender() {
          // omitted for readability
        }
     }





    @ConfigurationProperties注解主要用来把properties配置文件转化为bean来使用的,而@EnableConfigurationProperties注解的作用是@ConfigurationProperties注解生效。如果只配置@ConfigurationProperties注解,在IOC容器中是获取不到properties配置文件转化的bean的。使用如下:

    1、spring boot启动时默认是加载application.properties配置文件的,假设该配置文件中内容为:

    local.host=127.0.0.1
    local.port=8080

    2、创建一个类ComponentProperties,把配置文件转化为bean来使用。@ConfigurationProperties注解可以把properties文件转化为bean,然后使用@Component注解把该bean注入到IOC容器中。

    package com.example.demo;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    
    @Component
    /*prefix定义配置文件中属性*/
    @ConfigurationProperties(prefix="local")
    public class ComponentProperties {
    
        /*host和port属性必须保持与application.properties中的属性一致*/
        private String host;
        private String port;
    
        public void setHost(String host) {
            this.host = host;
        }
        public void setPort(String port) {
            this.port = port;
        }
    
        @Override
        public String toString() {
            return "ComponentProperties [host=" + host + ", port=" + port + "]";
        }
    
    }

    3、用@EnableConfigurationProperties注解使@ConfigurationProperties生效,并从IOC容器中获取bean。

    package com.example.demo;
    import org.springframework.boot.SpringApplication;
    import 
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.context.ConfigurableApplicationContext;
    import org.springframework.context.annotation.ComponentScan;
    
    //@SpringBootApplication
    @ComponentScan
    /*@EnableConfigurationProperties注解是用来开启对@ConfigurationProperties注解配置Bean的支持。
    也就是@EnableConfigurationProperties注解告诉Spring Boot 使能支持@ConfigurationProperties*/
    @EnableConfigurationProperties
    public class Springboot3Application {
    
        public static void main(String[] args) throws Exception {
    
            ConfigurableApplicationContext context = SpringApplication.run(Springboot3Application.class, args);
            /*@ConfigurationProperties注解和@EnableConfigurationProperties配合使用*/
            System.out.println(context.getBean(ComponentProperties.class));
            context.close();
        }
    }

    启动类如上,@ComponentScan注解默认扫描启动类所在的包,该包下的类如果注入到了IOC容器中,那么在该启动类就能获取注入的bean。然后用@EnableConfigurationProperties注解使@ConfigurationProperties注解生效。因此在该启动类中就可以获取刚才application.properties配置文件转化的bean了。另外,只使用@SpringBootApplication一个注解也是可以的,因为@SpringBootApplication注解中已经包含了@ComponentScan和@EnableConfigurationProperties注解。

    4、输出结果如下:

    ComponentProperties [host=127.0.0.1, port=8080]
  • 相关阅读:
    如何成为合格的技术面试官?
    互联网上有多少个网站?
    前端领域不需要架构师?
    WebAssembly 简介
    git常用命令
    剑指offer-基本思想学习(未包括代码)
    OS知识点总结
    对软件工程的一点认识
    项目实现过程的每个阶段
    编译原理课程设计总结
  • 原文地址:https://www.cnblogs.com/jtlgb/p/9524923.html
Copyright © 2011-2022 走看看