Spring Boot Questions


What is Spring Boot?
In simple words, Spring Boot Framework is Auto-Dependency Resolution, Auto-Configuration, Management EndPoints, Embedded HTTP Servers (Tomcat/Jetty etc.) and Spring Boot CLI

In other words, Spring Boot Framework is Spring Boot Starter, Spring Boot Auto-Configurator, Spring Boot Actuator, Embedded HTTP Servers, and Groovy.


What are the different Spring Boot Components?
  • Boot Initializer
  • Spring Boot Starter
  • Auto Configurator.
  • Spring Boot CLI.
  • Actuator.

What are the differences between Spring and Spring Boot?
The Spring Framework provides multiple features that make the development of web applications easier. These features include dependency injection, data binding, aspect-oriented programming, data access, and many more.
Over the years, Spring has been growing more and more complex, and the amount of configuration such application requires can be intimidating. This is where Spring Boot comes in handy – it makes configuring a spring application a breeze.
Essentially, while Spring is unopinionated, Spring Boot takes an opinionated view of the platform and libraries, letting us get started quickly.
Here are two of the most important benefits Spring Boot brings in:
  • Auto-configure applications based on the artifacts it finds on the classpath
  • Provide non-functional features common to applications in production, such as security or health checks

What are the various Advantages of Using Spring Boot?
Here are some of the various advantages of using Spring Boot:
  • It is quite easy to create Spring Based applications with Java or Groovy.
  • It lessens lots of improvement time and expands profitability.
  • It abstains from writing lots of standard Code, Annotations, and XML Configuration.
  • It is quite easy to coordinate Spring Boot Application with its Spring Ecosystem like Spring JDBC, Spring ORM, Spring Data, Spring Security and so forth.
  • It takes after “Opinionated Defaults Configuration” Approach to diminish Developer effort
  • It gives Embedded HTTP servers like Tomcat, Jetty and more to create and test our web applications effectively.
  • It gives CLI (Command Line Interface) tool to create and test Spring Boot (Java or Groovy) Applications from commanding prompt very easily and rapidly.
  • It gives lots of modules to create and test Spring Boot Applications effectively utilizing Build Tools like Maven and Gradle
  • It provides loads of plug-in to work with implanted and in-memory Databases effortlessly.

What are the various features of Spring Boot?
Various Spring Boot Features are as follows:
  • Web Development
  • Actuators
  • Dev tools
  • Application occasions and listeners
  • Admin highlights
  • Externalized Configuration
  • Properties Files
  • YAML Support
  • Type-safe Configuration
  • Logging
  • Security

What is auto-configuration in Spring boot? how does it help? Why Spring Boot is called opinionated?
Auto-configuration automatically configures a lot of things based upon what is present in the classpath. For example, it can configure JdbcTemplate if its present and a DataSource bean are available in the classpath. It can even do some basic web security stuff if Spring security is present in the classpath. Anyway, the point is auto-configuration does a lot of work for you with respect to configuring beans, controllers, view resolvers etc, hence it helps a lot in creating a Java application.

Now, the big questions come, why it's considered opinionated? Well because it makes a judgment on its own. Sometimes it imports things which you don't want, but don't worry, Spring Boot also provides ways to override auto-configuration settings.

It's also disabled by default and you need to use either @SpringBootApplication or @EnableAutoConfiguration annotations on the Main class to enable the auto-configuration feature.

What is @SpringBootApplication annotation?
This is one of the most important and core annotation from Spring Boot. We use this annotation to mark the main class of our Spring Boot application.
@SpringBootApplication
public class SpringOrderAnnotationApplication {
 public static void main(String[] args) {
SpringApplication.run(SpringOrderAnnotationApplication.class, args);
 }
}
@SpringBootApplication is a convenience annotation equal to declaring @Configuration@EnableAutoConfigurationand @ComponentScan with their default attributes.

How to disable a specific auto-configuration?
If we want to disable a specific auto-configuration, we can indicate it using the exclude attribute of the @EnableAutoConfiguration annotation. For instance, this code snippet neutralizes DataSourceAutoConfiguration:
// other annotations
@EnableAutoConfiguration(exclude = DataSourceAutoConfiguration.class)
public class MyConfiguration { } 
If we enabled auto-configuration with the @SpringBootApplication annotation — which has @EnableAutoConfiguration as a meta-annotation — we could disable auto-configuration with an attribute of the same name:
// other annotations
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class MyConfiguration { }
We can also disable an auto-configuration with the spring.autoconfigure.exclude environment property. This setting in the application.properties file does the same thing as before:
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

What is Spring Boot DevTools used for?
Spring Boot Developer Tools, or DevTools, is a set of tools making the development process easier. 
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
Applications using DevTools restart whenever a file on the classpath changes. This is a very helpful feature in development, as it gives quick feedback for modifications.

What is Spring Boot Actuator used for?
Actuator brings Spring Boot applications to life by enabling production-ready features. These features allow us to monitor and manage applications when they’re running in production.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Spring Boot Actuator can expose operational information using either HTTP or JMX endpoints. Most applications go for HTTP, though, where the identity of an endpoint and the /actuator prefix form a URL path.
Here are some of the most common built-in endpoints Actuator provides:
  • auditevents: Exposes audit events information
  • env: Exposes environment properties
  • health: Shows application health information
  • httptrace: Displays HTTP trace information
  • info: Displays arbitrary application information
  • metrics: Shows metrics information
  • loggers: Shows and modifies the configuration of loggers in the application
  • mappings: Displays a list of all @RequestMapping paths
  • scheduledtasks: Displays the scheduled tasks in your application
  • threaddump: Performs a thread dump

Can we use jetty instead of tomcat in spring-boot-starter-web?
Remove the existing dependency on spring-boot-starter-web and add these in.
<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> 

What Is Name of The Configuration File, Which You Use in Spring Boot?
Configuration file name which is utilized as a part of Spring boot ventures is known as an application. Properties. It is vital to document as it is utilized to abrogate all default configurations.

What are possible sources of external configuration?
Spring Boot provides support for external configuration, allowing us to run the same application in various environments. We can use properties files, YAML files, environment variables, system properties, and command-line option arguments to specify configuration properties.
We can then gain access to those properties using the @Value annotation, a bound object via the @ConfigurationProperties annotation, or the Environment abstraction.
Here are the most common sources of external configuration:
  • Command-line properties: Command-line option arguments are program arguments starting with a double hyphen, such as –server.port=8080. Spring Boot converts all the arguments to properties and adds them to the set of environment properties.
  • Application properties: Application properties are those loaded from the application.properties file or its YAML counterpart. By default, Spring Boot searches for this file in the current directory, classpath root, or their configsubdirectory.
  • Profile-specific properties: Profile-specific properties are loaded from the application-{profile}.properties file or its YAML counterpart. The {profile} placeholder refers to an active profile. These files are in the same locations as, and take precedence over, non-specific property files.

What is the need for Profiles?
Enterprise application development is complex. You have multiple environments
  • Dev
  • QA
  • Stage
  • Production
You want to have different application configuration in each of the environments.
Profiles help to have different application configuration for different environments.
Spring Boot would pick up the application configuration based on the active profile that is set in a specific environment.

What are the ways in which Spring Boot can read configurations?
Spring Boot can bind variables via @PropertySource, @Value, @Environment, @ConfigurationProperties.

How to access a value defined in the application.properties file in Spring Boot?
Use the @Value annotation to access the properties defined in the application. properties file.
@Value("${custom.value}")
private String customVal;

How to set the active profile in Spring Boot?
There are two ways to set the active profile in Spring Boot.
  • Pass in the active profile as an argument while launching the application.
  • Use the application.properties file to set the active profile.
java -jar -Dspring.profiles.active=production application-1.0.0-RELEASE.jar //pass as command line argument
spring.profiles.active=production

How to run Spring Boot application to custom port?
Use the application.properties file to configure a custom port for Spring Boot application. To change the server port, use server.port property.
server.port=9001

How to configure and enable SSL for your Spring Boot application?
Use the server.ssl.* properties in the application.properties or yml file to configure and enable SSL for your Spring Boot application. Here are typical SSL configurations for your application.
server.port=8443 //SSL port
server.ssl.key-store=classpath:keystore.jks //You can also configure it to external location
server.ssl.key-store-password//password for your key
server.ssl.key-password=//key password
Remember, Spring Boot does not support configuration of both HTTP and HTTPS through the property file. Configure other port programmatically if you need to use both ports.

How to configure database using Spring Boot?
The Spring Framework provides extensive support for working with SQL databases, from direct JDBC access using JdbcTemplate to complete “object-relational mapping” technologies such as Hibernate. To connect configure the database for your Spring Boot application, use the spring-boot-starter-jdbc or spring-boot-starter-data-jpa “starters”.To configure datasource configuration, use the application.properties file in your application.
spring.datasource.url=jdbc:mysql://localhost/javadevjournal
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
Above example is to configure MySQL in your application.

What is best way to expose custom application configuration with Spring Boot?
The problem with @Value is that you would have your configuration values distributed throughout your application. A better option would be to have a centralized approach.
You can define a configuration component using @ConfigurationProperties.
@Component
@ConfigurationProperties("basic")
public class BasicConfiguration {
private boolean value;
private String message;
private int number;
}
The values can be configured in application.properties
basic.value= true
basic.message= Dynamic Message
basic.number= 100 

How do we monitor all Spring Boot Micro Services?
Spring Boot provides monitor endpoints to monitor the metrics of individual microservices. These endpoints are useful for getting information about the application, such as whether they are up and running, and whether their components, such as databases, are working.
However, one of the main drawbacks or difficulties with using monitors is that we have to open the application’s knowledge points separately to understand their status or health. Imagine a microservice involving 100 applications, and the administrator will have to hit the execution terminals of all 100 applications.
To help us deal with this situation, we will use
https://github.com/codecentric/spring-boot-admin
Open source project. Built on top of the Spring Boot Actuator, it provides a Web UI that allows us to visualize the metrics of multiple applications.

How to implement Exception Handling using Spring Boot ?
Spring provides a very useful way to handle exceptions using ControllerAdvice. 
Exceptions thrown by a Controller method is mapped to the ControllerAdvice method using @ExceptionHandler annotations.

 What is the difference between RequestMapping and GetMapping?
·       RequestMapping is generic - you can use with GET, POST, PUT or any of the other request methods using the method attribute on the annotation.
  • GetMapping is specific to GET request method. It’s just an extension of RequestMapping to improve clarity.

What is the difference between JPA and Hibernate?
  • JPA is a specification/Interface
  • Hibernate is one of JPA implementations
When we use JPA, we use the annotation and interfaces from javax.persistence package, without using the hibernate import packages.
We recommend using JPA annotations as we are not tied to Hibernate as implementation. Later we can use another JPA implementation.

In which layer, should the boundary of a transaction start?
We recommend managing transactions in the Service layer. Logic for business transactions is in the business/service layer and you would want to enforce transaction management at that level.

What is ELK stack?How to use it with Spring Boot?
The ELK Stack consists of three open-source products - Elasticsearch, Logstash, and Kibana from Elastic. 

  • Elasticsearch is a NoSQL database that is based on the Lucene search engine.
  • Logstash is a log pipeline tool that accepts inputs from various sources, executes different transformations, and exports the data to various targets. It is a dynamic data collection pipeline with an extensible plugin ecosystem and strong Elasticsearch synergy
  • Kibana is a visualization UI layer that works on top of Elasticsearch.
These three projects are used together for log analysis in various environments. So Logstash collects and parses logs, Elastic search indexes and store this information while Kibana provides a UI layer that provide actionable insights.

How to write Test cases using Spring Boot ?
Spring Boot provides the @SpringBootTest for writing Unit Test Cases
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootHelloWorldTests {

@Test
public void contextLoads() {
}
}

How to implement Spring web using Spring boot?
Web Application Convenience 
• Boot automatically configures
– A DispatcherServlet & ContextLoaderListener
– Spring MVC using same defaults as @EnableWebMvc
• Plus many useful extra features:
– Static resources served from classpath
• /static, /public, /resources or /META-INF/resources
– Templates served from /templates
• If Velocity, Freemarker, Thymeleaf, or Groovy on classpath
– Provides default /error mapping
• Easily overridden
– Default MessageSource for I18N

What is a FreeMarker template?
FreeMarker is a Java-based templating engine that initially focused on dynamic web page generation using the MVC software architecture. The main advantage of using Freemarker is the complete separation of the presentation and business layers.
Programmers can handle application code, and designers can handle html page design. Finally, you can combine these with freemarker to give the final output page.


No comments:

Post a Comment