Spring Cloud Config: Resolving "No spring.config.import property" Error
Problem Statement
When working with Spring Boot and Spring Cloud Config, you may encounter the error:
No spring.config.import property has been defined
Action:
Add a spring.config.import=configserver: property to your configuration.
If configuration is not required add spring.config.import=optional:configserver: instead.
To disable this check, set spring.cloud.config.enabled=false or
spring.cloud.config.import-check.enabled=false.
This error typically occurs in Spring Boot 2.4 and later versions when attempting to configure a Config Client application without properly using the new configuration import mechanism. The error indicates that Spring Cloud Config expects a specific property to be defined but cannot find it.
Root Cause
Starting with Spring Boot 2.4, the configuration loading mechanism was substantially changed. The legacy bootstrap.properties
/bootstrap.yml
approach was deprecated in favor of the new spring.config.import
property. The error occurs when:
- Using newer Spring Boot/Cloud versions but configuring with the old bootstrap approach
- Missing the required
spring.config.import
property - Having incorrect or incomplete Config Client configuration
Solutions
Primary Solution: Use spring.config.import Property
For Spring Boot 2.4+ applications, use the spring.config.import
property in your application.properties
or application.yml
:
spring.config.import=optional:configserver:http://localhost:8888
spring:
config:
import: "optional:configserver:http://localhost:8888"
The optional:
prefix indicates that the application should start even if the Config Server is unreachable. If you require the Config Server to be available, use configserver:
without the optional prefix.
Config Server Connection Details
To properly configure your Config Client, ensure you specify:
spring.application.name=your-service-name
spring.config.import=optional:configserver:http://localhost:8888
spring:
application:
name: your-service-name
config:
import: "optional:configserver:http://localhost:8888"
Replace your-service-name
with your actual service name and http://localhost:8888
with your Config Server's URL.
Legacy Bootstrap Approach (Deprecated)
If you need to maintain compatibility with legacy bootstrap configuration, add the bootstrap starter dependency:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap'
Then you can use bootstrap.properties
or bootstrap.yml
:
spring.application.name=your-service-name
spring.cloud.config.uri=http://localhost:8888
spring:
application:
name: your-service-name
cloud:
config:
uri: http://localhost:8888
WARNING
The bootstrap approach is deprecated. Use the spring.config.import
method for new projects.
Disable Config Client (For Testing)
If you don't need Config Server functionality during testing or development, you can disable it:
spring.cloud.config.enabled=false
spring:
cloud:
config:
enabled: false
Alternatively, disable just the import check:
spring.cloud.config.import-check.enabled=false
spring:
cloud:
config:
import-check:
enabled: false
TIP
This approach is particularly useful for test configurations where you don't want to connect to a Config Server.
Complete Example
Here's a complete example of a properly configured Config Client:
spring:
application:
name: user-service
config:
import: "optional:configserver:http://localhost:8888"
cloud:
config:
enabled: true
server:
port: 8080
spring.application.name=user-service
spring.config.import=optional:configserver:http://localhost:8888
spring.cloud.config.enabled=true
server.port=8080
Troubleshooting Tips
- Verify Spring Boot and Cloud Versions: Ensure compatibility between your Spring Boot and Spring Cloud versions
- Check Config Server Availability: Verify your Config Server is running and accessible
- Maven/Gradle Refresh: After adding dependencies, refresh your project (
mvn clean install
or Gradle sync) - Check Property Placement: Ensure properties are in the correct configuration file (typically
application.properties
orapplication.yml
) - Verify Dependency Inclusion: Ensure you have the Spring Cloud Config Client dependency:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
implementation 'org.springframework.cloud:spring-cloud-starter-config'
Best Practices
- Use the latest configuration approach with
spring.config.import
for new projects - Make Config Server connection optional in development using
optional:configserver:
- Use proper error handling and fallback mechanisms for production environments
- Keep Spring Boot and Spring Cloud versions compatible
- Test configurations both with and without Config Server connection
By following these solutions and best practices, you can resolve the "No spring.config.import property" error and properly configure your Spring Cloud Config Client applications.