Java interview questions (Spring)

Spring

1. Talk about the understanding of Spring

Spring is an open source framework; it is a cornerstone that can be seamlessly integrated with other frameworks; it was born to simplify enterprise development, making development more elegant and concise; it is an IOC and AOP container framework.

Advantage:

        1. Strong ecosystem

        2. Low intrusive design, low code pollution

        3. Independent of various servers

        4.ioc reduces the complexity of business objects and improves the decoupling between components

        5. Highly open, you can freely choose all or part of Spring

2. How does Spring simplify development?

        1. POJO-based lightweight and minimally invasive programming

        2. Loose coupling through dependency injection and interface orientation

        3. Declarative programming based on aspects and conventions

        4. Reduce boilerplate code through aspects and templates

3. Understanding of AOP

        AOP is called aspect-oriented programming. For example, business A and business B now need the same operation. In the traditional method, we may need to add relevant operation codes to both A and B. However, using AOP can only write the code once, and A and B share the same operation. this code. Moreover, when A and B need to add new operations, they can flexibly add new business logic implementations without changing the original code.

4. Understanding of IOC

        IOC is called inversion of control; in the traditional development method, an object is initialized through the new keyword; the idea of ​​ioc is to instantiate an object through a container. Which object we need is taken out directly from the ioc container; the right to create the object is handed over to the ioc container.

        Reduced coupling between the two; easy resource management.

5. The bean scope supported by Spring

  1. Singleton: By default, the ioc container only creates one Bean instance, and returns the same instance each time;
  2. Prototype: The ioc container creates multiple Bean instances, each return is a new instance;
  3. request: only works on http requests, each request will create a new instance;
  4. session: only used for http sessions, the same session shares a bean, and different sessions use different instances;
  5. global-session: only for http session, all sessions share one instance;

6. Spring's transaction propagation mechanism

Multiple transaction methods call each other, how does the transaction propagate among these methods? Seven different propagation features are provided to ensure the normal execution of transactions:

  1. If there is a transaction, join it, if there is no transaction, create one (default);
  2. Join if there is a transaction, if there is no transaction, execute in a non-transactional method
  3. Join if there is a transaction, and throw an exception if there is no transaction
  4. Create a new transaction, suspend the transaction if it exists
  5. Execute in a non-transactional manner, suspending the transaction if it exists
  6. Do not use transactions, throw an exception if a transaction exists
  7. If there is a transaction, execute it nested in the transaction

7. When will Spring's transactions fail?

  1. Self-invocation occurs, and the class uses this to call the method of this class
  2. The method's access modifier is not public
  3. The database does not support transactions
  4. The object is not managed by the spring container
  5. An exception was thrown, the transaction was not rolled back

8. Spring bean life cycle

9. Is the Bean thread-safe?

        Safe if the bean is stateless

        If the bean has state, it is unsafe

10. What is the difference between BeanFactory and ApplicationContext?

Both are bean containers;

  1. ApplicationContext is a subinterface of BeanFactory
  2. BeanFactory provides simple functions, ApplicationContext provides more complete functions
  3. BeanFactory injects beans in the form of lazy loading, and instantiates them only when they are used; ApplicationContext instantiates all beans when it starts
  4. BeanFactory is created programmatically, and ApplicationContext is created programmatically and declaratively

11. The implementation principle and isolation level of spring transactions

The implementation of the transaction:

        1. Programmatic: control logic through code

        2. Declarative: implemented through annotations

Principles of transaction implementation:

        In fact, the operation of the transaction should be controlled by the database, but in order to facilitate the user to operate the business logic, spring has extended the implementation of the transaction function. Generally, we rarely use programmatic transactions, and more by adding @Transactional Annotations are used for implementation. When this annotation is added, the automatic function of the transaction will be closed, and there is a spring framework to help control it.
        In fact, transaction operation is a core embodiment of AOP. When a method is annotated with @Transactional, spring will generate a proxy object based on this class and use this proxy object as a bean. When using the method of this proxy object, if there is a transaction Processing, then the transaction will be automatically submitted to the relationship first, and then the specific business logic will be executed. If there is no exception in the execution logic, then the proxy logic will be submitted directly. If any exception occurs, then the rollback operation will be performed directly. Of course, the user You can control which exceptions are rolled back.

isolation level:

  1. Uncommitted read: may see uncommitted data in other transactions (dirty read)
  2. Committed read: can read the data of the committed transaction (non-repeatable read)
  3. Repeatable reading: It can ensure that the results of multiple queries of the same transaction are consistent (phantom reading)
  4. Serialization: Enforce transaction ordering so that there will be no conflicts; execution efficiency is low, and there are not many scenarios used

12. What is the startup process of the Spring container?

       1. Initialize the Spring container

        2. Register the BeanDefinition of the BeanPostProcessor configuration class into the container

        3. Call the refresh() method to refresh the container

13. What design patterns does Spring use

        Factory, Adapter, Visitor, Decorator, Delegation, Proxy, Observer, Policy, Template, Chain of Responsibility

14. The way of spring injection

  1. constructor injection
  2. set method injection
  3. annotation injection

15. The difference between bean objects and ordinary objects

        Most of them are the same, but the names in different processes are different; after ordinary objects go through processes such as dependency injection, their properties are assigned and become Bean objects.

16. What is bean autowiring? what are the ways

The automatic assembly of beans refers to the property value of the bean is searched in the container through certain rules and methods when injecting, and set to the specific object property;

Way:

        1. no: default

        2.byName: Automatically assemble according to the attribute name

        3.byType: Automatic assembly according to type

        4.constructor: in the byType method of the constructor parameter

        5.autodetect: If the default constructor is found, use the automatic assembly construction; otherwise use the automatic assembly by type

17. How is dependency injection implemented?

        Judge the object constructed without parameters to see which attributes have @Autowired annotations on them. If there are annotations, use reflection to assign values ​​to the attributes in the object of the current Service class

SpringMVC

1. Overview of springmvc. Advantages of springmvc

        SpringMvc is a module of spring, a framework based on MVC, without the need for an intermediate integration layer to integrate

advantage:

  1.  It is based on component technology

  2. Doesn't depend on servlets

  3. Any view can be used

  4. easy to expand

2. The operating principle of springmvc

3. The workflow of springmvc

4. The core components of springmvc

        1. Front-end controller: responsible for receiving requests, distributing them, and responding to clients

        2. Processor mapper: according to the uri to match and find what can be processed Handler, and will package the interceptor involved in the request Handlertogether

        3. Processor adapter: According to HandlerMappingthe found Handler, the adaptation executes the correspondingHandler

        4. View parser: According to the Handlerreturned logical view/view, parse and render the real view

        5. Request processor: the processor that handles the actual request

5. Common annotations of springmvc

        @Component: refers to various components

        @Controller: control layer

        @Service: business layer

        @Repository: data access layer

        @Bean: Import annotations in third-party packages

        @Import: components imported into the container

6. Difference between @Autowired and @Resource

Both are annotations for injection.

        1. @Autowired is an annotation defined by Spring; @Resource is an annotation defined by java

        2. @Autowired searches by type, if there are more than one, then search by name; @Resource searches by name, if it does not exist, then search by type

        3. @Autowired only supports setting 1 parameter, while @Resource supports setting 7 parameters

        4. @Autowired supports property injection, constructor injection and Setter injection, while @Resource only supports property injection and Setter injection

7. Is the controller of SpringMvc a singleton mode? If so, what is the problem and how to solve it?

        singleton pattern

Problem: There are thread safety issues when multi-threaded access

Solution: Do not define member variables in the controller. If you must define a member variable, set it as a multi-instance mode through annotations.

Guess you like

Origin blog.csdn.net/qq_35056891/article/details/130166251