|
1 |
| -# General Concepts |
2 |
| - |
3 |
| -## Table of contents |
4 |
| - |
5 |
| -- [Objective](#-objective) |
6 |
| -- [What's spring & why to use it?](#whats-spring--why-to-use-it) |
7 |
| -- [Spring Goals](#spring-goals) |
8 |
| -- [Spring Architecture](#spring-architecture) |
9 |
| -- [dependency injection & inversion of control](#dependency-injection--inversion-of-control) |
10 |
| -- [Spring Container](#spring-container) |
11 |
| -- [Spring Bean](#spring-bean) |
12 |
| -- [Bean Scope](#bean-scope) |
13 |
| -- [Bean life cycle](#bean-life-cycle) |
14 |
| -- [Aspect-oriented programming](#aspect-oriented-programming-aop) |
15 |
| -- [Model-view-controller](#model-view-controller-mvc) |
16 |
| -- [Data Access Objects (DAO)](#data-access-objects-dao) |
17 |
| -- [Code Samples Structure](#code-samples-structure) |
18 |
| -- [References](#-references) |
| 1 | +# Table of Contents |
| 2 | + |
| 3 | +- [Objective](#objective) |
| 4 | +- [What is the spring Framework?](#what-is-the-spring-framework) |
| 5 | +- [Why Spring framework came out?](#why-spring-framework-came-out) |
| 6 | +- [What is spring architecture?](#what-is-spring-architecture) |
| 7 | +- [Spring Beans](#spring-beans) |
| 8 | + - [What is a bean?](#what-is-a-bean) |
| 9 | + - [What is bean scope?](#what-is-bean-scope) |
| 10 | + - [What is bean life-cycle?](#what-is-bean-life-cycle) |
| 11 | +- [What is dependency injection & inversion of control?](#what-is-dependency-injection-and-inversion-of-control) |
| 12 | +- [What is aspect-oriented programming?](#what-is-aspect-oriented-programming) |
| 13 | +- [Hibernate](#hibernate) |
| 14 | + - [What is hibernate?](#what-is-hibernate) |
| 15 | + - [What is an entity life-cycle?](#what-is-an-entity-life-cycle) |
| 16 | + - [What is cascading and their types?](#what-is-cascading-and-their-types) |
| 17 | + - [Difference between eager and lazy loading](#difference-between-eager-and-lazy-loading) |
| 18 | +- [Resources](#resources) |
19 | 19 |
|
20 | 20 | <img src="https://spring.io/images/OG-Spring.png" title="spring logo, src:spring.io" width="100%" height="500">
|
21 | 21 |
|
22 |
| ---- |
| 22 | +## Objective |
23 | 23 |
|
24 |
| -## 🎯 Objective |
| 24 | +- grasp spring concepts |
| 25 | +- learn the theory behind the Spring framework |
25 | 26 |
|
26 |
| -1. Learn the theory behind spring |
27 |
| -2. grasp general concepts of spring |
| 27 | +## What is the Spring Framework? |
28 | 28 |
|
| 29 | +- it's a web framework for building web apps using java |
| 30 | +- released in 2004 |
29 | 31 |
|
30 |
| -### What's spring & why to use it? |
| 32 | +## Why Spring framework came out? |
31 | 33 |
|
32 |
| -- spring is a java framework for building web applications and it was released back in 2004 |
33 |
| -- spring came out to solve some problems in J2EE (Jakarta enterprise edition) |
34 |
| - 1. Enterprise java bean (JEB) and their complex configurations |
35 |
| - 2. poor performance |
36 |
| -- therefore, we can generally say that spring is a simplified version for building java enterprise applications |
| 34 | +- to solve complex configurations in a java enterprise edition that uses java enterprise beans (EJB) |
| 35 | +- also to enhance poor performance |
| 36 | +- create loosely coupled software |
37 | 37 |
|
38 |
| -### Spring Goals |
| 38 | +## What is spring architecture? |
39 | 39 |
|
40 |
| -- lightweight development using POJO |
41 |
| -- loosely coupled app using dependency injection |
42 |
| -- declarative programming using AOP |
43 |
| -- Minimize boilerplate code |
| 40 | +- spring introduces a layered architecture; each layer contains a set of modules (~20 modules) |
| 41 | +- through this layered architecture spring achieved one-stop-shop philosophy |
| 42 | +- Modules: |
| 43 | + 1. Core: contains dependency injection and inversion of control which uses a factory design pattern |
| 44 | + 2. Aspect-oriented programming: to remove cross-cutting concerns |
| 45 | + 3. Data access: contains JDBC, ORM |
| 46 | + 4. Web: contains servlets, web sockets |
44 | 47 |
|
45 |
| -### Spring Architecture |
| 48 | +## Spring beans |
46 | 49 |
|
47 |
| -- spring was introduced through a layered architecture, each layer contains a set of modules (~20 modules) and all these modules are on top of the core layer, spring contains a spread set of layers so it uses **one-stop-shop philosophy** |
48 |
| -- Spring modules |
49 |
| - 1. core: contains dependency injection feature which uses factory pattern to create beans in the project |
50 |
| - 2. Aspect-oriented programming (AOP) |
51 |
| - 3. Data Access Object |
52 |
| - 4. Object-relational mapping |
53 |
| - 5. Java Enterprise Edition (JEE) |
54 |
| - 6. Web: contains MVC, web sockets |
| 50 | +### What is a bean? |
55 | 51 |
|
56 |
| -### Dependency injection & Inversion of control |
| 52 | +- Simply, a bean is a java object which is created and destroyed through spring IOC and DI |
57 | 53 |
|
58 |
| -- Let say you create class A and this class take an object of class B as a parameter for the constructor, this hierarchy makes a dependency (class A depend upon class B), the problem arises when you have a set of classes and each class depend upon another set of classes this will make your software highly coupled (bad architecture) as some parts will be collapsed when we change something |
59 |
| - |
60 |
| -```java |
61 |
| -class B { |
62 |
| - // Some code |
63 |
| -} |
64 |
| - |
65 |
| -class A { |
66 |
| - B obj; |
67 |
| - public A(B obj) { |
68 |
| - this.obj = obj; |
69 |
| - } |
70 |
| -} |
71 |
| -``` |
72 |
| - |
73 |
| -- in order to, make software loosely coupled we need to apply: |
74 |
| - 1. Inversion of control principle: to create and manage objects |
75 |
| - 2. Dependency injection principle: to inject required dependencies for each object |
76 |
| - |
77 |
| -### Spring container |
78 |
| - |
79 |
| -- it's one of the core components of spring framework which is responsible for assembling and maintaining objects from creation till destruction and it uses dependency injection (DI) to build a certain object |
80 |
| - |
81 |
| -### Spring Bean |
82 |
| - |
83 |
| -- basically it's a java object which is created and maintained through spring IOC container and it forms the backbone for any spring application |
84 |
| - |
85 |
| -### Bean Scope |
| 54 | +### What is bean scope? |
86 | 55 |
|
87 | 56 | - bean scope is responsible for deciding:
|
88 | 57 |
|
89 |
| - 1. How long the bean will live? |
| 58 | + 1. How long will the bean live? |
90 | 59 | 2. How many instances will be created?
|
91 | 60 |
|
92 | 61 | | Scope | Description |
|
93 |
| - | :------------: | :------------------------------------------------------ | |
94 |
| - | Singleton | Default scope, create one instance per spring container | |
95 |
| - | Prototype | Create new instance every time bean is requested | |
| 62 | + | :------------: |:------------------------------------------------------: | |
| 63 | + | Singleton | Default scope, create one instance per Spring container | |
| 64 | + | Prototype | Create a new instance every time the bean is requested | |
96 | 65 | | Request | Single bean instance per HTTP request |
|
97 | 66 | | Session | Single bean instance per HTTP session |
|
98 | 67 | | Global-session | Single bean instance per global HTTP session |
|
99 | 68 |
|
100 |
| -### Bean life cycle |
| 69 | +### What is bean life-cycle? |
101 | 70 |
|
102 |
| - <img src="https://media.geeksforgeeks.org/wp-content/uploads/20200428011831/Bean-Life-Cycle-Process-flow3.png" title="bean life cycle src:geeksforgeeks.org" width="100%" height="500"> |
| 71 | +<img src="https://media.geeksforgeeks.org/wp-content/uploads/20200428011831/Bean-Life-Cycle-Process-flow3.png" title="bean life cycle src:geeksforgeeks.org" width="100%" height="500"> |
103 | 72 |
|
104 |
| -### Aspect-oriented programming (AOP) |
| 73 | +## What is Dependency Injection and Inversion of control? |
105 | 74 |
|
106 |
| -- it's a programming paradigm that uses the separation of concerns cross-cutting principle to increase modularity which consequently decreases coupling |
107 |
| -- **Eg.** if we define 5 classes each one contains a logger object |
108 |
| -- How to apply AOP ? |
109 |
| - 1. define point-cut (logger, security), points where cross-cutting concerns are applied |
110 |
| - 2. create aspect for each point-cut (logger aspect, security aspect) which defines what will this aspect do? |
111 |
| - 3. create aspect configurations, which defines when and where will those aspects execute? |
| 75 | +- It's a process of delegating instantiation and assembling objects into: |
| 76 | + 1. XML files |
| 77 | + 2. XML using annotations |
| 78 | + 3. other java code |
| 79 | +- and to inject object dependencies you have to use: |
| 80 | + 1. Constructor injection |
| 81 | + 2. Setter injection |
112 | 82 |
|
113 |
| -### Model-view-controller (MVC) |
| 83 | +## What is aspect-oriented programming? |
114 | 84 |
|
115 |
| - <img src="https://qph.fs.quoracdn.net/main-qimg-c3f2bef1693561dfb24e0f00aa592c80" title="MVC, src:quora.com" width="75%" height="300" style="float:right;"> |
116 |
| - |
117 |
| - *Ideal MVC* |
| 85 | +- it's a programming paradigm that aims to increase modularity by separating cross-cutting concerns |
| 86 | +- Steps: |
| 87 | + 1. Define point-cut (logger, security) "cross-cutting concern" |
| 88 | + 2. Define those points into something called aspect |
| 89 | + 3. Define configuration for each aspect (when-where you want to call this aspect) |
118 | 90 |
|
119 |
| -- it's a software architectural pattern in which your software is separated into 3 parts: |
120 |
| - |
121 |
| - 1. Model: it's a component that handles anything related to data in the software |
122 |
| - 2. View: it's a component that deals with UI related stuff |
123 |
| - 3. Controller: it's a component that manages flow between the other two components and handles any business logic in the software |
| 91 | +## Hibernate |
124 | 92 |
|
125 |
| - <img src="/diagrams/spring-mvc.png" title="Spring MVC" width="75%" height="300" style="float:right;"> |
126 |
| - |
127 |
| - *Spring MVC* |
128 |
| - |
129 |
| -### Data Access Objects (DAO) |
| 93 | +### What is hibernate? |
130 | 94 |
|
131 |
| -- it's a design pattern that provides an interface that provides access to underlying databases throughout some (CRUD) operations |
| 95 | +- It's an object-relational mapping that relies on a layer in between app and database to handle conversion between tables and classes |
| 96 | +- Java App ⇒ hibernate ⇒ JDBC ⇒ DB |
132 | 97 |
|
133 |
| -```java |
134 |
| -public class Employee { |
135 |
| - private String name; |
136 |
| - public void setName(String name) { |
137 |
| - this.name = name; |
138 |
| - } |
139 |
| - public String getName() { |
140 |
| - return this.name; |
141 |
| - } |
142 |
| -} |
| 98 | +### What is an entity life-cycle? |
143 | 99 |
|
144 |
| -interface EmployeeDAO { |
145 |
| - List<Employee> getAllNames(); |
146 |
| -} |
147 |
| -``` |
| 100 | +1. transient (un-tracked) |
| 101 | +2. persist (attached to the database so any change that happens will reflect on the database) |
| 102 | +3. detached |
| 103 | +4. removed |
148 | 104 |
|
| 105 | +### What is cascading and their types? |
149 | 106 |
|
150 |
| ---- |
| 107 | +- Apply the same changes-operations to related entities |
| 108 | +- Types: |
| 109 | + 1. Save |
| 110 | + 2. Delete |
| 111 | + 3. Detach |
| 112 | + 4. All |
| 113 | + 5. Remove |
| 114 | + 6. merge |
151 | 115 |
|
152 |
| -### Code samples structure |
153 |
| - <img src="/diagrams/class-diagram.png" title="Class diagram" width="100%" height="400"> |
154 |
| - |
155 |
| - *Class Diagram* |
156 |
| - |
157 |
| - <img src="/diagrams/spring-components.png" title="Spring components" width="100%" height="400"> |
158 |
| - |
159 |
| - *Spring Components* |
160 |
| - |
161 |
| - > **_Note:_** This is not the optimal structure and you may find some little changes in some examples for simplification purposes |
| 116 | +### Difference between eager and lazy loading |
162 | 117 |
|
| 118 | +- Eager: loads all data at once |
| 119 | +- Lazy: loads data whenever it's required-requested |
163 | 120 |
|
164 |
| -## 🔗 References |
| 121 | +## Resources |
165 | 122 |
|
166 |
| -1. [Spring & Hibernate for Beginners (includes spring boot)](https://www.udemy.com/course/spring-hibernate-tutorial/) |
167 |
| -2. [Web development with java spring framework](https://www.coursera.org/learn/web-development-with-java-spring-framework/home/welcome) |
| 123 | +- [Coursera](https://www.coursera.org/learn/web-development-with-java-spring-framework/home/welcome) |
| 124 | +- [Udemy](https://www.udemy.com/course/spring-hibernate-tutorial/) |
0 commit comments