Java Spring加载基于子模块的application.properties的bean
我有两个Spring模块,父模块有一个子模块依赖项。这两个项目都有自己的Spring注释Bean,Bean是使用Java Spring加载基于子模块的application.properties的bean,java,spring,Java,Spring,我有两个Spring模块,父模块有一个子模块依赖项。这两个项目都有自己的Spring注释Bean,Bean是使用@Bean创建的。 Project child的Resources中有child.properties,该文件用于为子项目中的Bean设置属性。 项目父项目有自己的Parent.properties,也用于在父项目中创建bean =>每两个项目都有其ServiceConfig类,并用@Configuration注释,我使用该类: 家长: @Bean public static Prop
@Bean
创建的。
Project child的Resources中有child.properties
,该文件用于为子项目中的Bean设置属性。
项目父项目有自己的Parent.properties
,也用于在父项目中创建bean
=>每两个项目都有其ServiceConfig
类,并用@Configuration
注释,我使用该类:
家长:
@Bean public static PropertyPlaceholderConfigurer configuration(){
final PropertyPlaceholderConfigurer props=new PropertyPlaceholderConfigurer();
props.setLocations(new Resource[]{new ClassPathResource("parent.properties")});
return props;
}
子项:
@Bean public static PropertyPlaceholderConfigurer configuration(){
final PropertyPlaceholderConfigurer props=new PropertyPlaceholderConfigurer();
props.setLocations(new Resource[]{new ClassPathResource("child.properties")});
return props;
}
现在我的问题是:我需要将一个bean从子项目自动连接到父项目,当我运行父项目(在pom.xml
中有子项目作为依赖项)时,无法构建自动连接的子bean,因为没有加载child.properties
。当我调试时,我看到spring只在父级而不是子级的PropertyPlaceHolderConfigure
bean中输入。
当我从父项目中删除propertyplaceholderconfigure
时,Spring从子项目中加载propertyplaceholderconfigure
Bean
我可以通过在父项目中放置
子.properties
文件来解决此问题,但我不喜欢此解决方案,我希望按项目保留每个配置。由于两个PropertyPlaceholderConfigurer bean具有相同的bean名称,因此子bean被父bean覆盖(如果你仔细查看日志,你会看到上面有说明)
尝试给它们不同的名称用
@PropertySources
注释注释相应应用程序的Spring配置。它允许您指定@PropertySources
文件的数组
e、 g
Spring将向您的应用程序添加注释的属性。有关更多信息,请参阅Spring文档。尝试将子模块中的方法“配置”重命名为“childConfiguration”,看看是否有帮助。
@Configuration
@PropertySources({
@PropertySource("classpath:parent.properties"),
@PropertySource("classpath:child.properties")})
public class MyApp() // Add annotations on YOUR configuration
{
...
}