Year-end summary of the interview, I collected 21 classic Spring face questions, additional answers worth collecting

This article is reproduced from: the year-end summary of the interview, I collected 21 classic Spring face questions, additional answers worth collecting


1. Do you understand Spring Framework (features)? Which modules Spring framework?

  • Spring, A method for simplifying an open source framework for enterprise-class application development.
  • Simplify development: it made a common API packages, such as packages for JDBC using Spring JDBC to access the database, you do not need to consider how to get connected and closed questions.
  • Decoupling: Spring to help us manage dependencies between software, the coupling between such objects would be lower, so it has been improved maintainability.
  • Other Integration Framework: to facilitate the expansion and optimize its function, for example, and the integration Mybatis.
  • Spring framework is well-built according to the design pattern, which makes us in the development environment, can easily use the framework, regardless of how the background is carried out.
  • Spring container Spring Framework is a core module, used to create a managed object, and initializing Alto destruction operations, and the dependencies between objects.
  • Spring framework has now grouped into a plurality of modules 20:

2. Spring advantage?

  • Simplify development, decoupling, other integrated frame.
  • Low intrusive design, code level contamination level.
  • Spring's DI mechanism reduces the complexity of the business object to replace and improve the decoupling between software.
  • Spring AOP supports some common tasks centralized management, such as: security, transaction, logs, etc., so that the code can better reuse.

3. What is Inversion of Control (IOC), what is the dependency injection (DI)?

  • IOC: that is, the dependencies between objects created by the container, the relationship between the object could have been created and maintained by our own developers, after we use the Spring framework, relationships between objects created and maintained by the container, the developers do allow the container to do, this is the inversion of control. BeanFactory interface is the core interface Spring Ioc container.
  • DI: When we use the Spring container, the container to create dependencies between objects by calling the set method or constructor.
  • Inversion of Control is the goal, dependency injection is a means to achieve our Inversion of Control.

4. Java implemented in three ways Dependency Injection?

  • Constructor injection
  • set injection method
  • Interface injection

5. Spring There are several configurations?

  • Based on the beginning of the configuration XML file format commonly used in this configuration file, and then use a series of bean definition configuration options and specialized application components. Spring XML configuration is to use a series of XML tags are supported by the Spring namespace to achieve.
  • Instead of the configuration bean elements as XML annotation-based approach can be used to configure annotations. It notes that some component scans commonly used dependency injection are: @Controller @Service @Autowired @RequestMapping @RequestParam @ModelAttribute @Cacheable @CacheFlush @Resource @PostConstruct @PreDestroy @Repository @Scope @SessionAttributes @InitBinder @Required @Qualifier
  • Scan components: base-package container will scan its assigned sub-packets of all the following packet classes, if the class has some specific comments, the container is included in management.
  • In some particular annotations added earlier classes: @Component general comment annotation @Service @Repository persistence service layer annotations, @Controller control layer annotations
  • Java-based configuration

6. Spring Bean's life cycle?

  • Spring means in the bean element is instantiated, and the process is destroyed. We initialization method specified by init-method property; specifies the methods of destruction through the destroy-method approach.
  • Note: Only when the Singleton scoped to be effective.

7. Explain what lazy loading?

  • By default, all will start after the container is scope for singleton bean is created; but some business scenarios we do not need it in advance are created;
  • In this case, we can set in the bean lzay-init = "true", so that, when the container starts, the scope of a single embodiment of the bean, is not created.

8. What is the explanation automated assembly?

  • The Bean is injected into a Property of Bean other, by default, the container does not automatic assembly, we need to manually set. Spring can be get by dependencies between bean injected into the Bean Factory manner, to achieve automatic assembly.
  • Auto Assembly recommended to use less, if you want to use, it is recommended to use ByName

9. Spring bean element in scope?

  • When you create a Bean instance by the Spring container when not only complete example of the strength of the bean, bean can also specify the scope. Spring bean element supports the following five kinds of scopes:
  • Singleton: Singleton, the entire spring IOC container, using the singleton bean defined only one instance.
  • Prototype: multi-mode embodiment, each prototype acquired by getBean method defined beans container, will generate a new instance of the bean.
  • Request: For each Http request, defined using the request will generate a new bean instance, only when the Web application, which scope is to be effective.
  • Session: For each Http Session, Bean defined using the session will produce a new instance.
  • Globalsession: each global Http Sesisonn, using the definition of the present session will produce a new instance.

10. Spring is in the bean thread safe it?

  • Spring framework does not encapsulate multi-threading of the bean singleton thread safety and concurrency problems, we need to consider the developers themselves.
  • But in fact, most of the Spring bean is not mutable state (for example: service category and class dao), all in a way singleton Spring bean thread safe. If there are multiple states bean words (such as: View Model objects), we need to consider their own thread-safety issues.

11. how to inject a Java Collection in Spring?

Spring provides a set of four kinds of configuration elements theory class:

  • lt; List &: the tag value list is used fitted with a duplicate values
  • lt; set &: set the value of this tag is used to assemble no duplicates
  • lt; map &: Branch to the tag for injecting the key-value pairs
  • lt; props &: This tag is used to support and string types of injection key pairs.

12. Spring Framework which are used in the design mode?

  • Proxy mode is the most used in the AOP.
  • Singleton, bean defined in Spring when the default configuration file is a singleton.
  • Factory mode, BeanFactory used to create an instance of an object.
  • Template method used to solve repetitive code.
  • Front controller, Spring provides DispatcherSerclet to the request for distribution.
  • View Help, Spring provides a set of JSP tags.
  • Dependency injection, which is used to wear BeanFactory / ApplicationContext interface core concept.

13. Spring event handling?

  • The core of Spring is ApplicatonContext, which is responsible for managing the complete lifecycle of the bean. Spring provides the following built-in event: ContextRefreshedEvent ContextStartedEvent ContextStoppedEvent ContextClosedEvent RequestHandleEvent
  • As Spring event handling is single-threaded, so if an event is issued, until and unless all recipients of the message obtained by the process is blocked and the process will not continue. Therefore, if an event handler is used, care should design your application.
  • Listening context event
  • Custom Event

14. Spring AOP (Aspect Oriented) programming principle?

  • AOP Aspect Oriented Programming, it is an idea. It is for the extraction of business process section, in order to achieve optimized code, the purpose of reducing code duplication. It is, for example, at the time of writing the business logic code, we have the habit of writing: logging, transaction control, access control and so on, each sub-module have to write the code, the code evident duplication. At this time, we use aspect-oriented programming ideas, the use of cross-cutting technology, the code will duplicate part, does not affect the main business logic part extracted and placed somewhere centralized management of calls. Forming a log cut, cut transaction control, access control section. Thus, we only need to deal with business logic relationship, that improve the work efficiency, but also makes the code becomes simple and elegant. This is the aspect-oriented programming ideas, it is an extension of object-oriented programming ideas.
  • AOP usage scenarios: cache, rights management, content delivery, error handling, lazy loading, record tracking, optimization, calibration, commissioning, persistence, resource pooling, synchronization management, control and other things. AOP related concepts: section (Aspect) connection point (the JoinPoint) notification (the Advice) pointcut (Pointcut) agent (the Proxy): weaving (Weaving)
  • Spring AOP's programming principles? JDK dynamic proxy proxy mechanism: only for classes that implement the interface to create agency. Cglib Agent: Agent for generating class does not implement the interface, the underlying application bytecode enhancement technology, subclass object generated current class.

15. Explain proxy mode (Proxy)

  • Proxy Mode: This is the proxy mode I do, I do, I handed over to an agent to complete. On the example, I produce a number of products, I do not sell myself, I entrust agents to help me sell, so that agents and customers to deal with, myself responsible for the production of major products on it. Use proxy mode, the need for this class, and the proxy class, the class and the proxy class together to achieve a unified interface. Then call on it in the main. This class of business logic is generally not change, in our time of need can continue to add proxy object, or modify the proxy class to implement business changes.
  • Acting mode can be divided into: static proxy Pros: can be done without modifying the function of the target object premise, the target extensions drawback: Because the original proxy class and to achieve a unified interface, agents will have a lot of class, class too more than once to increase the interface method, the target object and proxy object must be maintained. Type dynamic proxy (JDK agent / interface agent) proxy object, no need to implement the interface, generating a proxy object, the JDK using the API, the proxy dynamically construct objects in memory, we need to specify the proxy object / target object implements an interface . Cglib Agent Characteristics: Construction of a subclass object in memory, to achieve the expansion of the audiences.
  • Usage scenarios: when the code changes. Others do not just go and modify the code already written, if you need to modify it, this method can be extended by proxy. Hide a class, they can provide a proxy class when we want to extend a class function, you can use a proxy class when a class needs to provide different calling privileges for different callers, they can use a proxy class achieve. Class code amount reduction present time. We need to improve the processing speed of the time. For example, when we visited on a large-scale system, a generation examples will spend a lot of time, we can use proxy mode when you need to create an instance of it, so that we can improve the speed of access.

16. What is Spring MVC?

  • Spring MVC is a framework to simplify application development framework for web application development, which is part of the Spring, which are all the same and Struts2 presentation layer based on MVC architecture.
  • MVC (Model View Controller Model View Controller): This is a software framework for thinking, is a development model, the software is divided into three different types of modules, namely, models, views, and controllers. Model: encapsulate business logic for processing (Java class); view: for data display and user interface (the Servlet); controller: for coordination model and view (JSP); processing flow: The view request to the controller, the controller selects a corresponding process model; model processing result to the controller, the controller selecting the appropriate view to show a processing result;

17. Spring MVC five groups of keys?

  • Front-end controller (the DispatcherServlet)
  • Mapping processor (the HandlerMapping)
  • Processor (Controller)
  • Model and view (ModelAndView)
  • View resolver (ViewResolver)

The operating principle of 18. Spring MVC

  • Text Analysis: DispatcherServlet client requests submitted to a query from the one or more HandlerMapping DispatcherServlet controller, to find a processing request DispatcherServlet submit requests to the Controller Controller Controller call service logic processing, the query returns one or more ModelAndView DispatcherServlet ViewResoler view resolver find ModelAndView specified view view is responsible for displaying the results to the client

19. SpringMVC controller is not a singleton, and if so, what is the problem, how to solve?

  • It is a singleton, so when in multithreaded access have thread-safety issues, do not use synchronization, will affect the performance, the solution is not written in a field controllers inside.

20. SpringMVC how to set Redirects and Forwards?

  • In front of the return value plus "forword", so that you can achieve the results forwarded;
  • In front of the return value plus "redirect", you can make the return value redirection.

21. Spring MVC advantages:

  • View-based seamless integration framework, to facilitate testing using the IOC
  • Typical pure MVC framework, Struts is not entirely based on MVC framework
  • The tapestry is pure Servlet system

This article is reproduced from: the year-end summary of the interview, I collected 21 classic Spring face questions, additional answers worth collecting

Published 209 original articles · won praise 13 · views 10000 +

Guess you like

Origin blog.csdn.net/Java_supermanNO1/article/details/103163081