[Interview] Spring Framework face questions ready

Caused his interview preparation questions, add your own and integrate other people sharing!

1. Works of Spring

spring is in accordance with well-built design patterns, which implements the factory class factory pattern, this class called BeanFactory (Interface), usually a subclass ApplicationContext in the program (also interfaces).

The core spring is the IOC (Inversion of Control) containers, IOC is also a programming ideas for achieving decoupling between the modules in Spring Its role is to create objects, control and destruction maintenance life cycle. IOC: the object is created, initialization, destruction to spring to manage, rather than controlled by the developer, to realize inversion of control.

spring is a big factory class, spring feature is based on the configuration, create an instance of an object by an element in its configuration file.

According to business logic, objects that are often not independent, to create an object often involves the creation of another object, of course, the object should be the responsibility of the IOC container, responsible way is dependency injection DI, achieved through reflection. Injection three ways: (1) interface injection (2) injection constructor (3) Setter injection method.

2. the Spring of the advantages and disadvantages?

Advantages:
low-intrusive design
independent of the various application servers
dependency injection on the transparent component relationships, reducing the coupling
characteristics for common tasks section allows the centralized processing
and integration of third party frameworks good

Drawback:
the Spring based on a number of xml configuration file, so that we spent a lot of time on configuration, slow down the progress of the development, after the advent of SpringBoot promote the code over configuration solves this problem.

3. the Spring of IOC and AOP

1) IoC (Inversion of control) : Inversion of Control
concepts: the steering control of the vessel by the object itself; to create a configuration file from the instance of the container and creates a dependency between the various instances.
Core: bean factory; In Spring, each instance of the bean plants created referred bean

2) AOP (Aspect-Oriented Programming ): Oriented Programming
Concepts: AOP, generally referred to as aspect-oriented, object-oriented, as a supplement, for those service independent, but have an impact on the behavior of a plurality of common objects and logic to extract and packaged as a reusable module that is named "slice" (Aspect), to reduce the code system is repeated, reducing the coupling between modules, while improving maintainability of the system. Permissions can be used for authentication, logging, transaction processing.

Agent of two ways:
static proxy:
the preparation of the proxy class, respectively, for each specific class.
Write a proxy class for an interface.
Dynamic Agent:
prepare a InvocationHandler for one section, and then borrow Proxy class JDK reflection package various interfaces dynamically generate a proxy class.

4. the Spring of the three kinds of injection method IOC

setter
interface
constructor

5. The the Spring modules 7

title
Spring Core: Core library, IOC provide services.

Spring Context: provide a framework for Bean-style access methods, as well as enterprise-class features (JNDI, scheduled tasks, etc.).

Spring AOP: AOP services.

Spring DAO: JDBC abstraction to simplify the process of data access exceptions.

Spring ORM: Support for existing ORM framework.

Spring Web: provides basic comprehensive Web-oriented features, such as multi-file upload.

Spring MVC: Model-View-Controller provides facing Web application implementation.

6. What Spring can?

Spring is a lightweight container frame IOC and AOP.

7. BeanFactory and ApplicationContext What is the difference?

①BeanFactroy uses a form of lazy loading to inject Bean, that is only in use when a Bean (call getBean ()), only to load the Bean is instantiated.
②ApplicationContext, it is when the container starts, disposable created all of Bean.
③ with respect to basic BeanFactory, ApplicationContext only downside is take up memory space. When the application is configured more Bean, the program starts slow.

8. explain Spring supports several bean scopes

Spring bean container can be divided into five ranges:

(1) singleton: By default, each container is only one instance of the bean, the mode is maintained by a single embodiment BeanFactory itself.

(2) prototype: a request instance for each bean.

(3) request: Create an instance for each request for a network, after the completion of the request, the bean will fail and garbage collected.

(4) session: the request is similar to the range, to ensure that each session has an instance of the bean, after the session expires, bean failure will follow.

(5) global-session: global scope, global-session related and Portlet application. When your application deployment in the Portlet container, it contains a lot portlet. If you want to declare all of the portlet shared global memory variables, then it needs to be stored in global variables in the global-session. Servlet session scope and global scope in the same effect.

9. The the Spring automatic assembly

There are five types of automatic assembly Spring Framework xml configuration:

(1) no: the default mode is not the automatic assembly, the assembly is performed manually setting bean ref attribute.

(2) byName: name of the bean by automatic assembly, the same as if a bean property of the bean to another name, to carry out automatic assembly.

(3) byType: automatically assembled by the data type parameter.

(4) constructor: using the constructor assembly, and the parameters are assembled by the constructor byType.

(5) autodetect: automatic detection, if the constructor, construct assembled by automatic manner, or using automated assembly byType manner.

Based on annotation by:
using automatic assembly @Autowired annotation specified bean.

10. The difference between the @Autowired and @Resource

(1) @Autowired default injection assembly in accordance with the type, default it requires dependent objects must exist (be it the required property is set false).

(2) @Resource default is to assemble injected by name, only if can not find a name that matches the type of bean will be assembled in accordance with the injection.

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

1) factory pattern: BeanFactory factory pattern is simply reflected, for instance to create an object;

(2) Singleton: Bean singleton default mode.

(3) Agent Mode: Spring AOP function uses the JDK dynamic proxy and CGLIB bytecode generation technology;

(4) Template Method: to solve the problem of code duplication. For example. RestTemplate, JmsTemplate, JpaTemplate.

(5) observer pattern: definition object dependency relationship of many of the key, when a state of the object changes, all objects that depend on it are notified braked updated, as implemented in Spring listener -ApplicationListener .

12. the Spring transaction propagation behavior

① PROPAGATION_REQUIRED: If no transaction creates a new transaction, if the current transaction exists, joined the transaction, the setting is the most common settings.

② PROPAGATION_SUPPORTS: support for the current transaction, if the current transaction exists, joined the transaction, if the transaction does not currently exist, with regard to non-transactional execution. '

③ PROPAGATION_MANDATORY: support for the current transaction, if the current transaction exists, joined the transaction, if the transaction does not currently exist, an exception is thrown.

④ PROPAGATION_REQUIRES_NEW: create a new transaction, regardless of the current deposit transaction does not exist, creates a new business.

⑤ PROPAGATION_NOT_SUPPORTED: performing an operation to a non-transactional way, if the current transaction exists, put the current transaction pending.

⑥ PROPAGATION_NEVER: to perform non-transactional way, if the current transaction exists, an exception is thrown.

⑦ PROPAGATION_NESTED: If the current transaction exists, it is executed within nested transactions. If no transaction, press execute REQUIRED property.

13. the Spring isolation level

① ISOLATION_DEFAULT: This is a PlatfromTransactionManager default isolation level, use the default database transaction isolation level.

② ISOLATION_READ_UNCOMMITTED: read uncommitted, allow another transaction can see the data that uncommitted transactions.

③ ISOLATION_READ_COMMITTED: Read Committed to ensure the transaction after a modification of the data submitted in order to be read by another transaction, and can see the transaction update existing records.

④ ISOLATION_REPEATABLE_READ: repeatable read, modified to ensure that a transaction after the data submitted in order to be read by another transaction, but can not see the transaction update existing records.

⑤ ISOLATION_SERIALIZABLE: during the execution of a transaction in another transaction can not see updates made to the database.

14. explain a few terms Spring AOP inside

(1) cut (Aspect): the extracted common module, you may cross multiple objects. In Spring AOP, aspects are implemented using class (style based mode) or @AspectJ annotation to be implemented in the general category.

(2) a connection point (Join point): refers to a method, in Spring AOP, a connection point always represents a method execution.

(3) a notification (Advice): a particular action join point (Join point) executing on. Different types of advice, including "around", "before" and "after" and the like notified. Many AOP frameworks, including Spring, are based on advice as an interceptor model, and maintain a connection with the center point of the interceptor chain.

(4) entry point (Pointcut): refers to the starting point we have to define which intercept Join point right. By pointcut expression specified intercept method, such as specifying interception add *, search *.

(5) into (Introduction) :( type declarations also referred to as an internal (inter-type declaration)). Declare additional methods or certain types of fields. Spring allows the introduction of new interfaces (and a corresponding implementation) to any object in the proxy. For example, you could use an introduction to the bean implementation IsModified interface, to simplify caching.

(6) target object (Target Object): being notified by one or more section (aspect) (advise) object. Some people call it being notified (adviced) objects. Since Spring AOP is achieved by running a proxy, this object is always to be a proxy (proxied) objects.

(7) weaving (Weaving): refers to the enhancement applied to the target object to the process of creating a new proxy object. Spring is done at runtime weaving.

15. A five advice of Spring

before: Before advice, a method is called before execution.
after: notification call after method execution, regardless of method of execution was successful.
after-returning: Advice to be executed only if the method completes successfully.
after-throwing: Advice to be executed in the method throws an exception exit.
around: before method execution and notification after call.

Guess you like

Origin blog.csdn.net/weixin_43266090/article/details/88937440