The third day basic framework


1. Introduce spring
The core function of spring is to integrate the framework and simplify development;
spring achieves the above effects through the two cores of IOC and AOP:
1. First of all, IOC, Spring provides a container for creating objects, initializing objects, and managing objects , that is, the control of the object is handed over to the container, and
the object is changed from active new to passive acquisition from the container. This is called IOC inversion of control. We call this container an IOC container;
2. The IOC container also maintains objects and objects When we use objects, we can not only obtain them directly from the IoC container,
but also bind all the dependencies to the obtained objects, which is called DI dependency injection;
the other is AOP, which is actually a The idea, that is, the idea of ​​aspect-oriented programming, spring provides an implementation, which can enhance the code function without invading the original code
; in fact, it realizes the normal return before the method is executed through the dynamic proxy technology.
After that, after the exception is thrown, and after the execution is completed, do some things in each stage, so as to realize the enhancement of the method of teammates;
AOP five notification types: before the method is executed, after the normal return, after the exception is thrown, and after the execution is completed, and A
surround notification on top of the front four

2. Tell me about your understanding of dynamic proxy? Commonly used dynamic proxy implementation?


1. Provide a proxy for an object, and the proxy object controls access to the real object. The proxy mode is a structural design mode

2. There are two ways of dynamic proxy:
 JDK dynamic proxy: use the reflection mechanism to generate an anonymous class that implements the proxy interface, and call
InvokeHandler to process it before calling the specific method.
 CGLIB dynamic proxy: use the ASM (open source Java bytecode editing library, bytecode operation) open source package to
load the class file of the proxy object class, and modify its bytecode to generate subclasses for processing.

3. Talk about the life cycle of spring bean?
The spring life cycle is the process of a bean from creation to destruction. The details are as follows.
When spring creates a bean, it first instantiates the bean according to the bean definition and some information configuration, and then if the bean
implements a pre-processor or a post-processor After the interface, they will implement their corresponding methods. For example, if the bean implements the
ApplicationContextAware() interface, the setApplicationContext() method will be called. The other specific implementation methods
are not clear. Anyway, some implementations will be executed. Once realized, execute such a sequence of judgments. In the end,
if the bean implements the destroyed interface, the bean will be destroyed, and if it is not implemented, it will be kept.
4. Is the object of Spring a singleton or multiple instances by default? Does the singleton bean have thread safety issues
?
The default singleton is not thread-safe, so manually change the singleton to prototype.

4. What are the common annotations of Spring?
1. @Component
@Service
@Repository
@Controller
is used to write on the class, tell spring, create the object of this class, and load it into the IOC container
@Bran is used to type on the method and define the use of third-party beans
2. @Autowired - Used to autowire dependencies in spring beans. Automatically inject beans by type. Used in conjunction with
the @Qualifier annotation, beans can be injected by name.
3. @Qualifier - Used together with @Autowired, when there are multiple beans of the same type,
the requirement of injection according to name can be realized.
4. @Scope - used to configure the scope of spring beans.
5. @Configuration, @ComponentScan and @Bean - for java-based configuration.
6. @Aspect, @Before, @After, @afterReturn, @afterThrow, @Around, @Pointcut - for
aspect programming (AOP)

5. How does Spring implement declarative transaction control, and talk about its principles?
Spring supports programmatic transaction management and declarative transaction management in two ways!
1. Programmatic transaction control: TransactionTemplate needs to be used for implementation. This method is intrusive to business code
, so it is rarely used in projects.
2. Declarative transaction management: Declarative transaction management is based on AOP. Its essence is to intercept the method before and after the method through the AOP function,
and weave the function of transaction processing into the intercepted method, that is, add a transaction before the start of the target method, and
submit or roll back according to the execution status after the target method is executed. affairs.
The biggest advantage of declarative transactions is that there is no need to mix transaction management codes in business logic codes. You only need to declare
relevant or use @Transactional annotations to apply transaction rules to the business in logic.

6. Do you understand the Spring three-level cache? What is the role of cache at all levels?

1. Level 1 cache: Cache complete bean objects that have gone through a complete declaration cycle and have been initialized; however
, level 1 cache cannot resolve circular dependencies
2. Level 2 cache: Cache early semi-finished bean objects (the life cycle has not yet passed End);
used to solve the circular dependency problem in the object creation process
The Bean exposed in the early stage is actually the Bean that solves the circular dependency.
Early means that it has not been fully created, but due to circular dependencies, this kind of Bean needs to be exposed in advance. In fact, the beans exposed early and the fully created beans are the same object,
but the annotations in the early beans may not be processed, and the complete beans have been processed, but they still refer to the same object , but they are in different states during the Bean creation process.
3. Three-level cache:
ObjectFactory is cached, which represents the object factory, which is used to create an object; if there is no AOP, two-level cache can indeed solve the problem of circular dependency. If AOP is added, two-level cache cannot resolved
.
The proxy factory object of ObjectFactory<?> type is stored in the third-level cache, which is used to deal with the circular dependency problem when there is AOP. The
ObjectFactory object corresponding to each Bean is stored.
By calling the getObject method of this object, you can get it Early exposed Bean.
Note: A very important detail here is that the third-level cache will only take effect for single-case Beans.
Multiple instances cannot use the third-level cache. It can be seen from the class name DefaultSingletonBeanRegistry where the third-level cache is located. Only It is effective for SingletonBean, that is, singleton Bean.
Note: spring cannot solve the circular dependency problem caused by the constructor


Seven, springmvc
(1) Introduce springMVC?
Spring MVC is a Java-based lightweight web framework that implements the request-driven type of the MVC design pattern. By separating the
Model, View, and Controller, the responsibility of the web layer is decoupled, and complex web applications are divided into logically clear sections. part
, simplify development, reduce errors, and facilitate the cooperation among developers in the group.
2. Tell me about the request process of springMVC?
1. Initiate a request to the front-end controller (DispatcherServlet)
2. The front-end controller requests HandlerMapping to find the Handler, and can match the corresponding
controller method according to the requested uri and request method
3. The processor mapper HandlerMapping returns the found Handler to the front-end controller The controller method, and then call the processor adapter
to call the controller method
4. The processor adapter executes the controller method
5. The controller method returns the result to the adapter after the execution is completed, and the processor adapter encapsulates the returned result into a ModelAndView
object and returns it to the front controller ,ModelAndView is an underlying object of the springmvc framework, including Model (data model
) and view (view object)
6. The front controller requests the view resolver to perform view resolution, and obtains the actual view according to the logical view name in the View object Figure 7.
The
front controller performs view rendering, and the view rendering renders the model data model into the actual view file to generate the final view
Figure, back to a browser

 

(2) Quickly say more than five common annotations of springMVC?
1. @RequestMapping: Used to map request paths, which can be defined on classes and methods. When used on a class, it means that
all methods in the class use this address as the parent path.
2. @RequestBody: The annotation realizes receiving the json data of the http request body and converting the json into a java object.
3. @RequestParam: Specify the name of the request parameter to receive the parameter after the url question mark, such as /user?id=1
4. @PathViriable: Obtain the request parameter (/user/{id}) from the request path and pass it to the method Formal parameters
5. @ResponseBody: The annotation implements converting the object returned by the controller method into a json object to respond to the client.
6. @RequestHeader: Get the specified request header data

(3) What is the difference between the interceptor in springMVC and the filter in Servlet?
Filter: Depends on the servlet container, based on function callback in implementation, can filter almost all requests
Interceptor: Relies on the SpringMVC framework, which is an application of aspect-oriented programming (AOP).

(4) How is springMVC's unified exception handling and interceptor implemented?

1. Both of them are implemented based on AOP aspect-oriented programming technology;
unified exception handling only needs to define one aspect, the entry point is all controller methods,
after the notification type is selected to throw an exception, all exceptions can be handled in this way; of course springMVC has already implemented it for us, but we should know
how it is implemented.
2. Interceptor: The implementation is also based on AOP. You only need to define an aspect. For the entry point, you can configure the
controller interface that needs to be intercepted, and select the notification type to surround, so that before the processor method is executed, after the normal return,
after (Returning normally and throwing an exception belong to the end of the method), and you can do your own business


Eight, mybatis

(1) Introduce Mybatis?
MyBatis is an excellent persistence layer framework that supports custom SQL, stored procedures, and advanced mapping. MyBatis eliminates
almost all JDBC code and the work of setting parameters and getting result sets. MyBatis can operate the database through simple XML annotations
(2) What is the difference between #{} and ${} in MyBatis?
1. #{} is precompilation processing, ${} is string replacement.
2. When Mybatis processes #{}, it will replace #{} in sql with ?, and call the set method of PreparedStatement to assign a value; 3. When
Mybatis processes ${}, it replaces ${} with a variable value.
4. Using #{} can effectively prevent SQL injection and improve system security.
Note: We use #{} for normal parameter passing, only some special cases need to use it, such as the parameter is field name, table name, or
sql keyword, etc., only ${} can be used at this time

(3) How to write mybatis fuzzy query like statement?
 name LIKE CONCAT('%',#{keyword},'%')

(4) What should I do if the attribute name in the entity class is different from the field name in the table?
Type 1: By defining the alias of the field name in the SQL statement of the query, the alias of the field name is consistent with the attribute name of the entity class.
Type 2: Use <resultMap> to map the one-to-one relationship between field names and entity class attribute names.
Type 3, open mybatis hump naming automatic matching mapping

(5) Talk about the difference between resultType and resultMap?
If the column name in the database is the same as the attribute name in the encapsulated entity class, use ResultType; if not, use resultMap, and then
resultMap configures the one-to-one correspondence between the table and the entity class

(6) Tell me about your understanding of mybatis dynamic sql? Commonly used tags say something.
Mybatis dynamic sql allows us to write dynamic sql in the form of tags in the Xml mapping file to complete the function of logical judgment and dynamic
splicing of sql:
sql: a reusable statement block that can be referenced by other statements.
insert, update, delete, select: Map insert, update, delete, and query statements respectively.
if, choose, otherwise, when: used to conditionally include part of the where clause in the query.
trim, where, set: used to deal with the splicing of query condition statements.
foreach: Used to traverse a collection when building an IN conditional statement.
Its execution principle is to use OGNL to calculate the value of the expression from the sql parameter object, and dynamically splice sql according to the value of the expression to complete the
function of dynamic sql.
(7) How does mybatis realize the paging function?
1. Look at the database, you can use the paging keywords that come with the database, such as the limit keyword in mysql, but if this is the case, the
coupling with the database is serious, and the problem will be relatively large if you replace the database later.
2. You can introduce the PageHelper paging plug-in, which can Shield the problem of different database dialects


Nine, mybatisfplus

(1) MyBatis-Plus is an enhanced tool for MyBatis (opens new window). On the basis of MyBatis, only enhancements are made without changes, and it was born to simplify development and improve efficiency.

(2) How does mybatisPlus implement optimistic locking? Under what circumstances can optimistic locking be used?
Optimistic lock implementation method:
1. When fetching the record, get the current version
2. When updating, bring this version
3. When performing the update, set version = newVersion where version = oldVersion
4. If the version is wrong, the update will fail
No. 27 / 64 simply means:
1. Add a version field to the database . 2. When fetching a record, get the
current version . Data update is to prevent multiple users from modifying the same piece of data at the same time. It is suitable for scenarios with more reads and fewer writes, which can improve throughput . Scenes such as spikes


Ten, springboot
(1) The relationship between spring and springboot?
Spring can help us achieve framework integration and rapid development; but we need to do ssm framework integration and a lot of configuration writing; springboot strengthens the use of spring;

(2) The automatic assembly principle of springboot?


1. This is the case, because springboot will only scan the beans under its package and its sub-packages by default, but it cannot scan the beans in the third-party jar package. In order to solve this problem, the automatic assembly function was added , the bottom layer of spring will scan the beans configured under spring.factories in the meta-info folder under the classpath path,
and add them to the ioc container, which can be injected through the @AutoWied annotation when needed.

(3) What are the ways to run a SpringBoot project?
1. Run directly with jar -jar
2. Run the main method during the development process
3. You can configure the plug-in, pack the springboot project into a war package, and deploy it to Tomcat to run
4. Run maven spring-boot directly with the maven plug-in: run
(4) , Which is the core annotation of Springboot?
Which annotations does he consist of?
The core annotation of Spring Boot is @SpringBootApplication, which consists of several annotations: 
@SpringBootConfiguration: Combining - @Configuration annotation to realize the function of configuration file;
@EnableAutoConfiguration: Turn on the automatic configuration function, or turn off an automatic configuration option
@ComponentScan: Spring component scan


 

Guess you like

Origin blog.csdn.net/weixin_71921932/article/details/129913987