Spring Boot configuration

Annotations

Autoconfiguration is enabled by @EnableAutoConfiguration.

@SpringBootConfiguration -> extends @Configuration
@ComponentScan("..")
@EnableAutoConfiguration
public class Application {
 public static void main(String[] args){
  SpringApplication.run(Application.class,args);
 }

}

SpringBootConfiguration

Indicates that a class provides Spring Boot application @Configuration. Can be used as an alternative to the Spring’s standard @Configuration annotation so that configuration can be found automatically (for example in tests).
Application should only ever include one @SpringBootConfiguration and most idiomatic Spring Boot applications will inherit it from @SpringBootApplication. (source)

SpringBootApplication

Annotation @SpringBootApplication comprehends the following annotations:

  • @EnableAutoConfiguration
  • @ComponentScan
  • @SpringBootConfiguration
@SpringBootApplication(scanBasePackages="..")
public class Application{
}

Tests

@SpringBootTest is an alternative to @SpringJUnitConfig.

@SpringBootTest(classes=Application.class) --> loads config from @SpringBootApplication class
public class TransferServicesTests {
 @Autowired
 private TransferService transferService;

 @Test
 public void successfulTransfer(){
  TransferConfirmation conf = transferService.transfer(...);
  ...
 }

}

Properties

Spring looks for application.properties, application-{profile}.properties file, in:

  • classpath
  • /config folder in package or working directory
  • working directory while using the jar

Property file application.yml requires snakeYaml dependency and it’s not supported by @PropertySource, the yaml file can have sections with profile definitions.

---
spring:
 profiles: cloud
  xxxx: xxxx

@ConfigurationProperties

This annotation will hold externalized properties, avoids repeating prefix and set values from data members from corresponding properties.

application.properties
-----
rewards.client.host=192.168.1.42
rewards.client.port=8080
rewards.client.logdir=/logs
rewards.client.timeout=2000
---

@ConfigurationProperties(prefix="rewards.client")
public class ConnectionSettings {
 private String host;
 private int port;
 private Strong logdir;
 private int timeout;

}

Enabling @ConfigurationProperties can be one in three ways:

In application class:

@SpringBootApplication
@EnableConfigurationProperties(ConnectionSetting.class)
public class RewardsApplication { .. }

Add @ConfigurationPropertiesScan to application class:

@SpringBootApplication
@ConfigurationPropertiesScan
public class RewardsApplication {..}

Add @Component the configuration bean class itself:

@Component
@ConfigurationProperties(prefix="rewards.client")
public class ConnectionSettings {..}

Auto-configuration

Auto configuration is enable in @SpringBootApplication or using @EnableAutoConfiguration.

Creating a conditional clause:

//Create JdbcTemplate bean only when DataSource bean already exists
@Bean
@ConditionalOnBean(DataSource.class) // no need for @Autowired
public JdbcTemplate jdbcTemplate(DataSource dataSource){ 
 return new Jdbc;
}
//also: @ConditionalOnMissingBean, @ConditionalOnMissingClass, @ConditionalOnClass, @ConditionalOnProperty, ...

Override Auto-configuration

Ways of override Spring auto configuration:

  • definitions in application.properties
  • Define the proper beans (ex. DataSoruce).
  • Disable autoconfiguration.
@EnableAutoConfiguration(exclude=DataSourceAutoConfiguration.class) --> also, @SpringBootApplication
public class ApplicationConfiguration {
..
}

Or use configuration
spring.autoconfigure.exclude=x.x..MyClass