Skip to main content

What does it mean by auto configuration by spring boot ?

Spring boot automatically configure bean if its corresponding library in classpath, for example if JdbcTemplate is in classpath then spring boot will configure JdbcTemplate and will create a bean that can be injected directly where its required.

What is starter dependency in spring boot ?

Starter dependency is aggregation of common dependency that are available during build for example if we are creating a web project that exposes REST API then we require spring web , spring mvc, tomcat, jackson etc.but spring boot provides web starter that has all in single dependency.If we add all dependency individually then we need to maintain all compatible version of library but using spring boot starter we get complete package that maintains all compatible version.

What is the benefit of spring boot Command line interface (CLI) ?

When we write code in spring boot CLI and use any Object or type then spring boot CLI detects starter for that type and put all related library on classpath. After library is added on classpath it uses auto configuration to create bean or injection.

What is spring boot Actuator ?

Actuator checks the internal working of application for example if our application is working with other application it can check is other application working,what are beans in spring context,what is environment variable set by application etc.

Is spring boot application server ?

No, it uses embedded container like tomcat but itself spring boot is not application server.

What is spring Initializer ?

It is an web application that creates spring boot project for developer.we can access Initializer by
SPRING INITIALIZER

How to set up basic spring initializer project ?

Spring boot project can be set up by following steps given in link.
Spring Boot Project set up

How to create REST API using spring boot ?

Spring boot REST API project can be created by following steps given in link.
Spring Boot REST Example

What is the spring Boot plugin in maven ?

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

What is the spring Boot plug in gradle ?

dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:")}

what are the ways to set properties for spring boot application ?

  • Command-line arguments
  • JNDI attributes from java:comp/env
  • JVM system properties
  • Operating system environment variables
  • Randomly generated values for properties prefixed with random.*
  • application.properties or application.yml file referred externally
  • application.properties or application.yml file referred internally
  • Property sources specified by @PropertySource
  • Default
Note: this list is order of precedence It means any property set higher in in list will override property set lower in in list

Which has higher precedence between application.properties and application.yml ?

application.yml

How can we make template non cachable ?

Set a property in properties file
  • spring.thymeleaf.cache=false (thymeleaf)
  • spring.freemarker.cache=false (Freemarker)
  • spring.groovy.template.cache=false (Groovy templates)
  • spring.velocity.cache=false (Velocity)

How can i change port number of embedded server ?

By default tomcat starts on 8080 port.To change it other port,we need to provide a property in .properties file.
server.port = 9000

What is the default logging in spring boot?

Logback is default to log on console at INFO level.

How can we use another logging implementation instead of Logback in spring boot ?

We need to exclude Logback from dependency for example.
For maven
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>

For gradle
configurations { all*.exclude group:'org.springframework.boot',module:'spring-boot-starter-logging'}
Add your logging implementation, for example log4j2.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

How to test REST API in spring boot using mock MVC standaloneSetup method ?

Spring boot REST API can be tested using mock MVC standaloneSetup method following steps
given in link.
Spring Boot REST API Test with Mock MVC Using standaloneSetup

How to test REST API in spring boot using mock MVC webAppContextSetup method ?

Spring boot REST API can be tested using mock MVC webAppContextSetup method following steps
given in link.
Spring Boot REST API Test with Mock MVC Using webAppContextSetup

What is the significance of SpringJUnit4ClassRunner ?

It helps to load spring application context in Junit based application and enables autowiring
of beans in test class.

How @SpringApplicationConfiguration is different from @Context- Configuration ?

@SpringApplicationConfiguration loads the spring application by SpringApplication,due to which it can loads external properties, spring boot logging

What is the significance of @WebIntegrationTest ?

This annotation is used on test class to test web application.For example if we have any web application that accepts request, so to test API we create actual environment ,in which application is started in embedded server and actual HTTP request is sent to test API.

How to make actuator enable for spring boot ?

To make actuator eable for spring boot we need to include only following dependency
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
Adding this dependency due to auto configuration ,actuator will enable.

Even after adding actuator on classpath why can we not access many endpoint in spring boot for example /bean, /metrics ?

Most endpoints are sensitive.It means they are not fully public.While some are for example /health and /info.
Most of the details exposed by endpoint is sensitive so only authorized user can access it, so we need to include spring security on class path
Add following dependency
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>

How can we customize an endpoint in spring boot ?

We can customize endpoint as well.Each endpoint has three properties.
  • id: id of the endpoint
  • enabled: false means it can not be accessed
  • sensitive: false means no authorize is required
For example
endpoints.beans.id=springbeans endpoints.beans.sensitive=false endpoints.shutdown.enabled=true

What is the significance of /metrics endpoint ?

This provides snapshot of running application and give many metrics for example memory allotted, heap allocation, thread pool, garbage collector etc

What is the significance of /trace endpoint ?

This provides details of web request for example method, path, request and response header, timestamp etc.

What is the significance of /dump endpoint ?

This provides snapshot of current thread activity

How to deploy spring boot application in external tomcat ?

Spring boot application can be deployed in external tomcat using following steps
given in link.
Deployment Of Spring Boot In External Tomcat

What is the benefit of spring boot ?

It provides following benefit
  • Auto Configuration : Spring boot can itself auto configure many configuration like bean injection
  • Starter Dependency : It provides complete package of library for any functionality, by this we can be away from version mismatch
  • Command line interface : By this we can concentrate only on application code instead of trading code like import , semicolon after each line
  • Actuator : By this we can know what's going on inside running application.

What dependency should i include to reload changes without restarting server ?

<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.1.5.RELEASE</version>
</dependency>

Comments

Popular posts from this blog

Mockito interview Questions

1.       Question 1. What Is Mockito? Answer : Mockito allows creation of mock object for the purpose of Test Driven Development and Behavior Driven development. Unlike creating actual object, Mockito allows creation of fake object (external dependencies) which allows it to give consistent results to a given invocation. 2.       Question 2. Why Do We Need Mockito? What Are The Advantages? Answer : Mockito differentiates itself from the other testing framework by removing the expectation beforehand. So, by doing this, it reduces the coupling. Most of the testing framework works on the "expect-run-verify". Mockito allows it to make it "run-verify" framework. Mockito also provides annotation which allows to reduce the boilerplate code. 3.       Question 3. Can You Explain A Mockito Framework? Answer : In Mockito, you always check a particular class. The dependency in that class is injected using m...

JAVA Expert Interview Questions Answers 2017

Java Basics ::  Interview Questions and Answers Home  »  Interview Questions  »  Technical Interview  »  Java Basics  » Interview Questions 1.     What is the difference between a constructor and a method? A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator. A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator. 2.     What is the purpose of garbage collection in Java, and when is it used? The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used. 3.  ...

Java Example Program to Convert List to Set

import java.util.ArrayList; import java.util.HashSet; import java.util.Set; public class ListToSet {  /**   * @author Amarjit Kumar   * @category interview questions   *   * Description: Convert List to set in java with example program   *   */  public static void main(String[] args) {   ArrayList<String> arrList= new ArrayList<>();   arrList.add("Java");   arrList.add("Java");   arrList.add("List to String");   arrList.add("Example Program");   Set<String> strSet = new HashSet<String>(arrList);   System.out.println(strSet);   System.out.println(arrList);  } } /*  * Java program to convert list to set. Convert ArrayList of string to HashSet  * in java example program How to convert List to Set in java Set<String> strSet  * = new HashSet<String>(arrList); HashSet having a constructor which will take  * list as an ar...