Skip to main content

Spring Boot

References

Terminology

  • DAO: Data Access Object
  • POJO: Plain Old Java Object

General Notes

Modules

  • Core module

    • provides the dependency injection
  • AOP module

    • aspect oriented programming module
    • define method interceptors
  • DAO module

    • abstraction layer to creating a connection and releasing it
  • ORM module

    • provides integration with object relational mapping tools like Hibernate
  • JEE module

    • connections to systems
  • Web module

    • MVC framework to facilitate building web applications

org.springframework.context.ApplicationContext

  • represents the Spring IoC (Inversion of Control) container and is responsible for instantiating, configuring, and assembling the beans.
  • reads configuration metadata to determine what to work with
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");

Spring Boot (Udemy Course Notes)

Development Environment

  • Java JDK: 1.8 or higher

  • Maven: 3.2.5 or higher

    • configured for command line use
  • Gradle: 3.4.1 or higher

    • configured for command line use
  • IDE: IntelliJ IDEA

Verify Installations

At the command line, run each of these to test for successful installation

java -version //Java
javac -version //JDK
mvn -v
gradle -v

See the Development Environment setup page for info on setting up each.

Starting a Project

You can start with the Spring Initializr (https://start.spring.io/)

You can also use the project starters (https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-starter)

JPA

Java persistence API (Jakarta Persistence as of 2019 - https://jakarta.ee/specifications/persistence/2.2/)

  • describes the relational information of models
  • entity relationship mapping
  • when creating model classes, JPA requires a zero arg constructor. You can have a constructor with arguments but you also need the zero arg constructor

Mappings Basics

@Entity
public class Author {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;

@ManyToMany(mappedBy="authors")
private Set<Book> books;
}

In mappedBy=”authors”, authors is a reference to a property in the Books class.

@Entity
public class Book {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;

@ManyToMany
@JoinTable(name="author_book", joinColumns=@JoinColumn(name="book_id), inverseJoinColumns=@JoinColumn(name="author_id"))
private Set<Author> authors;
}

Metadata

  • @OneToMany

  • @ManyToOne

  • @SpringBootApplication

  • @Override

  • @Controller

    • Added to a controller class so Spring knows to inspect it and treat it as a controller
  • @RequestMapping(“/books”)

Turn on the H2 Console

  • The H2 Console is off by default. Turning it on will allow output to the console and gives us access to the H2 DB console interface
  • In application.properties, add spring.h2.console.enabled=true

Model Templating

  • Thymeleaf

    • https://www.thymeleaf.org/

    • An alternate to JSP

    • You can view the template naturally in the browser

    • You will need to include the Thymeleaf spring boot starter to the dependencies in order for this to work

      • spring-boot-starter-thymeleaf
  • Mustache

  • Handlebars

  • FreeMarker

Project Lombok

Java annotation library that provides constructors, getters/setters, and other information about your Java classes and objects.