Spring Boot AutoConfigure provides auto-configuration capabilities that automatically configure Spring applications based on jar dependencies present on the classpath
—
Learn essential configuration concepts for Spring Boot AutoConfigure.
Spring Boot loads configuration from multiple sources (in order of precedence):
application.properties or application.ymlPlace in src/main/resources/application.properties:
# Server configuration
server.port=8080
server.servlet.context-path=/api
# Application name
spring.application.name=my-application
# Logging
logging.level.root=INFO
logging.level.com.example=DEBUGAlternatively, use application.yml:
server:
port: 8080
servlet:
context-path: /api
spring:
application:
name: my-application
logging:
level:
root: INFO
com.example: DEBUG# application-dev.properties
server.port=8080
debug=true
# application-prod.properties
server.port=80
debug=falsejava -jar app.jar --spring.profiles.active=prodOr in properties:
spring.profiles.active=devimport org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "app")
public class AppProperties {
private String name;
private int timeout;
private List<String> servers;
// Getters and setters
}app.name=My Application
app.timeout=30
app.servers[0]=server1.example.com
app.servers[1]=server2.example.com@Service
public class MyService {
private final AppProperties properties;
public MyService(AppProperties properties) {
this.properties = properties;
}
public void doSomething() {
String name = properties.getName();
// Use configuration
}
}@SpringBootApplication(exclude = {
DataSourceAutoConfiguration.class,
HibernateJpaAutoConfiguration.class
})
public class Application {
// ...
}@SpringBootApplication(excludeName = {
"org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration"
})
public class Application {
// ...
}spring.autoconfigure.exclude=\
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration@Configuration
@ConditionalOnProperty(
prefix = "app.feature",
name = "enabled",
havingValue = "true"
)
public class FeatureConfiguration {
@Bean
public FeatureService featureService() {
return new FeatureService();
}
}app.feature.enabled=true@Configuration
@ConditionalOnClass(DataSource.class)
@ConditionalOnProperty("app.database.enabled")
public class DatabaseConfiguration {
// Configuration
}@Configuration
public class EnvironmentConfiguration {
@Bean
@Profile("dev")
public DataSource devDataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.build();
}
@Bean
@Profile("prod")
public DataSource prodDataSource() {
HikariDataSource ds = new HikariDataSource();
ds.setJdbcUrl(env.getProperty("db.url"));
return ds;
}
}export SERVER_PORT=8080
export SPRING_DATASOURCE_URL=jdbc:mysql://localhost/dbMaps to:
server.port=8080
spring.datasource.url=jdbc:mysql://localhost/dbjava -jar app.jar --server.port=8080 --spring.profiles.active=prodimport jakarta.validation.constraints.*;
@ConfigurationProperties(prefix = "app")
@Validated
public class AppProperties {
@NotNull
@Size(min = 1, max = 100)
private String name;
@Min(1)
@Max(3600)
private int timeout;
@Email
private String adminEmail;
// Getters and setters
}Add dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=user
spring.datasource.password=pass
spring.jpa.hibernate.ddl-auto=updatelogging.level.root=WARN
logging.level.org.springframework.web=DEBUG
logging.level.com.example=DEBUG
logging.file.name=app.logserver.port=8080
server.compression.enabled=true
server.http2.enabled=true
server.ssl.enabled=falsespring.cache.type=caffeine
spring.cache.caffeine.spec=maximumSize=500,expireAfterAccess=600s@ConfigurationProperties over @Value