Fundamental Spring Boot: Keep it Simple, Get it Right, Be Productive and Have Fun!

VMware Tanzu
VMware TanzuVMware Tanzu
Fundamental Spring Boot:
Keep it Simple, Get it Right,
Be Productive and Have Fun
Wei Li
09/02/2020
SpringOne 2020
22 2
About Me
Spring framework user
since version 1.0
Curious learner
Savvy runner
Problem solver
Wei Li​
Solutions Engineer,​
Liberty Mutual Insurance
33 3
Spring Boot in a Nutshell
Dependency management
High productivity
Quality and cloud native
applications
Auto and conditional
configuration
Convention over
configuration
Rich non-functional
features and tooling
Happy developers
44 4
About This Talk - Spring Boot Fundamental
Concepts
Application properties Manage beans
Bean scopeDependency injection
Post startup processingTroubleshooting tips
Be productive
55 5
Application Properties
One property file per
environment Validate properties
Make properties type-safe
(properties => POJO)
Favor YAML file
66 6
Application Properties – Favor YAML File
YAML Ain't Markup Language
77 7
Application Properties – One File per Environment
Activate
88 8
Application Properties – Default to Local Profile
99 9
Application Properties – Avoid using @Value
1010 10
Application Properties – Use Type-safe Properties
POJO
1111 11
Application Properties – Use Type-safe Properties
1212 12
Application Properties – Use Type-safe Properties
1313 13
Application Properties – Use Type-safe Properties
1414 14
Application Properties – We Can Validate Properties
1515 15
Application Properties – We Can Validate Properties
1616 16
Application Properties – We Can Validate Properties
1717 17
Application Properties – Takeaway
If you define a set of configuration keys for your own components,
we recommend you group them in a POJO annotated with
@ConfigurationProperties. Doing so will provide you with structured,
type-safe object that you can inject into your own beans.
- https://docs.spring.io/spring-boot/docs/current/reference/html/
spring-boot-features.html#boot-features-external-config-vs-value
1818 18
Dependency Injection
Preferred
Implicit constructor
injection
Lombok is our friend
Constructor Injection
Avoid if possible
Field Injection
Use wisely
Setter Injection
1919 19
Dependency Injection – Avoid Field Injection
Hint: hard to unit test
2020 20
Dependency Injection – Wisely Use Setter Injection
2121 21
Dependency Injection – Prefer Constructor Injection
2222 22
Dependency Injection – Implicit Constructor
Injection if Only One Constructor
Hint: test without Spring
2323 23
Dependency Injection – Implicit Constructor with
Lombok
Lombok generates constructor
2424 24
Dependency Injection – Takeaway
The Spring team generally advocates constructor injection, as it lets
you implement application components as immutable objects and
ensures that required dependencies are not null. Furthermore,
constructor-injected components are always returned to the client (calling)
code in a fully initialized state
- https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html
2525 25
Troubleshooting Tips
Turn on debug
• beans
• conditions
• configprops
• env
• loggers
• mappings
Tweak log level
Leverage Actuator endpoints
2626 26
Troubleshooting Tips - Turn on Debug
2727 27
Troubleshooting Tips - Tweak Log Levels
2828 28
Troubleshooting Tips – Leverage Actuator Endpoints
http://localhost:8080/actuator/beans - all beans in the application
http://localhost:8080/actuator/conditions - conditions that were evaluated on
configuration and auto-configuration classes
http://localhost:8080/actuator/configprops - all @ConfigurationProperties
http://localhost:8080/actuator/env - properties from Spring’s ConfigurableEnvironment
http://localhost:8080/actuator/mappings - a list of all @RequestMapping paths
http://localhost:8080/actuator/loggers - the configuration of loggers in the application
2929 29
Actuator Endpoints - beans
3030 30
Actuator Endpoints - conditions
3131 31
Actuator Endpoints – configprops
3232 32
Actuator Endpoints - env
3333 33
Actuator Endpoints - mappings
3434 34
Actuator Endpoints - loggers
3535 35
Be Productive
Lombok
DevTools
Boot Dashboard
H2 Console
3636 36
Be Productive – Let Lombok Write Code For You
Hint: refactoring is easier
3737 37
Be Productive – Let Lombok Write Code For You
3838 38
Be Productive – Use DevTools
Automatic Application
Restart
Browser Refresh by
LiveReload
3939 39
Be Productive – Use Boot Dashboard
Picture copyright - https://spring.io/blog/2015/10/08/the-spring-boot-dashboard-in-sts-part-1-local-boot-apps
Quick access to the
things you do all the
Time during
development
4040 40
Be Productive – Use H2 Console
4141 41
Manage Beans
@Component
@Service
@Repository
@Controller
@Configuration
@Bean
OthersSpring Built-in Stereotypes
4242 42
Manage Beans - Stereotypes
4343 43
Manage Beans - @Configuration and @Bean
4444 44
Bean Scope
Mostly used • Prototype
• Request
• Session
…
Singleton is by default Other scopes
4545 45
Bean Scope – Singleton is a given
4646 46
Bean Scope – Other scopes
4747 47
Post Startup Processing
CommandLineRunner ApplicationListener
4848 48
Post Startup Processing – Use CommandLineRunner
4949 49
Post Startup Processing – Use ApplicationListener
5050 50
Summary
Don’t over engineer (YAGNI)
Up to date dependencies
Convention over configuration
Learn from the source
Fundamental Spring Boot: Keep it Simple, Get it Right, Be Productive and Have Fun!
1 of 51

More Related Content

Similar to Fundamental Spring Boot: Keep it Simple, Get it Right, Be Productive and Have Fun!

Nike pop up   habitatNike pop up   habitat
Nike pop up habitatChef
270 views23 slides

Similar to Fundamental Spring Boot: Keep it Simple, Get it Right, Be Productive and Have Fun!(20)

More from VMware Tanzu(20)

Fundamental Spring Boot: Keep it Simple, Get it Right, Be Productive and Have Fun!