7
ď§ Just goto https://start.spring.io/
Creating Spring Boot Project
8.
8
ď§ Additional setof tools that can make the application
development faster and more enjoyable
Spring Dev Tools
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
pom.xml
11
ď§ Four maincomponents:
ď§ Spring Boot Starters - combine a group of common or related
dependencies into single dependency
ď§ Spring Boot AutoConfigurator - reduce the Spring Configuration
ď§ Spring Boot CLI - run and test Spring Boot
applications from command prompt
ď§ Spring Boot Actuator â provides EndPoints and
Metrics
Spring Boot Main Components
12.
12
Spring Boot Starters(1)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
pom.xml
15
ď§ Command LineInterface - Spring Boot software to run and test
Spring Boot applications
Spring Boot CLI
16.
16
ď§ Expose differenttypes of information about the running
application
Spring Boot Actuator
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
pom.xml
17.
17
ď§ Spring providesInversion of Control and Dependency Injection
Inversion of Control
//Tradiotional Way
public class UserServiceImpl
implements UserService {
private UserRepository
userRepository = new
UserRepository();
}
UserServiceImpl.java
//Dependency Injection
@Service
public class UserServiceImpl
implements UserService {
@Autowired
private UserRepository
userRepository;
}
UserServiceImpl.java
18.
18
Spring IoC
Meta Data:
1.XML Config
2. Java Config
3. Annotation Config
Automatic Beans:
1. @Component
2. @Service
3. @Repository
Fully Configured System
Explicit Beans
1. @Bean
IoC
19.
19
ď§ Object thatis instantiated, assembled, and otherwise managed
by a Spring IoC container
Beans
public class Dog implements Animal {
private String name;
public Dog() {}
//GETTERS AND SETTERS
}
Dog.java
21
Get Bean fromApplication Context
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
ApplicationContext context =
SpringApplication.run(MainApplication.class, args);
Animal dog = context.getBean(Dog.class);
System.out.println("DOG: " +
dog.getClass().getSimpleName());
}
}
MainApplication.java
22.
22
Bean Lifecycle
Instantiation SetProperties Set Name
Set Application
Context
Pre Initialization
Initialization
Post Initialization Bean is ready Bean is destroyed
When Container
is Shutdown
23.
23
Bean Lifecycle Demo(1)
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
ApplicationContext context =
SpringApplication.run(MainApplication.class, args);
((AbstractApplicationContext)context).close();
}
@Bean(destroyMethod = "destroy", initMethod =
"init")
public Animal getDog(){
return new Dog();
MainApplication.java
24.
24
Bean Lifecycle Demo(2)
public class Dog implements Animal {
public Dog() {
System.out.println("Instantiation");
}
public void init(){
System.out.println("Initializing..");
}
public void destroy(){
System.out.println("Destroying..");
}
}
MainApplication.java
25.
25
ď§ The defaultone is Singleton. It is easy to change to Prototype
Bean Scope
Request A
Request B
Request C
Request A
Request B
Request C
Dog
Dog 1
Dog 2
Dog 3
Singleton Prototype
Mostly used
as State-full
Mostly used
as State-less
28
Execute
Action
ď§ Model-view-controller (MVC)framework is designed around a
DispatcherServlet that dispatches requests to handlers
What is Spring MVC?
Dispatcher
Servlet
Handler
Mapping
Handler
Adapter
View
Resolver
Controller
View Name
Model
View
Service
Repository
DB
Request
Find
Controller
Resolve
View
Result
Model
Result
Model
Business Logic
Response
29.
29
Response
(html, json, xml)
MVCâ Control Flow
Web Client
Model
Controller
View
Request
User Action
Update
Model
Notify
Update
View
30.
30
Controllers
@Controller
public class DogController{
@GetMapping("/dog")
@ResponseBody
public String getDogHomePage(){
return "I am a dog page";
}
}
DogController.java
Controller
Request Mapping
Action
Text
Print Text
31.
31
Actions â GetRequests
31
@Controller
public class CatController {
@GetMapping("/cat")
public String getHomeCatPage(){
return "cat-page.html";
}
}
CatController.java
Request Mapping
Action
View
32.
32
Actions â PostRequests (1)
@Controller
@RequestMapping("/cat")
public class CatController {
@GetMapping("")
public String getHomeCatPage(){
return "new-cat.html";
}
}
CatController.java
Starting route
33.
33
Actions â PostRequests (1)
@Controller
@RequestMapping("/cat")
public class CatController {
@PostMapping("")
public String addCat(@RequestParam String catName,
@RequestParam int catAge){
System.out.println(String.format("Cat Name: %s,
Cat Age: %d", catName, catAge));
return "redirect:/cat";
}
}
CatController.java
Request param
Redirect
34.
34
Models and Views
@Controller
publicclass DogController {
@GetMapping("/dog")
public ModelAndView getDogHomePage(ModelAndView
modelAndView){
modelAndView.setViewName("dog-page.html");
return modelAndView;
}
}
DogController.java
Model and View
40
ď§ Entity isa lightweight persistence domain object
Entities
@Entity
@Table(name = "cats")
public class Cat {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
//GETTERS AND SETTERS
}
Cat.java
41.
41
ď§ Persistence layerthat works with entities
Repositories
@Repository
public interface CatRepository extends CrudRepository<Cat, Long>
{
}
CatRepository.java
42.
42
ď§ Business Layer.All the business logic is here.
Services
@Service
public class CatServiceImpl implements CatService {
@Autowired
private CatRepository catRepository;
@Override
public void buyCat(CatModel catModel) {
//TODO Implement the method
}
}
CatService.java
44
ď§ Spring Boot- Opinionated view of building
production-ready Spring applications
ď§ Spring MVC - MVC framework that has three
main components:
ď§ Controller - controls the application flow
ď§ View - presentation layer
ď§ Model - data component with the main logic
ď§ Spring Data - Responsible for database related
operations
Summary
License
ď§ This course(slides, examples, demos, videos, homework, etc.)
is licensed under the "Creative Commons
Attribution-NonCommercial-ShareAlike 4.0 International"
license
46
47.
Free Trainings @Software University
ď§ Software University Foundation â softuni.org
ď§ Software University â High-Quality Education,
Profession and Job for Software Developers
ď§ softuni.bg
ď§ Software University @ Facebook
ď§ facebook.com/SoftwareUniversity
ď§ Software University @ YouTube
ď§ youtube.com/SoftwareUniversity
ď§ Software University Forums â forum.softuni.bg
Editor's Notes
#28Â Blue Color â Provided by Spring
Purple Color â To Be Implemented by the Developer
Green Color â Provided by Spring. Sometimes implemented by the developer