SpringBoot应用在Eclipse中运行时配置文件读取异常
Hey there, sorry you're running into this inconsistent config file behavior in Eclipse with Spring Boot—let's walk through the most common fixes that usually resolve this issue:
First: Check the Test Config Naming Convention
Spring Boot follows a strict default pattern for profile-specific config files: application-{profile}.properties. If you’re using testapplication.properties instead of application-test.properties, Spring won’t automatically recognize it as the test environment config unless you explicitly tell it to. That’s one of the most frequent mistakes here!
If you want to stick with your current filename, add this annotation to your test classes to point Spring to the correct file:
@SpringBootTest @TestPropertySource(locations = "classpath:testapplication.properties") public class YourTestClass { // Test logic here }
Or, rename your test config to application-test.properties and add @ActiveProfiles("test") to your test classes to let Spring pick it up automatically.
Verify File Placement & Eclipse Resource Settings
Correct directory structure:
application.propertiesmust live insrc/main/resources(for non-test runs)- Your test config should be in
src/test/resources(for test runs)
Eclipse uses these folders to separate main and test classpath resources—putting them in the wrong place means they won’t be loaded when you run the corresponding environment.
Fix Eclipse build path filters:
- Right-click your project → Properties → Java Build Path → Source tab
- Confirm
src/main/resourcesandsrc/test/resourcesare listed as source folders - For each folder:
- Check the "Output folder" is set to
target/classes(main) andtarget/test-classes(test) - Ensure
.propertiesfiles aren’t listed under "Filtered resources" (if they are, remove the filter)
- Check the "Output folder" is set to
Clean & Rebuild to Eliminate Stale Files
Eclipse often caches old resource files in the target directory, leading to unexpected behavior. Do this step-by-step:
- Go to Project → Clean... → Select your project and click OK
- Run a full build: For Maven, right-click → Run As → Maven clean install; for Gradle, Run As → Gradle Build (include
cleanandbuildtasks) - Stop any running app instances and restart your Eclipse run configuration
Double-Check Run Configurations
- When running tests: Use a JUnit run configuration, not a Spring Boot App configuration. The latter will only load resources from
src/main/resourcesby default. - If using profiles: Ensure the correct profile is activated. For tests, add
@ActiveProfiles("test")to your test classes. For non-test runs, check that no conflictingspring.profiles.activevalue is set in VM arguments or main properties.
Final Check: Compare Config Files
Even if you think the files are identical, use Eclipse’s Compare feature to confirm: Right-click one file → Compare With → Other Resource → Select the other config file. Look for subtle differences like trailing spaces, typos, or encoding issues that might be causing the property resolution error.
内容的提问来源于stack exchange,提问作者Telefon Wolakpdx

