【发布时间】:2015-08-30 18:21:23
【问题描述】:
我有两个 Spring 模块,父模块有一个子模块依赖项。这两个项目都有自己的带有 Spring 注释的 bean,并且 bean 是使用 @Bean 创建的。
项目子的资源中有child.properties,该文件用于为子项目中的Bean 设置属性。
Project Parent 有自己的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 只从父级而不是子级进入 PropertyPlaceholderConfigurer bean。
当我从父项目中删除 PropertyPlaceholderConfigurer 时,Spring 会从子项目中加载 PropertyPlaceholderConfigurer Bean。
我可以通过将child.properties 文件放在父项目中来解决此问题,但我不喜欢此解决方案,我想按项目保留每个配置。
【问题讨论】:
-
尝试将子子模块中的方法“configuration”重命名为“childConfiguration”,看看是否有帮助。