Interview must ask [Framework]-spring family bucket, mybatis, RPC and ORM

What is spring

Spring is a framework for creating beans and managing objects, including ioc, aop and other technical points

spring startup class

1. @configuration means to start the class.
2. @componentScan ("package path") specifies all the paths of the beanDefinition that scans the class and generates the class file into the class.
3. @Import, in addition to scanning, you can also import beanDefinition through import
Insert picture description here

What is bean

Spring creates and implements automatic dependency injection, and the object that fills in the properties is the bean

Bean dependency injection timing

The process of dependency injection is triggered
when the user asks for a Bean from the IoC container for the first time. If lazy-init=true is set, the bean will be initialized when the first getBean is set, and lazy-init=false will be initialized directly when the container starts ( singleton bean);

The difference between object and bean creation

Spring bean is an object created by spring, and it will automatically inject dependencies and fill in attributes. For
example, there is an auto-assembly attribute user in person, which is
@component
class person{ @Autowired User user; // Autowired will assemble according to the type, or according to Bean name assembly } Then the user attribute in the bean person initialized by the spring context container is automatically injected. If the new object person is used, the user in this java object is null




Spring bean creation == bean life cycle

1. Spring starts applicationContext, scans the class class annotated by componentScan
1. Class file of the class, generates BeanDefinition, adds BeanDefinitionMap (concurrentHashMap)
BeanDefinitionMap <beanName, beanDefinition object>, which is an attribute in BeanFactory
2. After scanning all classes, construct The beanFactory object (beanFactory's BeanDefinitionMap is fully loaded)
3. Call the beanFactoryPostProcesssor post processor, which can process BeanDefinitinon
(the programmer implements the beanFactoryPostProcesssor method and can modify the BeanDefinitinon)
4. Instantiate and create the bean beanFactory.getBean() method
beanFactory .getBean() method, if the bean is not obtained, the bean will be created
5. beanPostProcessor, bean post processor,
realize the filling of @Autowired annotation in
bean (autowirePostProcessor), realize bean Aop, create dynamic proxy class
6. Singleton Pool(ConcurrentHashMap<beanName, object address>)
7. Then the bean can be called by the getBean() method to generate a beanDefinition, assemble it into the beanDefinitionMap, and place it in the beanFactory.
Insert picture description here
beanFactory

FactoryBean interface

Custom bean
A bean class implements the FactoryBean interface and overrides the getObject method to customize the object of this bean class. For
example, the following bean implements the FactoryBean interface and uses the proxy.newProxyInstance method to create a dynamic proxy class
Insert picture description here

Various Aware callback mechanisms

After Spring fills in the properties, if it implements the interface, it will call back the methods in the interface to get some of its own properties.
How does Spring get the beanName in the bean properties? How to get the current beanFactory
with beanNameAware
Spring?
Use beanFactoryAware
as shown in the figure:
Insert picture description here

What is beanDefinition The
beanDefinition class abstracts some properties of the bean, including constructor, lazy loading, automatic injection method, properties, method names, whether the bean depends on other beans, beanClass, etc.
Insert picture description here

IOC inversion of control, DI dependency injection

Inversion of control: The right to create objects is managed by Spring.
DI (dependency injection): In the process of creating objects in Spring, the attributes that the objects depend on are injected into the class.

Spring's IOC dependency injection method

Constructor injection, setter method injection, annotation injection, interface injection

What is a circular dependency and why is there a circular dependency

The attributes of the A object depend on B, and the attributes of the B class depend on A. There is no way to set the attributes when it is created.

Because of the life cycle of the bean
1, load beanDefinition, generate objects
with unpopulated properties 2, fill properties, aware
3, bean post-processing, such as bean aop to generate proxy objects
4, put it into the singleton pool

The three-level cache solves the circular dependency problem:

What is the third-level cache is currentHashmap
1. First-level cache: singleton pool, storing the original object singletonObjects of the bean
2. Second-level cache: unfilled attributes, early-exposed objects earlySingletonObjects
3. Third-level cache: ( no aop just Not used) SingletonFactories, the factory used for aop, will only be executed once after execution, and a proxy object will be generated and placed in the second-level cache. If there is a circular dependency and aop, when the aop is executed subsequently, the proxy object will not be regenerated, and it will be taken directly from the second-level cache

The
creation of the second-level cache A depends on B, and the A object that is not filled with the attribute B is placed in the second-level cache. When the B object is created, the first-level cache is used to find the A object, and then the second-level cache is found
. Among the properties, the A object is also successfully instantiated

Three-level cache
But Bean may aop. After AOP, the objects in the second-level cache are proxy objects, which are inconsistent with the original objects. There will be problems at this time,
so the third-level cache singletonFactories is needed! ! ! !
B first gets singletonFactories from the third-level cache, and singletonFactories generates proxy objects and injects attributes of B. This solves the problem of attribute B because the AOP injected object changes from the original object to the proxy object~~~ and generates the proxy object on the second level. In cache

Because the prerequisite for adding singletonFactories three-level cache is to execute the constructor, the circular dependency of the constructor cannot be resolved .

what is mybatis

Mybatis is a framework for mapping database data and java objects. Need to write sql manually, so it is semi-automatic
Hibernate+jpa is fully automatic sql, so it is fully automatic mapping

The steps and principles of MyBatis's analytical sql and operation

1. InputStream loads xml configuration file
2. Creates sqlSessionFactory
3. sqlSessionFactory creates sqlSession to establish connection
4. sqlSession.getMapper creates mapper object
5. Mapper call method
5. sqlSession.commit, flush, close

${} dynamic sql dollar—dynamic
#{} static sql, first translated into? when parsing the xml file? , Similar to
the pre-loading mechanism of jdbc mybatis, to prevent sql injection

What design patterns are used in Spring

Factory mode: BeanFactory in spring is the embodiment of simple factory mode, which obtains bean objects according to the unique identifier passed in;
singleton mode: provides a global access point BeanFactory;
proxy mode: the principle of AOP function uses proxy mode (1 JDK dynamic proxy. 2. CGLib bytecode generation technology proxy.)
Decorator mode: Dependency injection requires the use of BeanWrapper;
observer mode: Observer mode in spring is commonly used in the implementation of listener. Such as ApplicationListener.
Strategy mode: When the bean is instantiated, it is decided which way to initialize the bean instance (reflection or CGLIB dynamic bytecode generation)

AOP dynamic proxy

1. JDK dynamic proxy Proxy.InvocationHandler generates proxy objects. Rewrite the invoke method, and the proxy object implements the method of calling the real object by reflecting the invoke method
2. cglib dynamic proxy

The difference between dynamic proxy and static proxy
Static proxy, the .class file of the proxy class exists
before the program runs ; dynamic proxy: use reflection to dynamically create proxy objects when the program is running <reusability, ease of use, more centralized call invoke>

The difference between CGLIB and JDK dynamic proxy
Jdk must provide an interface to use;
cglib does not need, as long as a non-abstract class can achieve dynamic proxy

Spring integrates mybatis, what object does it automatically assemble?

It is a proxy object created by mybatis using jdk's dynamic proxy
Insert picture description here
Insert picture description here

How to automatically assemble the Mapper object created by mybatis into the spring class

1. Create a class A to implement ImportBeanDefinitionRegistrar
2. @import (A.class) annotation, import the mapper class into beanDefinition

beanFactory.registerSingleton
1, proxy.newProxyInstance creates a proxy class
2, beanFactory.registerSingleton(beanName"",xxx);
implement Factorybean

What is servlet

A servlet is a java program running on a web server to receive and send http requests.
Use:
1. Implement the servlet interface, rewrite the doGet and doPost methods
2. Deploy the developed Java classes to the web server

servlet life cycle

1. When each user accesses the servlet, a servlet instance will be created, and the servlet will execute the init method.
2. For each request, the servlet will call the service method. According to the internal difference of the service method, different doget methods are requested.
3. The servlet is removed from the server. delete delete servlet

What is MVC?

MVC is a design pattern, model (view)-controller (Controller)
jsp (view) + servlet (Controller) + javabean (model)
1, where model refers to the dao layer and data processing layer
2 , View is displayed through jsp
3. The controller layer abstracts the sending and receiving of requests through servlet

The execution process of SPRING MVC

  1. (DispatcherServlet) intercept user requests

The difference between springmvc and springboot

Spring is an "engine";
Spring MVC is an MVC framework based on Spring;
Spring Boot is a set of rapid development integration packages based on Spring4 conditional registration:

What is AOP? What do you use it for?

Use dynamic proxy to weave the logic that needs to be executed in the aspect.
Purpose:
JWT authentication interception, log processing, spring transaction implementation principles are all AOP

Two realizations of AOP?

JDK dynamic proxy and CGLIB dynamic proxy

Under what circumstances can't act as an agent?

The JDK proxy mode must have an interface.

How does Spring solve the problem of circular dependencies

Propagation characteristics of spring transactions

 required:如果存在事务,就用当前事务,没事务,创建事务
 supports:如果存在事务,就用当前事务,没事务,则非事务的执行(但增删改根本存不上)
 mandatory:如果存在事务,就用当前事务,没事务,抛出异常
 required_new:总是开启一个新的事务(很少用)
 not_support:总是非事务的运行,有事务也是非事务运行
 never:如果有一个事务,则抛出异常
 nested:如果一个活动的事务存在

springMVC process:

(1): The user request is sent to DispatcherServlet, and DispatcherServlet calls HandlerMapping processor mapper;

(2): HandlerMapping finds the corresponding processor according to xml or annotation, generates the processor object and returns it to DispatcherServlet;

(3): DispatcherServlet will call the corresponding HandlerAdapter;

(4): HandlerAdapter calls a specific processor to process the request after adaptation, generates ModelAndView and returns it to DispatcherServlet

(5): DispatcherServlet passes ModelAndView to ViewReslover to parse the generated View and return it to DispatcherServlet;

(6): DispatcherServlet renders the view according to View;

The principle of SpringBoot automatic configuration

@EnableAutoConfiguration find the configuration file of META-INF/spring.factories (the beans that need to be created are in it)
read the spring.factories file in each starter

Core annotations of Spring Boot

The core annotation is that @SpringBootApplication consists of the following three types

@SpringBootConfiguration: Combines the @Configuration annotations to realize the function of the configuration file.
@EnableAutoConfiguration: Turn on the automatic configuration function.
@ComponentScan: Spring component scanning.

Mybatis first level cache and second level cache

The first level cache is turned on by default and cannot be turned off.

The first level cache refers to the principle of SqlSession level caching: the data structure used is a map, if there are commit operations (modification, addition, deletion) in the middle of the
two , the first level cache area in this sqlsession is all clear of the second level cache. Cache across SqlSessions. It is a mapper-level cache; Principle: It is implemented through CacheExecutor. CacheExecutor is actually a proxy object of Executor

Problems in traditional JDBC development

1. Frequent creation and release of database connection objects can easily cause a waste of system resources

Disadvantages of Hibernate and JPA

SQL statements can be generated based on rules, but the configuration of many-to-one and one-to-many relationships is more troublesome, and complex SQL is not easy to write

How is the mapping between the Xml mapping file of Mybatis and the internal data structure of Mybatis mapped?

The tag requestMap
tag will be parsed as a ResultMap object.
Each,,, and tag will be parsed as a MappedStatement object.

What label does dynamic sql use?

foreach,trim

What label is used for the unique id auto-increment primary key

selectkey

How does Mybatis perform paging? What is the principle of the paging plug-in?

Through the paging plug-in, SQL is intercepted in memory, limit statements are added, and physical paging is performed

The difference between # and $ in Mybatis

# Treat the incoming data as a string, and add a double quotation mark to the automatically incoming data.
$ Directly display the incoming data and generate it in sql

How to prevent SQL injection or1=1

#Method can prevent SQL injection to a large extent.

When using Mybatis, how to call SQL when calling DAO (Mapper) interface

mapper interface, mapper interface implementation class, mapper.xml file
. The full class name of the mapper interface is the value of the namespace in the .xml mapping file.
The method name of the interface is the id value of the MappedStatement in the mapping file. The parameters in the interface method are Parameters passed to sql

The working principle of the Dao interface is the JDK dynamic proxy. Mybatis will use the JDK dynamic proxy to generate a proxy proxy object for the Dao interface when Mybatis runs. The proxy object proxy will intercept the interface method and execute the sql represented by the MappedStatement instead, and then return the sql execution result.

In the Xml mapping file of Mybatis, can the id be repeated for different Xml mapping files?

For different Xml mapping files, if the namespace is configured, the id can be repeated ;

Mybatis's primary and secondary cache

spring cloud

Registration center Eureka, load balancer Ribbon, client calling tools Rest and Feign, distributed configuration center Config, service protection Hystrix, gateway Zuul Gateway, service link Zipkin, message bus Bus, etc.
Eureka service registration center, there are two pieces of client and server
Eureka Client: responsible for registering the information of this service in Eureka Server
Eureka Server: registration center, there is a registry, which saves the machine and port number of each service

Feign service consumers
Calls between various microservices, using @FeignClient annotations to achieve
REST style encapsulation, GRPC RPC calls

Fuse load balancing for hystrix services
: ribbon, nginx

Hystrix principle (to be investigated)

By maintaining a thread pool of its own, when the thread pool reaches the threshold, the service degradation is initiated and the fallback default value is returned

Why do we need hystrix circuit breaker

Prevent avalanches, release resources in time, prevent more cascading failures in the system, and isolate failures and delays to prevent the failure of a single dependency from affecting the entire application;

.How to operate a LINUX server

You need to rely on the Linux server to install the ssh server.Generally, the port of this ssh service is 22, and you
need to rely on the Linux server to install the sftp server.Generally, the port of this sftp service is 25.

Use the ssh client to connect to the linux server and operate the linux server through commands.Use the
sftp client to connect to the sftp server to upload and download files (installation package, modified file upload)

LINUX commonly used commands

Pwd Get the current path
Su Switch user
Ll View files and directories in the current directory
Tail View files
Rm -rf Delete files
Vi Modify files
Mv Move/rename files or folders
Mkdir Create folders
Rm -f Delete folders
Tar Pack / unzip
Grep Find the information you want

Guess you like

Origin blog.csdn.net/u010010600/article/details/109203708