Java Spring引导忽略其他属性文件
我在模块A中使用application.properties和相应的properties类plus配置设置了以下Maven多模块:Java Spring引导忽略其他属性文件,java,spring-boot,properties,Java,Spring Boot,Properties,我在模块A中使用application.properties和相应的properties类plus配置设置了以下Maven多模块: @Component @ConfigurationProperties(prefix = "city") @Getter @Setter @ToString public class CityProperties { private int populationAmountWorkshop; private double productionInef
@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
@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();
}
}
以及bean-test.properties文件:
spring.main.allow-bean-definition-overriding=true
从CConfiguration
中删除最后一个bean时,应用程序将启动。但是,有了这个bean定义,我得到了以下错误:
说明:
无法注册在类路径资源[c/CConfiguration.class]中定义的bean“cityProperties”。已在文件[/module1/target/classes/A/CityProperties.class]中定义了具有该名称的bean,并且已禁用重写
行动:
考虑重命名一个bean,或者通过设置Spring。
这正是我在bean-test.properties中定义的。然而,这似乎被忽视了。起初我认为属性文件甚至没有加载,但当在PropertySource
中拼写错误时,我得到了一个错误,即在类路径上找不到该文件,这表明该文件是由Spring拾取的,但它的内容被忽略了。我认为
@PropertySource("classpath:bean-test.properties")
将仅用于自定义特性,而不用于弹簧特性。
试着把
spring.main.allow-bean-definition-overriding=true
在application.properties.中,@Andreas的可能重复项,响应建议在我的
CConfiguration
之前使用AutoConfigureBefore
,但在什么之前,因为另一个bean是由在同一配置
类上定义的组件扫描拾取的。此外,我的印象是,在bean定义上有一个Primary
,这将得到解决。bean定义中的@Primary
使它成为按类型自动布线时的默认值。豆子还是要吃的。如果希望加载两个bean,其中一个是主bean,则需要重命名其中一个,例如,或者重命名方法,因为如果未在@bean
注释中指定,则方法名称隐式地为bean名称。bean定义重写是指一个bean替换另一个同名bean。这就是@AutoConfigureBefore/After
可能变得相关的地方,尽管它打算与条件注释(如@ConditionalOnMissingBean
)一起使用,以便只加载一个bean定义,因此不会发生重写。关于bean定义重写的更多信息,我强烈建议您阅读。这是可行的,但是问题的原始用例是,spring boot应用程序是在单元测试(不是spring boot测试)中启动的,在我的应用程序代码中,我不允许重写bean,但对于测试它是有意义的。