Thursday, July 16, 2020

# Spring Boot #



What is spring-boot-starter-parent in Spring-Boot pom.xml file?


We have seen that spring-boot application pom.xml has parent declaration as shown in the below pom.xml file. Why do we need that?
?
1
2
3
4
5
6
7
8
9
10
11
12
13
                                                     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.java2novice.springboot</groupId>
    <artifactId>spring-boot-tutorials</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
    </parent>
</project>
As we know, in general, parent pom declarations allows us to manage following things for multiple child projects.
  • Configuration: It allows us to maintain consistancy in terms of java version and other related properties accross all sub projects.
  • Depedency Management: This will control all versions of the dependencies to avoid dependency version conflicts.
  • Default Plugin Configuration

Let’s explore real-time spring boot interview questions.
1. What is Spring Boot?
A) Spring Boot is a sub-project of Spring’s open source organization. It is a one-stop solution for Spring components. It mainly simplifies the difficulty of using Spring. It saves a lot of configuration and provides various launchers, so developers can get started quickly.
2. Why use Spring Boot?
A) Spring Boot has many advantages, such as:
·         Operate independently
·         Simplified configuration
·         Automatic configuration
·         No code generation and XML configuration
·         Application monitoring
·         Easy to get started
Spring Boot has so many advantages.
3) What are the advantages of Spring Boot?
A) The advantages of Spring Boot are:
·         Reduce development, testing time and effort.
·         Using JavaConfig helps avoid using XML.
·         Avoid a lot of Maven import and various version conflicts.
·         Provide advice development methods.
·         Start development quickly by providing default values.
·         There is no separate web server required. This means you no longer need to start Tomcat, Glassfish or anything else.
·         Less configuration is required because there is no web.xml file. Just add the class annotated with @Configuration and add the method annotated with @Bean. Spring will automatically load the object and manage it as before. You can even add @Autowired to the bean method to have Spring automatically load the required dependencies.
·         Environment-based configuration Using these properties, you can pass the environment you are using to the application: -Dspring.profiles.active = {enviornment}. After loading the main application properties file, Spring will load the subsequent application properties file in (application{environment} .properties).
4) What are the core configuration files for Spring Boot? What is the difference between them?
A) The core configuration files for Spring Boot are the application and bootstrap configuration files.
The application configuration file is easy to understand and is primarily used for automated configuration of Spring Boot projects.
The bootstrap configuration file has the following application scenarios.
·         When using Spring Cloud Config to configure the hub, you need to add the configuration properties of the connection to the configuration center in the bootstrap configuration file to load the configuration information of the external configuration center.
·         Some fixed attributes that cannot be overridden;
·         Some encryption/decryption scenarios;
5) What is JavaConfig?
A) Spring JavaConfig is a product of the Spring community that provides pure Java methods for configuring Spring IoC containers. So it helps to avoid using XML configuration. The advantages of using JavaConfig are:
·         Object-oriented configuration. Since the configuration is defined as a class in JavaConfig, users can take advantage of the object-oriented features in Java. A configuration class can inherit from another, override its @Bean method, and so on.
·         Reduce or eliminate XML configuration. The benefits of externalization configuration based on the principle of dependency injection have been proven. However, many developers don’t want to switch back and forth between XML and Java. JavaConfig provides developers with a pure Java way to configure Spring containers similar to the XML configuration concept. From a technical point of view, it is possible to configure the container using only the JavaConfig configuration class, but in fact many people think that it is ideal to mix JavaConfig and XML.
·         Type safety and refactoring friendly. JavaConfig provides a type-safe way to configure the Spring container. Thanks to Java 5.0 support for generics, it is now possible to retrieve beans by type rather than by name, without any cast or string-based lookups.
6) What are the formats of Spring Boot configuration files? What is the difference between them?
A) .properties and .yml, the main difference between them is the writing format.
1).properties
App.user.name = javastack
2).yml
App: user: name: javastack
In addition, .yml format does not support @PropertySourceannotations import configuration.
7) Which is the core annotation of Spring Boot? Which annotations is it mainly composed of?
A) The annotation above the startup class is @SpringBootApplication, which is also a core annotation for Spring Boot. The main combination contains the following three annotations:
@SpringBootConfiguration: Combines the @Configuration annotation to implement the configuration file.
@EnableAutoConfiguration: Turns on automatic configuration. You can also turn off an automatic configuration option, such as turning off data source auto-configuration: @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class }).
@ComponentScan: Spring component scan.
8) What are the ways to enable Spring Boot features?
1) Inherit the spring-boot-starter-parent project
2) Import spring-boot-dependencies project dependencies
9) Does Spring Boot require a separate container to run?
A) It is not necessary, and a container such as Tomcat/ Jetty is built in.
10) What are the ways to run Spring Boot?
1) Packing with commands or putting them into a container
2) Run with the Maven/Gradle plugin
3) directly execute the main method to run
11) What is Swagger? Did you implement it with Spring Boot?
A) Swagger is widely used for visual APIs, using the Swagger UI to provide an online sandbox for front-end developers. Swagger is a tool for generating visual representations of RESTful web services, specifications and full framework implementations.
It enables documents to be updated at the same speed as the server. When properly defined by Swagger, consumers can use a minimal amount of implementation logic to understand and interact with remote services. Therefore, Swagger eliminates the guesswork when calling the service.
12) What is Spring Profiles?
A) Spring Profiles allows users to register beans based on configuration files (dev, test, prod, etc.). Therefore, when the application is running in development, only certain beans can be loaded, while in PRODUCTION, some other beans can be loaded.
Suppose our requirement is that the Swagger document is only for the QA environment and all other documents are disabled. This can be done using a configuration file. Spring Boot makes it easy to use configuration files.
13) What is Spring Batch?
A) Spring Boot Batch provides reusable functions that are important when working with large numbers of records, including logging/tracking, transaction management, job processing statistics, job restarts, skipping, and resource management.
It also provides more advanced technical services and features that enable extremely high volume and high-performance batch operations through optimization and partitioning techniques. Simple and complex high-volume batch jobs can use the framework to process a large amount of information in a highly scalable manner.
14) What is a FreeMarker template?
A) 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.
15) How to implement exception handling using Spring Boot?
Spring provides a very useful way to handle exceptions using ControllerAdvice. We handle all exceptions thrown by the controller class by implementing a ControlerAdvice class.

Spring Framework

16) Which starter maven dependencies have you used?
A) Used some of the following dependencies
spring-boot-starter-activemq
spring-boot-starter-security
spring-boot-starter-web
This helps increase fewer dependencies and reduces version conflicts.
17) What is a CSRF attack?
A) CSRF stands for cross-site request forgery. This is an attack that forces the end user to perform unwanted operations on the currently authenticated web application.
CSRF attacks are specifically targeted at state change requests, not data theft because an attacker cannot view responses to forged requests.
18) What is WebSockets?
A) WebSocket is a computer communication protocol that provides a full duplex communication channel over a single TCP connection.
·         WebSocket is bidirectional – you can use a WebSocket client or server to initiate a message.
·         WebSocket is full-duplex – client and server communication are independent of each other.
·         Single TCP connection – The initial connection uses HTTP and then upgrades this connection to a socket-based connection. Then this single connection is used for all future communications
·         Light – WebSocket message data exchange is much lighter than http.
19) What is AOP?
A) In the software development process, the function of crossing multiple points of the application is called a crossover problem. These cross-cutting issues are different from the main business logic of the application. Therefore, separating these crosscutting concerns from business logic is where Aspect Oriented Programming (AOP) is.
20) What is Apache Kafka?
Apache Kafka is a distributed publish-subscribe messaging system. It is an extensible, fault-tolerant publish-subscribe messaging system that enables us to build distributed applications. This is an Apache top-level project. Kafka is suitable for offline and online messaging.

Microservices

21) How do we monitor all Spring Boot microservices?
A) 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 50 applications, and the administrator will have to hit the execution terminals of all 50 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.
22) What is the Spring Boot automatic configuration principle?
A) Note @EnableAutoConfiguration, @Configuration, @ConditionalOnClass is the core of automatic configuration, first it must be a configuration file, and then according to the class path to have this class to automatically configure.
23) What is the directory structure of Spring Boot?
Cn +- javastack +- MyApplication.java | +- customer | +- Customer.java | +- CustomerController.java | +- CustomerService.java | +- CustomerRepository.java | +- order +- Order.java +- OrderController. Java +- OrderService.java +- OrderRepository.java
This directory structure is the mainstream and recommended practice, and the @SpringBootApplication annotation is added to the main entry class to enable Spring Boot’s capabilities, such as automatic configuration, component scanning, and so on.
24) How do you understand Starters in Spring Boot?
A) Starters can be understood as a launcher, which contains a set of dependencies that can be integrated into the application. You can integrate Spring and other technologies in one place without having to find sample code and dependencies everywhere. If you want to use Spring JPA to access the database, just add the spring-boot-starter-data-jpa launcher dependency to use.
Starters contain dependencies that are needed in many projects. They run quickly and continuously and are a set of supported management transitive dependencies.
25) How do I run some specific code when Spring Boot starts?
A) You can implement the interface ApplicationRunner or CommandLineRunner. These two interfaces are implemented in the same way. They only provide a run method.

Spring Microservices

26) What are the ways in which Spring Boot can read configurations?
A) Spring Boot can bind variables via @PropertySource, @Value, @Environment, @ConfigurationProperties.
27) What log framework does Spring Boot support? Which is the recommended and default log framework?
A) Spring Boot supports Java Util Logging, Log4j2, and Lockback as the logging framework. If you use the Starters launcher, Spring Boot will use Logback as the default logging framework.
28) What are the ways in which SpringBoot implements hot deployment?
A) There are two main ways:
·         Spring Loaded
·         Spring-boot-devtools
29) How do you understand the Spring Boot configuration load order?
A) In Spring Boot, there are several ways to load a configuration.
1.    Properties file;
2.    YAML files;
3.    System environment variables;
4.    Command line parameters;
and many more……
30) How does Spring Boot define multiple sets of different environment configurations?
A) Provide multiple sets of configuration files, such as:
Applcation.properties application-dev.properties application-test.properties application-prod.properties
Specify a specific configuration file at runtime.
31) Can Spring Boot be compatible with older Spring projects? How do I do this?
A) Compatible, use @ImportResourceannotations to import old Spring project configuration file.
32) What are the methods to protect Spring Boot applications?
·         Use HTTPS in production
·         Check your dependencies with Snyk
·         Upgrade to the latest version
·         Enable CSRF protection
·         Use content security policies to prevent XSS attacks
33) What are the new features of Spring Boot 2.X? What is the difference with 1.X?
·         Configuration change
·         JDK version upgrade
·         Third-party class library upgrade
·         Responsive Spring programming support
·         HTTP/2 support
·         Configuration property binding
·         More improvements and enhancements…


No comments:

Docker Swarn

Docker is a tool intended to make the process of creating, deploying and running applications easier by using container based virtualization...