spring-boot之@ConfigurationProperties的使用

个人博客导航页(点击右侧链接即可打开个人博客):大牛带你入门技术栈 

@ConfigurationProperties是什么?

Using the @Value("${property}") annotation to inject configuration properties can sometimes be cumbersome, especially if you are working with multiple properties or your data is hierarchical in nature. Spring Boot provides an alternative method of working with properties that lets strongly typed beans govern and validate the configuration of your application.

上面就是官方文档的解释,如果你想使用多个属性或者你的数据是由层次结构的,那么就可以使用@ConfigurationProperties来处理

那么什么是层次的结构呢,实际上就是我们开发过程中说的属性的嵌套,例如一个Person 类中有一个引用类型是Address 那么这种就是有层次结构的


那么怎么使用呢

Spring Boot provides infrastructure to bind @ConfigurationProperties types and register them as beans. You can either enable configuration properties on a class-by-class basis or enable configuration property scanning that works in a similar manner to component scanning.

大概意思是说spring-boot对绑定@ConfigurationProperties 提供了一个基础的功能,你可以通过扫描组件或者通过创建类的方式进行,那么常用的大概有这几种

1.@EnableConfigurationProperties 和ConfigurationProperties 配合使用

@ConfigurationProperties(prefix = "acme")
@Data
public class AcmeProperties {
    private boolean enabled;

    private InetAddress remoteAddress;

    private final Security security = new Security();

    @Data
    public static class Security {

        private String username;

        private String password;

        private List roles = new ArrayList<>(Collections.singleton("USER"));


    }
}

@Configuration
@EnableConfigurationProperties({AcmeProperties.class})
public class AcmeConfigTest {
}


@RestController
@RequestMapping("/properties-get")
public class PropertiesTestController {
    //此处注入
    @Autowired
    private AcmeProperties acmeProperties;

    @GetMapping("/address")
    public String getRemoteAddress(){
        return JSON.toJSONString(acmeProperties.getRemoteAddress());
    }

    @GetMapping("/enable")
    public String getEnabled(){
        return JSON.toJSONString(acmeProperties.isEnabled());
    }

    @GetMapping("/security")
    public String getPropertyValue(){
        return JSON.toJSONString(acmeProperties.getSecurity());
    }
}

这种在写你自己的自动配置时经常用到,可以参考spring-boot 有关的自动配置大部分都是这么干的

2.@Component和@ConfigurationProperties配合

@ConfigurationProperties(prefix = "acme")
@Data
@Component
public class AcmeProperties {
    private boolean enabled;

    private InetAddress remoteAddress;

    private final Security security = new Security();

    @Data
    public static class Security {

        private String username;

        private String password;

        private List roles = new ArrayList<>(Collections.singleton("USER"));


    }
}


@RestController
@RequestMapping("/properties-get")
public class PropertiesTestController {
    @Autowired
    private AcmeProperties acmeProperties;

    @GetMapping("/address")
    public String getRemoteAddress(){
        return JSON.toJSONString(acmeProperties.getRemoteAddress());
    }

    @GetMapping("/enable")
    public String getEnabled(){
        return JSON.toJSONString(acmeProperties.isEnabled());
    }

    @GetMapping("/security")
    public String getPropertyValue(){
        return JSON.toJSONString(acmeProperties.getSecurity());
    }
}

3.使用在方法上和@Bean一起

@Data
public class AcmeProperties {
    private boolean enabled;

    private InetAddress remoteAddress;

    private final Security security = new Security();

    @Data
    public static class Security {

        private String username;

        private String password;

        private List roles = new ArrayList<>(Collections.singleton("USER"));


    }
}



@Configuration
public class AcmeConfigTest {

    @ConfigurationProperties(prefix = "acme")
    @Bean
    public AcmeProperties acmeProperties(){
        return new AcmeProperties();
    }
}

这种方式,在绑定第三方配置特别有用

有关配置文件

acme.enabled = true
acme.remote-address=127.0.0.1
acme.security.username = "wei"
acme.security.roles = {"role1","role2"}

附Java/C/C++/机器学习/算法与数据结构/前端/安卓/Python/程序员必读/书籍书单大全:

(点击右侧 即可打开个人博客内有干货):技术干货小栈
=====>>①【Java大牛带你入门到进阶之路】<<====
=====>>②【算法数据结构+acm大牛带你入门到进阶之路】<<===
=====>>③【数据库大牛带你入门到进阶之路】<<=====
=====>>④【Web前端大牛带你入门到进阶之路】<<====
=====>>⑤【机器学习和python大牛带你入门到进阶之路】<<====
=====>>⑥【架构师大牛带你入门到进阶之路】<<=====
=====>>⑦【C++大牛带你入门到进阶之路】<<====
=====>>⑧【ios大牛带你入门到进阶之路】<<====
=====>>⑨【Web安全大牛带你入门到进阶之路】<<=====
=====>>⑩【Linux和操作系统大牛带你入门到进阶之路】<<=====

天下没有不劳而获的果实,望各位年轻的朋友,想学技术的朋友,在决心扎入技术道路的路上披荆斩棘,把书弄懂了,再去敲代码,把原理弄懂了,再去实践,将会带给你的人生,你的工作,你的未来一个美梦。

你可能感兴趣的:(spring-boot之@ConfigurationProperties的使用)