Deep Dive Into Spring
Deep Dive Into Spring
https://github.com/kousen/shopping_rest
Spring
Project infrastructure
Spring
Lifecycle management of "beans"
transaction managers
rest clients
DB connection pools
testing mechanisms
Spring
Need "metadata"
Annotations → better
JavaConfig → preferred
Many "starters"
http://start.spring.io
- ContentNegotiatingViewResolver
- BeanNameViewResolver
- HttpMessageConverters
- Static content:
- /static or /public or /resources directory
Path Matching and Content Negotiation
Disables suffix pattern matching by default
- FreeMarker
- Groovy
- Thymeleaf
- Mustache
@ExtendWith(SpringExtension.class)
MockMvc package
MockMvcRequestBuilders
MockMvcRequestMatchers
Testing
Integration tests:
@MockBean
@Valid
application.properties:
spring.datasource.schema, spring.datasource.data
JdbcTemplate
Standard practice:
http://localhost:8080/h2-console
Create a SqlParameterSource
Run executeAndReturnKey(parameters)
Transactions
Spring transactions configured with @Transactional
Propagation levels:
READ_UNCOMMITTED, READ_COMMITTED,
REPEATABLE_READ, SERIALIZABLE
JPA
Java Persistence API
@PersistenceContext → EntityManager
Spring Data
Large, powerful API
CrudRepository, PagingAndSortingRepository
Either:
Or:
Hander class
all methods take ServerRequest and return ServerResponse
How Does Spring Do Its Job?
- Load bean definitions from all sources
- Post-process bean definitions
- Substitute values at config time, like JDBC properties
- Read values from application.yml or application.properties
- Create and configure all the beans
- Set properties and dependencies
- Run bean post-processors
- Generate any necessary proxies
HAL Browser
Browser used to access RESTful web services
Handles hypermedia
References
- Spring in Action, 5th edition, by Craig Walls
- For Hibernate/JPA
- Pro JPA 2, 2nd edition (on Safari)
- https://thoughts-on-java.org/ has lots of Hibernate tips
- https://vladmihalcea.com/books/high-performance-java-persistence/
- Online reference docs for:
- Spring Framework
- Spring Boot
- Spring Data
- Spring Security