簡體   English   中英

Spring boot 忽略額外的屬性文件

[英]Spring boot ignores additional properties files

我有以下 Maven 多模塊設置,其中包含 application.properties 和相應的屬性類以及模塊 A 中的配置:

@Component
@ConfigurationProperties(prefix = "city")
@Getter
@Setter
@ToString
public class CityProperties {
    private int populationAmountWorkshop;
    private double productionInefficientFactor;
    private Loaner loaner = new Loaner();
    private Tax tax = new Tax();
    private Guard pikeman = new Guard();
    private Guard bowman = new Guard();
    private Guard crossbowman = new Guard();
    private Guard musketeer = new Guard();

    @Getter
    @Setter
    @ToString
    public static class Loaner {
        private int maxRequest;
        private int maxAgeRequest;
        private int maxNbLoans;
    }

    @Getter
    @Setter
    @ToString
    public static class Tax {
        private double poor;
        private double middle;
        private double rich;
        private int baseHeadTax;
        private int basePropertyTax;
    }

    @Getter
    @Setter
    @ToString
    public static class Guard {
        private int weeklySalary;
    }
}

application.properties:

#City
# Amount of inhabitants to warrant the city to have one workshop
city.populationAmountWorkshop=2500
# Factor that is applied on the efficient production to get the inefficient production
city.productionInefficientFactor=0.6
# Maximum requests per loaner
city.loaner.maxRequest=6
# Maximum  age of loan request in weeks
city.loaner.maxAgeRequest=4
# Maximum loan offers per loaner
city.loaner.maxNbLoans=3
# Weekly tax value factor for the various population per 100 citizens
city.tax.poor=0
city.tax.middle=0.6
city.tax.rich=2.0
city.tax.baseHeadTax=4
city.tax.basePropertyTax=280
city.pikeman.weeklySalary=3
city.bowman.weeklySalary=3
city.crossbowman.weeklySalary=4
city.musketeer.weeklySalary=6

Configuration

@Configuration
@ComponentScan(basePackageClasses = AConfiguration.class)
public class AConfiguration {
}

然后是模塊 C 與我的應用程序:

@SpringBootApplication
@Import({CConfiguration.class})
public class SpringBootApp {

    public static void main(String[] args) {
        SpringApplicationBuilder builder = new SpringApplicationBuilder(SpringBootApp.class);
        ApplicationContext context = builder.profiles("server").run();
        CityProperties cityProperties = context.getBean(CityProperties.class);
        System.out.println(cityProperties);
    }
}

相關Configuration

@Configuration
@Import(AConfiguration.class)
@ComponentScan(basePackageClasses = {CConfiguration.class, DComponents.class})
@PropertySource("classpath:bean-test.properties")
public class CConfiguration {
    @Autowired
    @Qualifier("serverThreadPool")
    private Executor serverThreadPool;
    @Autowired
    private SubscriberExceptionHandler subscriptionExceptionHandler;

    @Bean
    public AsyncEventBus serverEventBus() {
        return new AsyncEventBus(serverThreadPool, subscriptionExceptionHandler);
    }

    @Bean
    @Primary
    @ConfigurationProperties(prefix = "city")
    public CityProperties cityProperties() {
        return new CityProperties();
    }
}

和 bean-test.properties 文件:

spring.main.allow-bean-definition-overriding=true

CConfiguration刪除最后一個 bean 時,應用程序將啟動。 但是,有了那個 bean 定義,我得到了這個錯誤:

描述:無法注冊在類路徑資源 [c/CConfiguration.class] 中定義的 bean 'cityProperties'。 具有該名稱的 bean 已經在文件 [/module1/target/classes/a/CityProperties.class] 中定義並且覆蓋被禁用。

行動:考慮通過設置 spring.main.allow-bean-definition-overriding=true 重命名其中一個 bean 或啟用覆蓋

這正是我在 bean-test.properties 中定義的。 然而,這似乎被忽略了。 起初我以為屬性文件甚至沒有加載,但是當在PropertySource拼錯它的名字時,我得到錯誤,在類路徑上找不到該文件,這表明該文件是由 Spring 拾取的,但它的內容是忽略。

我覺得

@PropertySource("classpath:bean-test.properties")

將僅用於自定義屬性,而不用於彈簧屬性。 試着放

spring.main.allow-bean-definition-overriding=true

在 application.properties 中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM