Spring Boot Fundamentals Udemy Notes
Spring Boot Fundamentals Udemy Notes
<dependency>
<artifactId>h2</artifactId>
</dependency>
fetches HSQL, DERBY which automatically creates the tables in the DB using
entities.
-spring.datasource.url
-spring.datasource.username
-spring.datasource.password
Create an entity;
Then in Repository Layer
create a EntityRepository interface and extend JPARepository<EntityName, TypeOfId>
Steps :
- Duplicate application.properties twice and
- name application-dev.properties and set value of
productrestapi.services.url
- name application-dev-testing.properties and set value of
productrestapi.services.url
Database Caching
Caching data in memory so that ORM tools can look for records in cache for faster
response.
To enable caching, SpringBoot uses 3rd party cache providers like HazelCast,
RehitCast., JBossCast.
• When there are multiple servers, objects which are cached need to be serialized
by implementing the Serializable interface.
What is Serialization?
Serialization in Java is the concept of representing an object's state as a byte
stream.
The byte stream has all the information about the object. Usually used in
Hibernate, JMS, JPA, and EJB,
serialization in Java helps transport the code from one JVM to another and then de-
serialize it there.
Steps to enable caching :
spring boot-starter-cache and hazelcast (from groupId
Add dependency to pom.xm1 for SpringBoot and HazelCast
Com. hazelcast), hazelcast-spring
Provide config required for the cache. Give the name, size, # of objects that would
go into the cache. These would be in javaconfig class.
Create a config class
Add @Configuration
return a Config object by setting the name, ttl etc.
Enable and use caching - Use @EnableCaching annotation in RestController or Service
classes.
Enable caching in SPringBootApplication class using @EnableCaching
Make model class Serializable by implementing Serializable interface.
Then wherever(RestController or Service classes) for the required method,
add the annotations QCacheable("cache-config-name-set-in-cache-config-class")
//readOnly=true only for readOnly methods
and @transactional(readOnly=true)
@CacheEvict("cache-config-name-set-in-cache-config-class") for delete methods
Evict cache (when caches are cleaned) what policy to be used for handling cache
LRU, LFU, NONE, RANDOM
** Spring Batch