rajeshkawali / Springboot-interview-project

Springboot examples for interviews

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Springboot-interview-project

Springboot examples for interviews

To get text from file

using @PropertySource and @Value we can retrive data from file (test.txt)

To read propery values from application.properties file

To get list of values

@Value("#{'${my.user.list}'.split(',')}")

To get key value pair

@Value("#{${getMap}}")

To assign properties values to class

@ConfigurationProperties(prefix = "user.address")

How to get default valuse if requested property is not present in application.properties file

If "app.name" property is not present in the file then it will not through any exception insteaded it will return default value given in @Value annotation using

@Value("${app.name: This is default value from Value annotation}")

Inject prototype bean into singleton class

using @Lookup and @Scope("prototype") we can inject prototype bean into singleton class.

How to exclude embeded tomcat and add jetty

The default embedded server is tomcat. Other options available are jetty and underTow

To remove the tomcat starter from your project, exclude tomcat from web dependency. Just add spring-boot-starter-jetty dependency

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
	<exclusions>
		<exclusion>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
		</exclusion>
	</exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

Disable embedded tomcat using below flag in application.properties

spring.main.web-application-type=none

How to qualify a bean when there are multiple beans available

@Primary indicates that a bean should be given preference when multiple beans are qualified to autowire, there should be exactly one primary bean.

@Qualifier indicates specific bean should be autowired when there are multiple beans.

To exclude any auto configured files from Spring boot

Using below annotation we can exclude any auto configured file from spring boot application

@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})

Difference between @Contoller and @RestController ?

  • @Controller is used to create traditional MVC controllers that handle incoming HTTP requests and return views.

  • @Controller is a specialized version of @Component annotation.

  • @Controller, we can return a view in Spring Web MVC.

  • @Controller was added in Spring 2.5.

  • @RestController is used to create RESTful web services that return data in JSON or XML format directly in the response body.

  • @RestController is a combination of @Controller and @ResponseBody.

  • @RestController, we cannot return a view.

  • @RestController simplifies the development of web services by eliminating the need for the developer to manually serialize and deserialize response data.

  • @RestController was added in Spring 4.0.

Spring boot profiles

To set specific profile in application.properties use below property

We can also access configuration from class using @Profile({"prod"})

spring.profiles.active=local

  1. application-local.properties: This properties file will be responsible to add the set of the environments for local.

  2. application-dev.properties: This properties file will be responsible to add the set of the environments for development.

  3. application-prod.properties: This properties file will be responsible to add the set environment for production.

  4. application-test.properties: This properties file will be responsible to add the set of the environments for the test.

Enable Lazy Initialization

Set the property value to true means that all the beans in the application will use lazy initialization.

spring.main.lazy-initialization=true

To enable lazy initialization for specific class

@Lazy

Spring boot actuator

Spring Boot's 'Actuator' dependency is used to monitor and manage the Spring web application

We can modify default endpoint of actuator by adding below flag in application.properties

management.endpoints.web.base-path=/actuatorDetails

We can enable and disable other actuator endpoints through property files. If you want to enable all actuator endpoints, then add following property.

management.endpoints.web.exposure.include=*

To enable only specific actuator endpoints, provide the list of endpoint id.

management.endpoints.web.exposure.include=health,info,beans,env,metrics,trace

URL : http://localhost:8787/actuatorDetails

URL : http://localhost:8787/actuatorDetails/info

Difference between @RequestParam and @PathVariable in spring boot

Both @RequestParam and @PathVariable are annotations in Spring Boot that can be used to extract data from a request URL, but they are used in different scenarios.

@RequestParam is used to extract request parameters from the URL, which are typically used for filtering, sorting, or paging data. For example, in a URL like /products?productId=1&size=10, productId and size are request parameters that can be extracted using @RequestParam. These parameters are optional by default, unless specified otherwise.

@GetMapping("/products")
public Product getProductById(@RequestParam Long productId, @RequestParam Long size) {}

@PathVariable is used to extract values from the URL path itself, which are typically used to identify a specific resource or entity. For example, in a URL like /products/123, 123 is the value of the products ID, which can be extracted using @PathVariable. These values are required by default, unless specified otherwise.

@GetMapping("/products/{productId}")
public Product getProductById(@PathVariable Long productId) {}

What happens if we use both the @Autowired annotation and the new operator to create a bean of the same type(same class) in a Spring Boot application?

If you use both the @Autowired annotation and the new operator to create a bean of the same type, then you will end up with two different instances of the bean.

When you use @Autowired, Spring Boot automatically creates and injects an instance of the specified bean into your class, while when you use the new operator, you are creating a new instance of the bean yourself. If you create a new instance of a bean using the new operator, Spring Boot will not manage the bean, which means that it will not be included in any of the dependency injection or autowiring processes that Spring Boot provides.

What happens if both application.properties and application.yml files exist in the classpath? What about bootstrap.properties and bootstrap.yml files?

If both application.properties and application.yml files exist in the classpath, then the application.yml file will take precedence over the application.properties file. This is because Spring Boot considers YAML files to be more readable and easier to manage than properties files.

Similarly, if both bootstrap.properties and bootstrap.yml files exist in the classpath, then the bootstrap.yml file will take precedence over the bootstrap.properties file. The bootstrap files are loaded before the application files, and they are used to configure external resources and initialize the application context. This means that if you have configuration settings that need to be loaded before the application context is initialized, then you should use bootstrap files.

About

Springboot examples for interviews


Languages

Language:Java 100.0%