Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring加载基于子模块的application.properties的bean_Java_Spring - Fatal编程技术网

Java Spring加载基于子模块的application.properties的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

我有两个Spring模块,父模块有一个子模块依赖项。这两个项目都有自己的Spring注释Bean,Bean是使用
@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 
{
    ...
}