- Spring Boot
The Spring Boot is a solution to make it easier to get started with Spring development:
- Minimize the amount of manual configuration for us by performing auto-configuration based on props files and JAR classpath.
- Help to resolve dependecy conflicts (even Maven or Gradle).
- Provide an embedded HTTP server (Tomcat, Jetty, Undertow, ...) and we don't need to install a server separately.
The Spring Initializr is the website provided by Spring Boot project, it help developers quickly create a starter Spring project.
We can setup the project information and download the *.zip
file. The file can be imported with any IDE or editor as we like. There are something to be awared when importing with Eclipse:
- Add
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
in thepom.xml
and update project with Maven. - Choose [Run As] > [Java Application] (not "Run on Server") to run the application.
There are two options for running the application with command line:
- Use Java
- Use Spring Boot Maven Plugin
- No need to have Maven installed or present on the path.
- If correct version of Maven is Not found on the computer, it will automatically download Maven.
- If we already have Maven installed previously, we can ignore or delete the
mvnw
files.
# [Option 1] Java
$ java -jar <NAME_OF_JAR>.jar
# [Option 2] Spring Boot Maven Plugin
$ mvnw clean compile test
$ mvnw package
$ mvnw spring-boot:run
By default, Spring Boot reads information from a standard properties file loacted at ./src/main/resources/application.properties
. We can define ANY custom properties in the file and then access these properties using @Value
annotations in application.
-
Define Custom Application Properties
# Define Custom Properties coach.name=Mickey Mouse team.name=The Mouse Club
-
Inject Properties into Spring Boot App
@RestController
public class FunRestController {
// inject properties for: coach.name and team.name
@Value("${coach.name}")
private String coachName;
@Value("${team.name}")
private String teamName;
...
}
Spring Data JPA create a DAO (Data Access Object) and just plug in our entity type and primary key. Then the Spring will give us a CRUD implementation for minimizing boiler-plate DAO code.
- Spring Data JPA provides the interface
JpaRepository
. - It exposes
findAll()
,findById()
,save()
,deleteById()
and others.
Thymeleaf is a Java templating engine commonly used to generate the HTML views for web applications. Moreover, it's a general purpose templating engine that can also used outside of web applications.
- Can be an HTML page with some Thymeleaf expressions.
- Include dynamic content from Thymeleaf expressions.
- Also can be used as Email Templates, CSV Template and PDF Template.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Spring will auto-configure to use Thymeleaf since we have Thymeleaf dependency in Maven POM.
@Controller
public class DemoController {
@GetMapping("/")
public String sayHello(Model theModel) {
theModel.addAttribute("theData", new java.util.Date());
return "helloworld";
}
}
- In Spring, the Thymeleaf template files go in
./src/main/resources/templates
. - For web applications, Thymeleaf templates have a
*.html
extension. e.g.helloworld.html
.
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head> ... </head>
<body>
<p th:text="'Time on the server is ' + ${theDate}" />
</body>
</html>