11 Spring Boot (1)
11 Spring Boot (1)
Spring Boot is module of Spring Framework which will speed up the development process.
Spring Boot is nothing but a Spring Framework for Rapid Application Development with extra support of configuration and
embedded server.
Chalenges in Spring
1. We to remember all of the dependcy which we need to add.
2. Also we need the manage version for all of this.
3. We need to add so many configuration for creating simple Web App.
What all Spring Boot Starter you have used or what all modules you have worked on?
Spring Boot Starter Web
Spring Boot Starter Data JPA
Spring Boot Starter AOP
Spring Boot Starter Web Service
Spring Boot Starter Security
Spring Boot Starter Apache Kafka
Spring Boot Starter Spring Cloud
Where to use setter injection over constructor injection and vice versa?
Can you provide an example of a real world use case where @PostConstruct is particularly useful.
Using @PostConstructor annotation we will define pre-processing logic like Kafka producer and consumer or database
logic.
Can you explain the key differences between yaml and properties file, and in what scenario you might prefer one
format over the other?
If I will configure same values in both properties then which value will be loaded in Spring Boot or who will load first
properties or yml file?
Properties file first.
What is bean scope and can you explain different type of bean scope?
How to define custom bean scope?
1. Implement Scope interface.
2. Overrirde all methods
3. Now we need to register beanFactory
Scope scope = new CoustomeThreadScope();
Context.getBeanFactory().registerScope(“threadScope”,scope);
4. Now annoted that CoustomeThreadScope class which is implemented by Scope interface.
Can you provide a few real-time use cases for when to choose Singleton scope and Prototype scope
Can we inject prototype bean in singleton bean? if yes what will happen if we inject prototype bean in singleton bean?
What is the purpose of the BeanPostProcessor interface in Spring, and how can you use it to customize bean
initialization and destruction?
Can we use @Service in repository class and @Controller in Service class and so on?
Yes we can use but it cannot have any logic to do.
Have you worked on Restful web services? If yes what all HTTP methods have you used in your project?
PUT
POST
GET
DELETE
How can you specify the HTTP method type for your REST endpoint?
Can you design a rest endpoint, Assume that you have a product database, and your task is to create an API to filter a
list of products by productType.
Design endpoints in a way that takes "productType" as input. If the user provides this input, the endpoint should filter
products based on the specified condition. If "productType" is not provided, the endpoint should return all the
products.
Can we perform update operation in POST http method if yes then why do we need PUT mapping or put http method?
Yes we can do it but its violet the rules of HTTP method.
PostMapping is create the new resource.
PutMapping is use to update the existing resource.
How can you customize the status code for your endpoint?
Just add @ResponseStatus annotation.
@ResponseStatus(HttpStatus.CEATED)
Can you explain deference between YML and properties file and which scenario you might prefer that?
YML is more readable in complex configuration and in structured way.
In properties file it does not follow any hierarchy it just plain line we write it any order.
In YML reduce prefix.
In YML it support list and arrays.
Cross origin is a security future use for controlling web page request from one domain to another domain.
@RestController
public class GreetingController {
How can you hide certain REST endpoints to prevent them from being exposed externally?
@ControllerAdvice
This annotation handles the specific Exception and send custom responses back to client.
How can you avoid defining handlers for multiple exceptions, or what is the best practise for handling exceptions?
use case: lets say you find a bug in production environment and now you want to debug the scenario, how can you do
that from your local?
How can you enable a specific environment without using profiles? OR what is the alternative to profiles to achieving
same use case?
We will add property in application.propertis file
Spring.profiles.active = prod
use case - Can I use AOP of evaluate performance of a method or is this possible to design a logging framework to capture
request and response body of a method?
How does your application interact with the database and which framework are you using?
Add dependency
Add important data source properties in application.properties file.
In this file we have Dialect – dialect will help JPA to generate a query behalf of us.
Create POJO class and add some annotations.
Now create repository interface and extends JPA repository.
What are the differences between hibernate JPA and Spring Data JPA ?
How can you connect multiple databases or data sources in a single application ?
What are the different ways to define custom queries in spring Data JPA ?
How will you define entity relationships or association mapping in spring Data JPA ?
Is this possible to execute join query in spring data JPA ? if yes, how can you add some insights ?
How will you implement pagination and sorting in Spring Data JPA ?
If same configuration write on both YML and Properties file then which value load first?
Properties file will load first.
@Configuartion
@ConfigurationProperties(prefix = “spring.datasource”)
@Data
@AllArgsConstructor
@Component
public class ConfigProprties
{
private String username;
private String password;
}
Can you explain when we should use singleton scope and prototype scope?
Singleton
Database Configuration, Service layer, Application Configuration
Prototype
User Session, Thread Safe, Heavy Initialization
What is the purpose of the BeanPostProcessor interface in Spring, and how can you use it to customize bean
initialization and destruction?