Saturday, March 5, 2022

Introduction to Spring Boot

@SpringBootApplication

• It enables component scanning, that is, looking for Spring components and configuration classes in the package of the application class and all its sub-packages.

• The application class itself becomes a configuration class.

• It enables autoconfiguration, where Spring Boot looks for JAR files in the classpath that it can configure automatically.


Component scanning

I prefer using constructor injection (over field and setter injection) to keep the state in my components immutable. An immutable state is important if you want to be able to run the component in a multithreaded runtime environment.


public class AnotherComponent {

private final MyComponent myComponent;

@Autowired
public AnotherComponent(MyComponent myComponent) {

this.myComponent = myComponent;

}

If we want to use components that are declared in a package outside the application's package, for example, a utility component shared by multiple Spring Boot applications, we can complement the @SpringBootApplication annotation in the application class with a @ComponentScan annotation:

package se.magnus.myapp;

@SpringBootApplication
@ComponentScan({"se.magnus.myapp","se.magnus.utils"})
public class MyApplication {

We can now auto-wire components from the se.magnus.util package in the application code, for example, a utility component named MyUtility, as follows:

package se.magnus.utils;

@Component
public class MyUtility { ...

This utility component can be auto-wired in an application component like so:

package se.magnus.myapp.services;

public class AnotherComponent {

private final MyUtility myUtility;

@Autowired
public AnotherComponent(MyUtility myUtility) {
 this.myUtility = myUtility;
 }

If we want to switch from Netty to Tomcat as our embedded web server, we can 
override the default configuration by excluding Netty from the starter dependency 
and adding the starter dependency for Tomcat:
implementation('org.springframework.boot:spring-boot-starter-webflux') 
{
exclude group: 'org.springframework.boot', module: 'spring-boot-
 starter-reactor-netty'
}
implementation('org.springframework.boot:spring-boot-starter-tomcat')

Spring Data

Spring Data comes with a common programming model for persisting data in 
various types of database engines, ranging from traditional relational databases (SQL 
databases) to various types of NoSQL database engines, such as document databases 
(for example, MongoDB), key-value databases (for example, Redis), and graph 
databases (for example, Neo4J)

The two core concepts of the programming model in Spring Data are entities and 
repositories. Entities and repositories generalize how data is stored and accessed 
from the various types of databases.

Entity
An entity describes the data that will be stored by Spring Data.

an entity that will be stored in a relational database can be annotated with JPA annotations
@Entity
@IdClass(ReviewEntityPK.class)
@Table(name = "review")
public class ReviewEntity {
@Id private int productId;
@Id private int reviewId;
private String author;
private String subject;

If an entity is to be stored in a MongoDB database, annotations from the Spring Data 
MongoDB subproject can be used together with generic Spring Data annotations.

@Document
public class RecommendationEntity {
 @Id
 private String id;
 @Version
 private int version;
 private int productId;
 private int recommendationId;
 private String author;
 private int rate;

Repositories
Repositories are used to store and access data from different types of databases. In 
its most basic form, a repository can be declared as a Java interface

To specify a repository for handling the JPA entity ReviewEntity we only need to 
declare the following:

public interface ReviewRepository extends CrudRepository<ReviewEntity, ReviewEntityPK> {
 
 Collection<ReviewEntity> findByProductId(int productId);
}

In this example we use a class, ReviewEntityPK, to describe a composite primary key. 

public class ReviewEntityPK implements Serializable {
 public int productId;
 public int reviewId;
}




No comments:

Post a Comment