Tags

  • Required knowledge of dependency injection. Preferably Spring, but can be CDI as well.
  • “Any time spent writing configuration is time spent not writing application logic” – (WALLS, Craig – Spring Boot In Action)

What it is?

It is a framework that eases the configuration of a Spring-based application. You can download the dependency of Spring Boot and its related projects as any other dependency of Maven / Gradle, but you can create a project configured using the site: start.spring.io/ (Note: For better visualization, click Switch to the Full version.)

Basic Concepts

  • The created project with Spring Boot can be in JAR or WAR format. If it is JAR and Web, Spring Boot starts an embedded Tomcat each time it is started.
  • Spring Boot needs a class with a method main that executes the method SpringApplication.run
  • By default, The @SpringBootApplication annotation is used in this class. Basically it is an annotation that adds other annotations:
    • @Configuration – It marks the class as a bean definition source.
    • @EnableAutoConfiguration – Add some beans based on Classpath and other settings.
    • @ComponentScan – Tells Spring where to look for other components, configurations, and services.
    • @EnableWebMvc – If spring-webmvc is on classpath, this annotation is also added, with all Spring MVC configuration.
  • If the class that runs SpringApplication.run (let’s call it MainApplication) is in the com.conscious.it package, it’s not necessary to create the beans with default constructor. Spring maps all packages and subpackages from MainApplication package.

Practice

  • Whoever used Spring with XML configuration will notice a big difference. Whoever used Spring with configuration in annotations will already know a little better.
  • With a Spring Boot project created, it is possible to create a Controller (remembering that it should be created in a sub-package where MainApplication is). The simplest example, a Hello World via Rest:
  • Our MainApplication that runs SpringApplication.run

  • So when running this class and/or this generated JAR, by default, Spring Boot sets the Context Path to ‘/‘ and the port to 8080. So when starting a Spring Boot project, by default it is accessible in http://localhost:8080
  • You can change these settings with the following properties in application.properties

Spring Data JPA

  • We have Spring Data JPA and H2 in our classpath. So, let’s create our CRUD:

  • CRUD created. Yes, that’s right. You don’t have to implement the CRUD methods. If you don’t know Spring Data JPA, some useful informations:
    • CrudRepository<User, Integer> – The first value (User) is the object to be saved on database, annotated with @Entity, and the second value is the type of the id defined with @Id in the object. With this, this interface can be injected and you already can use it. The main methods available will be shown next.
    • save – Saves or updates the object in the database
    • findAll – Returns all saved objects
    • findOne – Returns a object by Id
    • delete – Deletes a object in the database
    • count – Counts how many objects have in the database
    • exists – Returns if the object exists, by Id
  • Datasource configuration

  • This is the only configuration you need. Relevant information: Note that you do not need to declare the H2 Driver class. Spring Boot is smart enough to know by Classpath. If you want to keep the data when you turn off the application, just use the spring.jpa.hibernate.ddl-auto=update configuration, because the default value is create-drop.

Controller Without REST

  • Spring Boot with Web has the following mapped directories:
    • src/main/resources/static – Where static files such as JS, fonts and CSS should be.
    • src/main/resources/templates – Where the pages should be.
  • In my sample project, I created two basic HTMLs. Since we use Thymeleaf, we use HTML for views. One of the great advantages of Thymeleaf is to be simple, so when adding a user to the screen, the HTML would look like this:

That’s it, folks. On the next post, I’ll talk more about Spring Boot configurations, Live Reload, Actuator, Spring Boot tests, how to change a Spring Boot JAR project to WAR and a lot more. If you want to read more subjects of the blog, read the blog track: Tracks – You might have lost 01

Advertisements