SpringBoot @ConfigurationProperties使用详解(内含源代码)

导读:本篇文章讲解 SpringBoot @ConfigurationProperties使用详解(内含源代码),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

SpringBoot @ConfigurationProperties使用详解(内含源代码)

源代码下载链接地址https://download.csdn.net/download/weixin_46411355/87400786

1.1 简述

Spring Boot中注解@ConfigurationProperties有三种使用场景,而通常情况下我们使用的最多的只是其中的一种场景。本篇文章带大家了解一下三种场景的使用情况。

1.2 场景一

使用@ConfigurationProperties@Component注解到bean定义类上,这里@Component代指同一类实例化Bean的注解。

基本使用实例如下:
com.bjpowernode.at_configurationproperties_anno.domain

User.java

package com.bjpowernode.at_configurationproperties_anno.domain;

import ch.qos.logback.core.joran.action.AppenderRefAction;
import lombok.Data;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Data
//将类定义为一个bean的注解,比如@Component,@Service,@Controller,@Repository
//或者@Configuration
@Component
//表示使用配置文件中前缀为user1的属性的值初始化该bean定义产生的bean实例的同名属性
//在使用时这个定义产生的bean时,其属性name会是Tom
@ConfigurationProperties(prefix="user1")
public class User {
    private String name;
    //lombok注解提供getter/setter方法
}

对应application.properties配置文件内容如下:

# 应用名称
spring.application.name=at_configurationproperties_anno
# 应用服务 WEB 访问端口
server.port=8080
###
user1.name=Tom

测试类

package com.bjpowernode.at_configurationproperties_anno;

import com.bjpowernode.at_configurationproperties_anno.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class AtConfigurationpropertiesAnnoApplicationTests {

    @Autowired
    private User user;

    @Test
    void contextLoads() {
        System.out.println(user);
    }

}

在此种场景下,当Bean被实例化时,@ConfigurationProperties会将对应前缀的后面的属性与Bean对象的属性匹配。符合条件则进行赋值。

1.3 场景二

使用@ConfigurationProperties@Bean注解在配置类的Bean定义方法上。以数据源配置为例:

@Configuration
public class DataSourceConfig {

	@Primary
	@Bean(name = "primaryDataSource")
	@ConfigurationProperties(prefix="spring.datasource.primary")
	public DataSource primaryDataSource() {
		return DataSourceBuilder.create().build();
	}
}

这里便是将前缀为“spring.datasource.primary”的属性,赋值给DataSource对应的属性值。

@Configuration注解的配置类中通过@Bean注解在某个方法上将方法返回的对象定义为一个Bean,并使用配置文件中相应的属性初始化该Bean的属性。

1.4 场景三

使用@ConfigurationProperties注解到普通类,然后再通过@EnableConfigurationPropertiesz注解到启动类中定义为Bean

这里User对象并没有使用@Component相关注解。

javabean

package com.bjpowernode.at_configurationproperties_anno03.domain;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

@Data
@ConfigurationProperties(prefix = "user1")
public class User {
    private String name;
}

启动类 对应的使用形式如下:

package com.bjpowernode.at_configurationproperties_anno03;

import com.bjpowernode.at_configurationproperties_anno03.domain.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

@SpringBootApplication
@EnableConfigurationProperties({User.class})
public class AtConfigurationpropertiesAnno03Application {

    public static void main(String[] args) {
        SpringApplication.run(AtConfigurationpropertiesAnno03Application.class, args);
    }

}

上述代码中,通过@EnableConfigurationPropertiesUser进行实例化时,便会使用到@ConfigurationProperties的功能,对属性进行匹配赋值。

测试类

package com.bjpowernode.at_configurationproperties_anno03;

import com.bjpowernode.at_configurationproperties_anno03.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class AtConfigurationpropertiesAnno03ApplicationTests {

    @Autowired
    private User user;

    @Test
    void contextLoads() {
        System.out.println(user);
    }

}

测试效果
在这里插入图片描述

1.5 聊聊@EnableConfigurationProperties

关于@EnableConfigurationProperties注解的使用:

@EnableConfigurationProperties的作用是把springboot配置文件中的值与我们的xxxProperties.java的属性进行绑定,需要配合@ConfigurationProperties使用。

问题:首先我想说的是,不使用@EnableConfigurationProperties能否进行属性绑定呢?
回答:答案是肯定的!我们只需要给xxxProperties.java加上@Component注解,把它放到容器中,即可实现属性绑定。(即为场景一:使用@ConfigurationProperties@Component注解到bean定义类上)

即javaBean
添加一个@Component注解
在这里插入图片描述

package com.bjpowernode.at_configurationproperties_anno03.domain;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@ConfigurationProperties(prefix = "user1")
@Component
public class User {
    private String name;
}

主配置文件就可以不要@EnableConfigurationProperties注解了
在这里插入图片描述
测试类不变
在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/85522.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!