@EnableConfigurationProperties失效了 不能将配置类导入容器

以前,我定义属性值都是在application开头的配置文件的,有一天自定义了一个配置文件名为ldj.properties.结合注解 @PropertySource 指定加载读取它,然后@EnableConfigurationProperties测试发现,使用的都是默认值,没有读取配置的值.

package com.dj.springtest.config.properties;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * User: ldj
 * Date: 2023/7/14
 * Time: 23:27
 * Description: No Description
 */
@Data
//@Component("test") //直接然容器扫描成为Bean 成功
@PropertySource(value = "classpath:ldj.properties")
@ConfigurationProperties(prefix = "spring.test.config")
public class TestConfigProperties {

    private String name = "ldj";

    private Integer age = 18;

    //@ConfigurationProperties和 @Value 可以兼容的
    @Value("${demo.test.gender:女}")
    private String gender;
}
package com.dj.springtest.config;

import com.dj.springtest.config.properties.TestConfigProperties;
import com.dj.springtest.service.TestService;
import com.dj.springtest.service.TestServiceImpl;
import lombok.Data;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * User: ldj
 * Date: 2023/7/14
 * Time: 23:28
 * Description: No Description
 */
@Data
@Configuration
//@Import(value = {TestConfigProperties.class}) //直接当普通类导入成Bean 成功,默认也是先从bootstrap,application开头的文件获取,没有再从ldj.properties获取
@EnableConfigurationProperties(value = {TestConfigProperties.class}) //失败!! 属性值必须配置在application或者bootstrap开头文件才可以
public class TestConfig {

    @Bean
    @ConditionalOnMissingBean(TestService.class)
    public TestServiceImpl testService(TestConfigProperties properties) {
        return new TestServiceImpl(properties.getName(), properties.getAge());
    }

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
@EnableConfigurationProperties注解失效的原因可能是因为缺少@ConfigurationPropertiesScan注解。在使用@EnableConfigurationProperties注解时,需要在应用程序的主(通常是带有@SpringBootApplication注解的)上添加@ConfigurationPropertiesScan注解,以便扫描和加载配置。如果没有添加@ConfigurationPropertiesScan注解,@EnableConfigurationProperties注解将无效,并且配置项无法正确加载。因此,要解决@EnableConfigurationProperties失效的问题,需要在主上添加@ConfigurationPropertiesScan注解。这样就可以正确调用和加载配置项了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [使用@EnableConfigurationProperties注解,注意配置,避免失效](https://blog.csdn.net/weixin_42792301/article/details/105437035)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [SpringBoot中@EnableConfigurationProperties失效](https://blog.csdn.net/taxusw/article/details/127377649)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值