32-Spring Boot - A Closer Look Flashcards
What is defactor standard?
De facto standards are those which have been accepted as the best standard for their purpose
Spring Boot looks for application.properties by default
What locations will Spring Boot look for application.properties?
First, /config: subdirectory of the working directory
Then, working directory
Then, config package in the classpath
Last, classpath root
If no profile is active, what properties files will be loaded?
application.properties
application-default.properties
How to change the location that Spring boot looks up for properties files?
spring. config.location
e. g.
spring. config.location=classpath:/default.properties,classpath:/override.properties
How to change the default application config file that Spring boot looks for?
spring.config.name
default value is “application”
Does @PropertySource support YML config files?
No,
YAML is only supported when Spring Boot loads application.yml
How to specify multiple profiles config in a single file?
Using --- to separate and spring.profiles for profile name e.g. # Sections without spring.profiles are always loaded # Something --- spring: profiles: local --- spring: profiles: cloud
Order that Spring Boot select properties
- Devtools settings
- @TestPropertySource and @SpringBootTest properties
- Command line arguments
- SPRING_APPLICATION_JSON (inline JSON properties).
- ServletConfig / ServletContext parameters.
- JNDI attributes from java:comp/env
- Java System properties
- OS environment variables
- Profile-specific application properties
- Application properties / YAML
- @PropertySource files
- SpringApplication.setDefaultProperties.
What is the purpose of using @ConfigurationProperties?
e.g. @ConfigurationProperties(prefix="rewards.client") public class ConnectionSettings { // .. binding properties start with rewards.client to this class }
Is ConfigurationProperties a part of Spring framework or Spring boot?
It’s a part of Spring boot
Ways to enable @ConfigurationProperties?
1. @SpringBootApplication @EnableConfigurationProperties(ConnectionSettings.class) public class Application {} 2. @SpringBootApplication @ConfigurationPropertiesScan public class Application {} 3. @Component @ConfigurationProperties(prefix="rewards.client") public class ConnectionProperties {}
What is relaxed binding?
Flexible in properties matching. Only applied for @ConfigurationProperties e.g. @ConfigurationProperties("rewards.client-connection") class Config { String hostUrl; } Valid matches: 1. rewards.clientConnection.hostUrl 2. rewards.client-connection.host-url 3. rewards.client_connection.host_url 4. REWARDS_CLIENTCONNECTION_HOSTURL
List out 6 @Conditional
1, 2. ConditionalOnClass, ConditionalOnMissingClass
3, 4. ConditionalOnBean, ConditionalOnMissingBean
5, 6. ConditionalOnProperty, ConditionalOnMissingProperty
What are the targets of @Conditional?
class, bean method
What jar file defines auto-configuration logic in Spring boot?
spring-boot-autoconfigure.jar
What are inside spring-boot-autoconfigure/META-INF/spring.factories?
- @EnableAutoConfiguration reads a factories file for list of AutoConfiguration classes used by Spring Boot
How to configure schema and default data for DB?
spring. datasource.schema=/testdb/schema.sql
spring. datasource.data=/testdb/data.sql
How to configure MySQL db connection?
spring. datasource.url=jdbc:mysql:…
spring. datasource.username=
spring. datasource.password=
How to update the default log level of the application?
logging.level.root=INFO
How to update the log level of a package?
logging.level.com.myproject=DEBUG
Ways to exclude 1 auto-config in Spring Boot app
- @EnableAutoConfiguration(exclude=DataSourceAutoConfiguration.class)
- (application.yml) spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
What pooled datasources are supported by Spring Boot?
Tomcat
HikariCP
Commons DBCP 1 & 2
How to set pooled datasource type in Spring Boot?
spring.datasource.type=
e.g. Tomcat
HikariCP
Commons DBCP 1 & 2
What is the order of selecting pooled data source type?
HikariCP (highest precedence) –> Tomcat Commons DBCP2