1.
Spring Core
Dependency Injection /inversion of control
Without dependency injection , we have to create the objects that we
want in our application using the new keyword.
Class is created
Created class used
If we want to use the class we created, we have to instantiate it
using the key word. The main method depends on the Alien
class. The solution is dependency injection
The goal of the dependency injection is to remove this
dependency by separating the usage from the creation of the
object
Lets fix it
SpringApplication is a class implementing
@component converts the POJO in to spring bean which are ConfigurableApplicationContext interface which is a
classes managed by spring bean container that creates and subinterface of superinterface ApplicationContext
provides the objects that we need at run time The main application is not responsible for creating the alien object. The task is
delegated to spring bean container.
What if the alien class depends on another object
Here, even if we put @Component annotation on the laptop class it does not work without the new keyword because we are not using getBean method. The solution is to use @Autowire annotation
Types of dependency injection
1. Constructor injection Dependency Injection in Spring can be done through
constructors, setters or fields. Interface injection is
2. Setter or field injection not used by spring
3. Interface injection
You should use constructor injection for mandatory
dependencies and setter injections for optional
dependencies instead of field injection. Some reasons
why
• It makes it clear to everybody which dependencies are
required
• It makes testing easier
• You can make your objects immutable
2. Spring Boot JPA
Dependency Required : spring-boot-starter-data-jpa, once you add this
dependency it is compulsory to add data connection
2. 1. In the application properties , setup connection. Photo app is the
name of the database
server.port=2029
spring.datasource.username=erko
spring.datasource.password=2019Sqlserver%
spring.datasource.url = jdbc:mysql://localhost:3306/photo_app Minimum requirement without security
Spring.jpa.hibernate.ddl-auto=update
spring.datasource.hikari.maximum-pool-size=10
2.3 The Model
Relationship
Agent Cutomer
List of customers Agent FK
List of Orders List of orders FK
FK
Order
Agent FK
Customer FK
FK
2.3 The model, JsonIgnoreProperties
Agent Order
Customer
When you are listing the customer model in agents, you have
to ignore the relationship properties otherwise you will end up
in an infinite loop. For example, every time you place the
customer model class in another class, you have to ignore
agent and orders which are the properties of the customer
model but are by themselves models /classes.
2.3 The model: here are the models
2.3 The model: constructor
• Default constructor is required
• List properties are not included in the constructor
• But objects ( the many side object properties are included)
• Generate getters and setters for everything
2.3 Create an interface to your table