当前位置: 首页 > 知识库问答 >
问题:

Spring Boot忽略其他属性文件

邰德业
2023-03-14

我在应用程序中设置了以下Maven多模块。属性以及模块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;
    }
}

应用程序。特性:

#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
@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
@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();
    }
}

还有豆子测试。属性文件:

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

CConfiguration中删除最后一个bean时,应用程序将启动。但是,有了这个bean定义,我得到了以下错误:

描述:无法注册在类路径资源[c/CConfiguration.class]中定义的bean“cityProperties”。已在文件[/module1/target/classes/A/CityProperties.class]中定义了具有该名称的bean,并且已禁用重写。

操作:考虑重命名一个bean或通过设置spring.main.allow-bean-定义-重写=true启用重写

这正是我在bean测试中定义的。财产。然而,这似乎被忽视了。起初我认为属性文件甚至没有加载,但当在PropertySource中拼写错误时,我得到了一个错误,即在类路径上找不到该文件,这表明该文件是由Spring提取的,但其内容被忽略了。

共有1个答案

薛承志
2023-03-14

我认为

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

将仅用于自定义特性,而不用于Spring特性。试着把

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

在应用程序中。财产。

 类似资料:
  • 问题内容: 鉴于以下课程, 假设在调用ClassOne.main之后还有其他事情要做,则下面的类也将被破坏。 有没有办法忽略System.exit(1); ClassTwo的调用中对ClassOne的了解? 问题答案: 您 本身 不能忽略它,但是可以防止它通过SecurityManager终止JVM。查看此问题以获取详细的代码示例。

  • 问题内容: 我在GlassFish 4上使用java.util.logging。 我正在定义自己的类,以通过定义System属性 来初始化。 我的班级加载文件,将其与其他属性文件合并,并进行一些自定义替换。 以下是我文件的相关部分: 我正在使用标准并将其配置为使用格式化程序。 但是字符串被完全忽略了。相反,SimpleFormatter使用其默认格式。 我哪里做错了? 问题答案: 我假设系统属性是

  • 通过参考关于在,我想知道当文件不存在时如何忽略。 目前,下面的文件抛出FileNot找到异常,无法继续编译。 我想要的是让开发者编译。属性是否存在。我怎样才能做到这一点?

  • 问题内容: 我有一个标准POJO,其中包含一组属性。POJO已被注释为,以便作为文档保留在MongoDB中。 我如何(注释?)可以忽略/避免POJO中的属性之一被保留? 问题答案: 该注释是。参见http://static.springsource.org/spring- data/data-document/docs/current/reference/html/#mapping-usage-

  • 我在使用springboot 1.5.4时遇到了两个问题,我无法解决它们。 问题 1. 我在src/main/resources中配置了logback-spring.xml,当spring boot启动时,将创建两个目录,一个以“应用程序名”启动,另一个以bootstrap启动.. 我很困惑为什么创建bootstrap日志文件目录,spring boot创建了两个日志目录,顺便说一下,spring

  • 问题内容: 我有一个Golang程序,该程序从命令行读取字符串参数,并将其传递给fmt.Sprintf函数。假设tmp_str是命令行中的目标字符串。 在某些情况下,该程序将传递完整的字符串,例如“ Hello Friends”,而不是字符串模板。.程序会惊慌并返回: 你好朋友%!(EXTRA string = world) 因此,如何忽略fmt.Sprintf的额外字段? 问题答案: 是的,您可