Do you learn JAVA, are you sure you don’t understand this Sping boot?

From the initial monolithic architecture to the current microservice architecture, Spring Boot has played a huge role. Its concise and rapid development charm is worthy of being extended to any project of the company. Currently, Spring Boot has great features and it integrates many excellent technologies!
There are also two very important strategies in the SpringBoot framework: out of the box and convention over configuration.

SpringBoot is a framework to simplify Spring application development. He integrated Spring's technology stack and provided various standardized default configurations. This allows us to quickly develop Spring projects and avoid the trouble of xml configuration. Reduce the cost of the Spring project.

1. SpringBoot external configuration file loading sequence

This is quite a lot, but we are just a few important ones.

  • High priority ones will override low priority ones.

  • The parameter configuration on the command line has the highest priority.

  • The configuration file with profile outside the jar package.

  • The configuration file with profile in the jar package.

  • The configuration file without profile outside the jar package.

  • The configuration file without profile in the jar package.

Second, how does SpringBoot extend the configuration of SpringMVC

By creating your own class to inherit WebMvcConfigurerAdapter, annotate @Configuration on the class. Then rewrite the method inside. All the WebMvcConfigurers in the container will work together.

Insert picture description here
If we don't want to use mvc's automatic configuration, all use our own. The annotation @EnableWebMvc can be added to the configuration class. This annotation can be imported into a webmvcconfigurationsupport class. Then there is such a sentence in the annotation on the automatic configuration class of mvc, there is a conditional annotation, and it will take effect when there is no such class.

The author has compiled the latest collection of knowledge points including: JVM, MySQL, MyBatis, Dubbo, Linx, TomCat and other knowledge points. If you need it, please join our discussion group 1149778920 to exchange and learn to receive information. Code : qf

Insert picture description here

Three, Spring inversion of control (IOC)

Implementation mechanism of Spring IoC

The realization principle of IoC in Spring is the factory pattern plus reflection mechanism.

interface Fruit {
    
    
   public abstract void eat();
 }
 
class Apple implements Fruit {
    
    
    public void eat(){
    
    
        System.out.println("Apple");
    }
}
 
class Orange implements Fruit {
    
    
    public void eat(){
    
    
        System.out.println("Orange");
    }
}
 
class Factory {
    
    
    public static Fruit getInstance(String ClassName) {
    
    
        Fruit f=null;
        try {
    
    
            f=(Fruit)Class.forName(ClassName).newInstance();
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
        return f;
    }
}
 
class Client {
    
    
    public static void main(String[] a) {
    
    
        Fruit f=Factory.getInstance("io.github.dunwu.spring.Apple");
        if(f!=null){
    
    
            f.eat();
        }
    }
}

4. What is the difference between BeanFactory and ApplicationContext?

BeanFactory and ApplicationContext are the two core interfaces of Spring, and both can be used as Spring containers. The ApplicationContext is a subinterface of BeanFactory.

Dependency

  • BeanFactory: It is the lowest interface in Spring. It contains definitions of various Beans, reads bean configuration files, manages bean loading and instantiation, controls bean life cycle, and maintains dependencies between beans.

As a derivation of BeanFactory, ApplicationContext interface not only provides the functions of BeanFactory, but also provides more complete framework functions:

-Inherit MessageSource, so it supports internationalization.

  • Unified resource file access method.
  • Provides events for registering beans in the listener.
  • Load multiple configuration files at the same time.
    Load multiple (inherited) contexts so that each context focuses on a specific level, such as the web layer of the application.

Loading method

BeanFactroy uses lazy loading to inject Beans, that is, only when a Bean is used (getBean() is called), the Bean is loaded and instantiated. In this way, we cannot find some existing Spring configuration problems. If a certain attribute of Bean is not injected, after BeanFacotry is loaded, an exception will not be thrown until the first use of the getBean method.

ApplicationContext, which creates all Beans at once when the container is started. In this way, when the container starts, we can find configuration errors in Spring, which helps to check whether the dependent properties are injected. After the ApplicationContext is started, all single-instance beans are pre-loaded. By pre-loading single-instance beans, you can ensure that when you need them, you don't have to wait because they have already been created.

Compared with the basic BeanFactory, the only disadvantage of ApplicationContext is memory space. When there are more Beans in the application configuration, the program startup is slower.

Creation method

BeanFactory is usually created programmatically, ApplicationContext can also be created in a declarative way, such as using ContextLoader.

way to register

Both BeanFactory and ApplicationContext support the use of BeanPostProcessor and BeanFactoryPostProcessor, but the difference between the two is: BeanFactory requires manual registration, while ApplicationContext is automatically registered.

SpringBoot integrates redis

Introduce the redis launcher. data-redis. After the introduction, the configuration file is used to configure the redis host. Then it can be operated through redistemplate. It will be garbled, and the jdk serialization json device is used by default. Need to be changed to jackson. Implement a RedisTmeplate by yourself. The generic type is object, object. Then implement CacheManager. You will find that the cache becomes a json format.
Insert picture description here

The author is here to integrate the latest real interview questions for you from big factories. Small partners in need can add a group: 1149778920 Password: qf
pick up by yourself
Insert picture description here

Guess you like

Origin blog.csdn.net/S11035762/article/details/109258494