Spring Boot Concepts
Internal
Spring Project Structure
Autoconfiguration
With autoconfiguration, Spring Boot can make reasonable guesses of what components need to be configured and wired together based on the classpath content, environment variables and other factors. It is enabled by the @EnableAutoConfiguration annotation, which is implied by @SpringBootApplication. When the application starts, Spring Boot autoconfiguration detects libraries such as Spring MVC, Tomcat, SQL databases and configures the application bean with the right dependencies.
Also see Spring Framework's Spring Framework autoconfiguration.
Developer Tools (DevTools)
Additional set of tools that come as part of Spring Boot, and which can make, according to the documentation, "development experience a little more pleasant". The artifacts are identified as org.springframework.boot:spring-boot-devtools. They include the following development-time features:
- automatic application restart when Java code or property files change. This is implemented by loading everything that is maintained in src/main, such as application classes and property files, with a special classloader. This classloader monitors changes and reloads the new classes and restarts the application context when a change is detected. The other classloader loads dependency libraries.
- automatic browser refresh when browser resources (templates, JavaScript, stylesheets, etc.) change. More details in SIA page 24.
- automatic disable of template caches
- built-in H2 console, which can be used agains the H2 database, it deployed. For more details see:
Development tools are self-disable when the application runs in production settings, such as when running as a fully packaged application (such as with java -jar).
They should be declared optional in Maven or "compileOnly" in Gradle.
Use:
dependencies {
runtimeOnly('org.springframework.boot:spring-boot-devtools')
}
Configuration Processor
Spring Boot Starter Dependency
A Spring Boot starter is a dependency that has the word "starter" in their artifact ID. They are special dependency that typically do not have any library code themselves, but instead transitively pull in other libraries. The starter dependency pattern insures that the build file will be significantly smaller and easier to manage because we don't need to declare dependencies on all libraries needed. It allows to think of dependencies in terms of what capabilities they provide, rather than in term of library names. Also, no library version specification is necessary. Spring Boot will come with specific values and will insure they are transitively compatible. The only version that needs to be specified is the Spring Boot version.
'org.springframework.boot:spring-boot-starter' is included in all other starters.
How do I see what's in a starter?
Example:
dependencies {
implementation('org.springframework.boot:spring-boot-starter-web')
testImplementation('org.springframework.boot:spring-boot-starter-test')
}
Persistence Initialization
schema.sql
If there is a file named "schema.sql" in the root of the application's classpath, then Spring Boot will execute the SQL in that file against the database when the application starts and create necessary tables. To build the file into the application JAR, place it in src/main/resources.
data.sql
Also, if there is a file named "data.sql" in the root of the application's classpath, then Spring Boot will execute the SQL in that file against the database when the application starts and will populate the tables created with schema.sql. To build the file into the application JAR, place it in src/main/resources.
Caching
Spring Boot automatically configures a suitable cache manager to serve as provider for the relevant cache.
TODO: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-caching.html. Also see Spring_Framework#Cache.
Spring Boot Plugins
Spring Boot Gradle Plugin
Spring Boot Maven Plugin
IntelliJ Support
Testing
Process: https://spring.io/blog/2016/04/15/testing-improvements-in-spring-boot-1-4
Use:
dependencies {
testImplementation('org.springframework.boot:spring-boot-starter-test')
}
@RunWith(SpringRunner.class)
@IntegrationTest, @WebIntegrationTest
Testing Logging Configuration
By default, test logging is executed by Logback.
Spring Boot Mockito Support
Actuator
Spring Boot CLI
Spring Boot Main Class
The Spring Boot main class, also know as the bootstrap class, bootstraps the project. It lives under src/main/java. It is a class that contains a main method to be executed when the Spring Boot JAR artifact is run. The class is annotated with @SpringBootApplication.
package ...;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ExampleApplication {
public static void main(String[] args) {
// SpringApplication performs the bootstrapping of the application.
// It gets a configuration class and the command-line arguments.
SpringApplication.run(ExampleApplication.class, args);
}
}