Spring Boot多模块如何加载各模块的application.yml配置文件
Hey there! Let's fix this issue where your Spring Boot app isn't loading the common config from moduleCommons. The problem is that by default, Spring Boot only picks up the application.yml from the module you're launching (in this case, moduleWeb), so we need to explicitly tell it to include the common config too. Here are a few straightforward solutions:
Before anything else, ensure moduleWeb has a dependency on moduleCommons so the common config file is available in the classpath.
For Maven (add to moduleWeb/pom.xml):
<dependencies> <dependency> <groupId>com.myProject</groupId> <artifactId>moduleCommons</artifactId> <version>${project.version}</version> </dependency> </dependencies>
For Gradle (add to moduleWeb/build.gradle):
dependencies { implementation project(':moduleCommons') }
spring.config.import (Recommended for Spring Boot 2.4+) This is the cleanest way to import external config files in newer Spring Boot versions.
- Rename
moduleCommons/src/main/resources/application.ymltoapplication-commons.yml(to avoid filename conflicts with your module-specific configs). - In
moduleWeb/src/main/resources/application.yml, add this line to import the common config:
spring: config: import: classpath:/application-commons.yml
This way, the common config loads first, and your moduleWeb config will override any duplicate properties (perfect for your use case where you want shared settings + module-specific tweaks).
@PropertySource with YAML Support (Older Spring Boot Versions) If you're on Spring Boot <2.4, you can use @PropertySource, but note it doesn't support YAML out of the box—you'll need a custom factory class.
Step 1: Add the @PropertySource annotation to your MyProjectWebApplication class
@SpringBootApplication( scanBasePackages = {"com.myProject.web", "com.myProject.commons"}, exclude = {DataSourceAutoConfiguration.class, LiquibaseAutoConfiguration.class} ) @EnableJpaRepositories( basePackages = "com.myProject.commons.datasources.defaut.repository", repositoryFactoryBeanClass = EnversRevisionRepositoryFactoryBean.class ) @ContextConfiguration(classes = {MyProjectDbConfig.class}) @PropertySource(value = "classpath:application-commons.yml", factory = YamlPropertySourceFactory.class) // Add this line public class MyProjectWebApplication extends SpringBootServletInitializer { // ... your existing code ... }
Step 2: Create the YamlPropertySourceFactory class
import org.springframework.boot.env.YamlPropertySourceLoader; import org.springframework.core.env.PropertySource; import org.springframework.core.io.support.DefaultPropertySourceFactory; import org.springframework.core.io.support.EncodedResource; import java.io.IOException; import java.util.List; public class YamlPropertySourceFactory extends DefaultPropertySourceFactory { @Override public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException { if (resource == null) { return super.createPropertySource(name, resource); } List<PropertySource<?>> sources = new YamlPropertySourceLoader() .load(resource.getResource().getFilename(), resource.getResource()); return sources.get(0); } }
If you just want a quick way to test without changing code/config files, add a JVM argument when launching MyProjectWebApplication:
--spring.config.additional-location=classpath:/application.yml
(Note: This works if moduleCommons's application.yml is in the classpath, but might cause conflicts if both modules have the same property keys—renaming to application-commons.yml is safer.)
For your moduleBatch application, you can apply the exact same logic: set up the dependency on moduleCommons, then import the common config using one of the methods above.
内容的提问来源于stack exchange,提问作者j.2bb

