Spring Boot
Spring Boot
spring boot looks at a) frameworks available on the classpath b) existing configuration for the
application. based on these, spring boot provides basic configuration needed to configure the
application with these frameworks. this is called auto configuration .
4. spring-boot-properties-migrator
Helps project migrate from spring 1.x to 2
It will identify the properties that are no longer managed by Spring Boot.
6. Statelessness
Statelessness means that every HTTP request happens in complete isolation. When the
client makes an HTTP request, it includes all the information necessary for the server to
fulfill that request. The server never relies on information from previous requests. If that
information was important, the client would have to send it again in subsequent request.
Statelessness also brings new features. It’s easier to distribute a stateless application
across load-balanced servers. A stateless application is also easy to cache.
7. ORM in Spring
Spring-ORM is an umbrella module that covers many persistence technologies, namely
JPA, JDO, Hibernate and iBatis
8. JPA vs Hibernate
What is JPA?
A JPA (Java Persistence API) is a specification of Java which is used to access, manage,
and persist data between Java object and relational database. It is considered as a
standard approach for Object Relational Mapping.
JPA can be seen as a bridge between object-oriented domain models and relational
database systems. Being a specification, JPA doesn't perform any operation by
itself. Thus, it requires implementation. So, ORM tools like Hibernate, TopLink, and
iBatis implements JPA specifications for data persistence.
What is Hibernate?
A Hibernate is a Java framework which is used to store the Java objects in the relational
database system. It is an open-source, lightweight, ORM (Object Relational Mapping)
tool.
Need of JPA
JPA Hibernate
It is just a specification. Various ORM tools It is one of the most frequently used JPA
implement it for data persistence. implementation.
To achieve this, we have to define global exception handler class with annotation
@RestControllerAdvice.
We have added one method "handleGenericNotFoundException", which handles exceptions
defined with
@ExceptionHandler(value = NotFoundException.class).
Any service generating exception "NotFoundException" will come to
“handleGenericNotFoundException” method, and the method will wrap the exception in
common format using the "CustomErrorResponse" class. In this case, the "customer not found"
error will be thrown as defined in “handleGenericNotFoundException” method inside the
ExceptionAdvice class.
@RestControllerAdvice
public class ExceptionAdvice {
@ExceptionHandler(value = NotFoundException.class)
public ResponseEntity<CustomErrorResponse>
handleGenericNotFoundException(NotFoundException e) {
CustomErrorResponse error = new CustomErrorResponse("NOT_FOUND_ERROR",
e.getMessage());
error.setTimestamp(LocalDateTime.now());
error.setStatus((HttpStatus.NOT_FOUND.value()));
return new ResponseEntity<>(error, HttpStatus.NOT_FOUND);
}}
@Bean
@Primary
@ConfigurationProperties("app.datasource.first")
public DataSourceProperties firstDataSourceProperties() {
return new DataSourceProperties();
}
@Bean
@Primary
@ConfigurationProperties("app.datasource.first.configuration")
public HikariDataSource firstDataSource() {
return
firstDataSourceProperties().initializeDataSourceBuilder().type(HikariDataSource.cla
ss).build();
}
@Bean
@ConfigurationProperties("app.datasource.second")
public DataSourceProperties secondDataSourceProperties() {
return new DataSourceProperties();
}
@Bean
@ConfigurationProperties("app.datasource.second.configuration")
public BasicDataSource secondDataSource() {
return
secondDataSourceProperties().initializeDataSourceBuilder().type(BasicDataSource.cla
ss).build();
}
@Autowired
private Environment env;
....
public void method() {
.....
String path = env.getProperty("userBucket.path");
..... }
Application.properties
cust.data.employee.name=Sachin
cust.data.employee.dept=Cricket
Use @Configuration
Employee.java
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@ConfigurationProperties(prefix = "cust.data.employee")
@Configuration("employeeProperties")
public class Employee {
private String name;
private String dept;
//Getters and Setters go here }
13. Why would you want to remove some auto configuration and how would you do
so?
If there are performance issues and you want to start up your spring boot application faster, you
can exclude certain auto configurations
@SpringBootApplication(exclude = {JacksonAutoConfiguration.class})
public class JustGifItApplication {
…
}
You need to be sure that you can exclude the auto configured dependency.
YAML Properties
.yml extension .properties extension
Supports key/value pairs(Map), Lists and Supports key/value pairs, scalar types
Scalar Types
Hierarchical Non-hierarchical
Multiple Spring profiles in default config file One Spring profile per Config file
@Configuration
public class CollegeConfig {
@Bean
public College collegeBean() {
return new College();
}
}
● Difference between @COmponent and @Bean
@Component - If any class is annotated with @Component it will be automatically detect by
using classpath scan.We can’t write specific implementation based on dynamic condition
@Bean - It is used to explicitly declare a single bean, rather than letting Spring do it
automatically. It works only when class is also annotated with @Configuration. We should use
@bean, if you want specific implementation based on dynamic condition.
getBean(Class<T> requiredType);
getBean(Class<T> requiredType, Object... args);
getBean(String name);
Each of the getBean methods is considered a factory method, which returns a bean matching
the criteria supplied to the method, like the bean's type and name.
Spring then extends BeanFactory with the ApplicationContext interface, which introduces
additional application configuration. Spring uses this configuration to start-up a bean container
based on some external configuration, such as an XML file or Java annotations.