Bootstrapping an Application
Estimated time to read: 4 minutes
Code Repository¶
For the rest of this course, it is recommended that you create a local git repository from the following URL: https://gitlab.com/videolearning/spring-fundamentals.
Note
If you are following the examples in this course, ensure that you load the module directory into you IDE and not the overall project directory, else the application will not build!
Scenario¶
In the current scenario, the Trackzilla application will need to be written from scratch. There are several tools that make this process easy:
Spring Initializer¶
The Spring Initializer is available at http://start.spring.io. The Initializer generates the base code, dependencies and Maven / Gradle configuration for you.
When adding dependencies to the Initializer for this course, add Spring-Web-Starter
.
Once complete, generate the project, extract the zip file that it produces and open the new folder within an IDE.
Within the Maven POM / Gradle build configuration, Spring Initializer will have set a 'parent' element for the spring-boot dependencies. This parent element allows specifying the Spring Boot version in one place, so that version tags can be omitted for each child dependency.
Main Method¶
The main class contains a main method with the @SpringBootApplication
annotation. This method is used to bootstrap and launch a Spring application from a Java main method.
The method typically delegates to Spring Boot's SpringApplication class by calling the run method.
package io.entityfour.fundamentals;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication // The @SpringBootApplication annotation bootstraps the application by starting Spring
public class FundamentalsApplication {
public static void main(String[] args) { // Standard Main method
SpringApplication.run(FundamentalsApplication.class, args); // The method delegates to Spring Boots, `SpringApplication` class by calling the `run()` method.
System.out.println("Hello World!");
}
}
Spring Boot CLI¶
The Spring Boot CLI can also be used as an alternative to initialize a project.
On a Mac, the Spring Boot CLI can be accessed by installing spring
via brew
. See Brew for MacOS for more information.
From the terminal, the following commands can be used to initalize a Spring project.
Groovy Scripts¶
Groovy scripts can also be run directly within the CLI.
For example, the code below creates a RESTful API that is mapped to the default path and returns a string of 'Hello World'.
To run the file, the following command can be used:
spring run app.groovy
Automatic Configuration¶
Automatic configuration is a very useful and powerful feature of Spring Boot, which takes a "convention-over-configuration" approach.
This is a huge time saver that can help boot productivity when building Java applications.
How it works¶
Automatic Configuration looks at the JAR files on the CLASSPATH and auto-configures beans automatically following the procedure below:
graph
A[Process </br> Finds JARs on the classpath </br> and auto-configures the bean]
B[Auto-Configure </br> Automatically configures </br> elements based on their type]
For example, it will automatically configure a data source if a Hibernate
JAR is found on the CLASSPATH or it could also automatically configure a DispatcherServlet
if a Spring MVC JAR is found on the CLASSPATH.
Non-Invasive¶
Automatic configuration is designed to be non-invasive. For example, if a Bean, such as a datasource bean, is provided by the application or developer, the default embedded support backs away and the provided source is used instead.
Automatic configuration is always applied after all user defined Beans have been registered.
Insights¶
To check what automatic configuration is currently being applied, and why, then the application can be started with a ---debug
switch.
Alternatively, the Spring Boot actuator can be used. See TAVISTOFIXLINK to 107 - Actuators
Alternatively, debug logging an be enabled by adding a property value to the application.properties
file. For example:
Annotations¶
The @SpringBootApplication
annotation has to be added to the Main application class, to tell Java that the application should utilize Spring Boot. It is a shortcut annotation that applies three annotations in one statement.
@SpringBootApplication
applies the following annotations:
@SpringBootConfiguration
-
Replaces
@Configuration
and annotates ta class as a configuration -
@EnableAutoConfiguration
-
Tells Spring Boot to configure beans
-
@ComponentScan
- Tells Spring Boot to scan the current package and subpackages for components
Application Properties¶
The application.properties
file can contain many values depending on the use case of the application. This can include log settings, datasource integration, spring settings, etc.
See Common Application Properties for more information.
Profiles¶
Spring Boot profiles are fundamental to the Spring framework.
Assuming there is an application the needs to be deployed to different environments, the application configuration in each of these environments will be different.
For example, when running in development, the application should connect to a development database. In production, the application should connect to the production database.
graph
A[Application]
B[Development]
C[Test]
D[Production]
BD[Dev Database]
CD[Test Database]
DD[Production Database]
A --> B
B --> BD
A --> C
C --> CD
A --> D
D --> DD
Profile Configuration¶
Spring Boot supports all of the Spring profile configurations with a few additional features, such as:
spring.profiles.active = dev // Used to define the currently active profile
application-{profile}.properties // Profile specific property files. These have to be named following the syntax in this example
application-dev.properties // Development Profile
application-test.properties // Test Profile
application-prod.properties // Production Profile
Spring Boot will automatically load the properties in an application.properties file for all profiles, then the ones in profile-specific properties files.
graph
A[application.properties]
B[application-dev.properties]
C[application-test.properties]
D[application-prod.properties]
AA(Start) --> |Load| A
A --> AB(Append)
AB --> |Is dev?| B
AB --> |Is test?| C
AB --> |Is prod?| D