Interview Question About Spring Cloud Context, You Can Answer Like This

Spring cloud, I believe everyone is familiar with it. Spring cloud is a cloud application development tool based on Spring boot; Spring boot focuses on a single individual that is fast and easy to integrate.

So the question is, what is the context in the Spring cloud monomer?

First of all, let’s recall the application context of Spring. What is the application context? It can be simply understood that it is a container object that puts the objects you need Spring to manage for you into the container. The application context is an abstract representation of the Spring container, and our common ApplicationContext is essentially a powerful interface that maintains the bean definition and the cooperative relationship between objects. The core of Spring is the container, and the container is not unique. The framework itself provides many container implementations, which are roughly divided into two types: one is the less commonly used BeanFactory, which is the simplest container and can only provide basic DI function; another is the application context derived from inheriting the BeanFactory, and its abstract interface is the ApplicationContext we mentioned above, which can provide more enterprise-level services, such as parsing configuration files, etc., which is also the application context. The most common use case for instance objects. With the context object, we can register objects that need to be managed by Spring with the container. For the context abstract interface, Spring also provides us with multiple types of container implementations, which can be selected in different application scenarios:

  • AnnotationConfigApplicationContext: Load context definitions from one or more java-based configuration classes, suitable for java annotations
  • ClassPathXmlApplicationContext: Load context definitions from one or more xml configuration files under the classpath, suitable for xml configuration
  • XmlWebApplicationContext: Load context definitions from one or more xml configuration files under the web application, suitable for xml configuration
  • FileSystemXmlApplicationContext: Load context definitions from one or more xml configuration files under the file system, that is, load xml configuration files in the system disk
  • AnnotationConfigWebApplicationContext: specially prepared for web applications, suitable for annotation methods

In this way, we only need to configure the managed objects (we call them beans in Spring) and the collaboration relationship between beans, load the configuration into the IOC container through the application context, and let Spring manage the objects for us. When the time comes, you can get the bean from the container, because the container can provide the object management service you want for your program.

So in fact, there is a concept of “bootstrap context” in a Spring cloud application, which is the parent context of the main application. The bootstrap context is responsible for loading configuration properties from the configuration server, as well as decrypting properties in external configuration files. Unlike the main application, which loads properties in the application, the bootstrap context loads properties in the bootstrap. Properties configured in bootstrap have higher priority, so they cannot be overridden by local configuration by default.

If you want to disable the bootstrap process, set spring.cloud.bootstrap.enabled=false.

Application scenarios of bootstrap configuration files:

  • When using the Spring Cloud Config configuration center, you need to add configuration properties connected to the configuration center in the bootstrap configuration file to load the configuration information of the external configuration center
  • Some fixed properties that cannot be overridden
  • Some encryption/decryption scenarios

The application configuration file is mainly used for automatic configuration of Spring Boot projects

OK, the context is over.

Latest 20 Spring Boot Interview Questions That I Often Ask Candidates

I interviewed some people who said on their resumes that they were familiar with Spring Boot, or that they were learning Spring Boot. When I asked them, they only stayed at the stage of simple use, and many things were not clear, which also made me disappointed with the interviewees.

Below, I will summarize some Spring Boot interview questions that I often ask candidates. I hope they will help you.

1. What is Spring Boot?

Spring Boot is a sub-project of Spring open source organization. It is a one-stop solution for Spring components. It mainly simplifies the difficulty of using Spring, saves heavy configuration, and provides various initiators for developers to get started quickly.

2. Why Spring Boot?

Spring Boot has many advantages, such as:

Independent operation

Simplify the configuration

Automatic configuration

No code generation and XML configuration

Application of monitoring

The easier

With all these advantages, why not use Spring Boot?

3. What are the core configuration files of Spring Boot? What’s the difference?

The core configuration files for Spring Boot are the Application and Bootstrap configuration files.

Application configuration file This is easy to understand and is mainly used for automatic configuration of Spring Boot projects.

The bootstrap configuration file can be used in the following scenarios.

When you use Spring Cloud Config to configure the configuration center, you need to add the configuration properties connected to the configuration center in the bootstrap configuration file to load the configuration information of the external configuration center.

Properties that are fixed and cannot be overridden;

Some encryption/decryption scenarios;

4. What are the formats of Spring Boot configuration files? What’s the difference?

.properties and.yml, the main difference between them is the writing format.

1).properties

2).yml

In addition, the. Yml format does not support @propertysource annotation import configuration.

5. What are the core annotations for Spring Boot? Which notes are it mainly composed of?

The annotation above the Boot class is @SpringBootApplication, which is the core annotation of SpringBoot. The main combination contains the following three annotations:

6. What are the ways to enable Spring Boot?

1) Inherit the spring-boot-starter-parent project

2) Import the spring-boot-Dependencies dependencies project dependencies

7. Does Spring Boot need a separate container to run?

You don’t need it, Tomcat/ Jetty containers are built in.

8.What are the ways to run Spring Boot?

1) Package with command or put in a container to run

2) Run the Maven/ Gradle plugin

3) Run the main method directly

9.What is the principle of Spring Boot automatic configuration?

The @enableAutoConfiguration, @Configuration, and @conditionalonClass annotations are at the heart of the auto-configuration, which must first be a Configuration file and then be automatically configured based on whether the class is present in the classpath.

10.What is the directory structure of Spring Boot?

cn

This directory structure is the prevailing and recommended practice, while the @SpringBootApplication annotation on the main entry class enables SpringBoot capabilities such as automatic configuration, component scanning, and so on.

11. How do you understand the Starters in Spring Boot?

“Starters” is a set of “Starters” that can be integrated into your app, giving you a one-stop way to integrate Spring and other technologies without having to dig around for sample code and dependencies. If you want to use Spring JPA to access a database, simply add the spring-boot-starter-data-jPA initiator dependency.

“Starters” includes a lot of the dependencies that you’ll need for projects that will run quickly and continuously, and are a set of supported, managed transitive dependencies.

How do I run some specific code when Spring Boot starts?

You can implement interfaces ApplicationRunner or CommandLineRunner, both of which are implemented in the same way, providing only a run method

13. What are the configuration reading methods available in Spring Boot?

Spring Boot can bind variables via @propertysource,@Value,@Environment, and @ConfigurationProperties

14. What logging frameworks does Spring Boot support? What are the recommended and default logging frameworks?

Spring Boot supports Java Util Logging, Log4j2, Lockback as the Logging framework, and if you use the Starters launcher, Spring Boot will use Logback as the default Logging framework.

15. What are the ways to implement hot deployment in SpringBoot?

There are two main ways:

Spring Loaded

Spring-boot-devtools

16, How do you understand the Spring Boot configuration load order?

In Spring Boot, you can load configurations in the following ways.

1) Properties file;

2) YAML files;

3) System environment variables;

4) Command line parameters;

And so on…

17, Spring Boot how to define multiple sets of different environment configuration?

Provides multiple sets of configuration files, such as:

applcation.properties

application-dev.properties

application-test.properties

application-prod.properties

The runtime specifies the specific configuration file

18. Is Spring Boot compatible with old Spring projects?

Compatible, using the @importResource annotation to import the old Spring project configuration file.

19. What are the methods to protect Spring Boot applications?

Use HTTPS in production

Check your dependencies using Snyk

Upgrade to the latest version

Enable CSRF protection

Use content security policies to prevent XSS attacks

20.What’s new with Spring Boot 2.X? What’s the difference from 1.x?

Configuration changes

JDK Version Upgrade

Upgrade the third-party class library

Responsive Spring programming support

Support HTTP / 2

Configuring property Binding

More improvements and enhancements…

Finally finished writing, I hope you study hard before going to the interview, otherwise the interviewer asked these questions, and can not answer it will be embarrassing. Understanding these issues will also help you understand Spring Boot.

These 20 interview questions are for season 1, and more Spring Boot/Cloud related interview questions will be compiled later.