Spring is a comprehensive summary of the interview questions

1. What is Spring?

Spring is an open source Java EE development framework. The core functions of Spring Framework can be applied in any Java application, but there are better scalability of Web applications on the Java EE platform. Spring Framework's goal is to make the development of Java EE applications more simple, by using the POJO-based programming model to promote good programming style.

2, Spring What are the advantages?

Lightweight: Spring in terms of absolute size and transparency belong lightweight, basic version of the Spring framework only about 2MB.

Inversion of Control (IOC): Spring using inversion of control technology to achieve loose coupling. Dependence is injected into an object, instead of creating or looking for dependent objects.

Aspect Oriented Programming (AOP): Spring aspect-oriented programming support, while the business logic of applications and system services separate.

Container: Spring contain and manage the application object configuration and life cycle.

MVC framework: Spring web framework is a well-designed web MVC framework, to replace some of the good web framework.

Transaction management: Spring next to a local business to a global business (JAT) provides a unified transaction management interface.

Exception Handling: Spring provides a convenient API abnormality particular technique (thrown by JDBC, Hibernate, or JDO) into uniform, Unchecked exception.

3, Spring transaction implementation

Programmatic transaction management: This means that you can bring a great deal of flexibility programmatically manage affairs in this way, but it is difficult to maintain.
Declarative transaction management: This approach means that you can separate transaction management and business code. You only need annotations or XML configuration management affairs.
4, transaction management Spring Framework What are the advantages

It provides a unified programming model across different transaction API (such as JTA, JDBC, Hibernate, JPA, and JDO).
It provides a simple API rather than a series of complex transactions API (eg JTA). Is a programmatic transaction management
that supports declarative transaction management.
It can be well integrated and Spring's various data access technologies.
5, spring-defined transaction propagation rules

PROPAGATION_REQUIRED: support for the current transaction, if no transaction, we create a new business. This is the most common choice.
PROPAGATION_SUPPORTS: support for the current transaction, if no transaction is executed in a non-transactional way.
PROPAGATION_MANDATORY: support for the current transaction, if no transaction, throw an exception.
PROPAGATION_REQUIRES_NEW: New Transaction, if the current transaction exists, the current transaction pending.
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 is carried out with PROPAGATION_REQUIRED similar operations.
6, Spring underlying principle Affairs

Division processing unit --IoC
since spring solve the problem is localized single database transaction processing, the specific implementation first transaction processing unit is divided in the spring IoC. Various configurations of the transaction and will put ioc container (provided the transaction manager, the transaction set and the propagation characteristics of isolation mechanism).

AOP interceptors need transaction class
Spring transaction module is implemented by AOP transaction function declaration process, the specific operation (such as the configuration and implementation of read transaction, the transaction object abstract), with the use of AOP TransactionProxyFactoryBean interface function, generating a proxy object proxy, the proxy by TransactionInterceptor complete interception method, the knitting method of the transaction processing function of interception. Ioc container configuration read transaction attributes, data structures, internal conversion (The TransactionAttributeSourceAdvisor used) is required for the transaction spring, TransactionAttribute conversion data object representation.

To implement transaction processing (generation, commit, rollback, suspend)
the Spring entrusted to the specific transaction processor. Implement an abstract and adaptation. Specific transaction processor adapted: DataSource Data source supports, hibernate transaction support source data, JDO transaction support source data, JPA, JTA transaction support source data. These are supported by design PlatformTransactionManager, support AbstractPlatforTransaction a series of transactions. TransactionManager offers a range of commonly used data source support.

Combined
PlatformTransactionManager realized TransactionInterception interface let combined with TransactionProxyFactoryBean, form a system design Spring declarative transaction.

7, Spring MVC running processes

The first step: the controller initiates a request to the front end (the DispatcherServlet)

Step two: the front controller lookup request HandlerMapping Handler (can be configured according to xml, to find comments)

The third step: a processor mapper HandlerMapping Handler returns the controller to the distal end

Step four: the front controller calls the adapter processor to perform Handler

Step five: processor adapter to perform Handler

Step Six: Handler execution is complete return to the adapter ModelAndView

Seventh Step: a processor adapter returns the controller to the distal ModelAndView (ModelAndView springmvc underlying object is a frame, and comprising a Model view)

Eighth step: a front end view of the controller requests to the parser parses a view (a view resolved into real (jsp The logical view name))

Step 9: View View resolver returns the controller to the distal end

Step 10: a front end view rendering controller (model view rendering data (object ModelAndView) filled into the request field)

Tenth step: the response result to the user front-end controller

8, BeanFactory and ApplicationContext What is the difference?

ApplicationContext provides a solution to document information, a way to load resource files (such as images), they can send messages to monitor their beans. Further, the container or the operation of the beans, these operations must be programmatically bean processing plant may be treated in a manner stated in the application context. Implements the MessageSource application context, the interface is used to obtain local information, the actual implementation is optional.

Similarities: Both are loaded bean through xml configuration file, compared ApplicationContext and BeanFacotry, provides more extensions.

Different points: BeanFactory is lazy loading, if a particular property is not injected Bean, after BeanFacotry load until the first call to use getBean method will throw an exception; while the ApplicationContext in the initialization test itself, it is a good check depends whether the property is injected; so usually we choose to use ApplicationContext.

9. What is the Spring Beans?

Spring Beans are Java objects that make up the core of the Spring application. These objects instantiated by the Spring IOC container, assembly, management. These objects are created by the configuration of the metadata container, for example, create an XML file defined.

beans are created in the Spring beans in single cases. In the bean tag has an attribute of "singleton", if set to true, the bean is a single embodiment, if set to false, the bean prototype bean. Singleton property defaults to true. Thus, spring frame default to all bean singleton bean.

10, talk about the support of Spring bean scopes

Spring framework supports the following five different scopes:

singleton: Spring IOC there is only one container Bean instance, by way of a single instance of Bean exists.
prototype: a bean can define multiple instances.
request: each HTTP request will create a new Bean. The scope applies only to WebApplicationContext environment.
session: a definition of a HTTP Session Bean. The scope applies only to WebApplicationContext environment.
globalSession: the definition of a Bean with a global HTTP Session. The scope will also apply only to WebApplicationContext environment.
The default bean scope attribute is "singleton".

11, Spring singleton implementation principle

The Spring Framework is supported by way of a single example of a single embodiment registry were implemented, the registry is cached HashMap object, if the configuration information in the configuration file does not require the use of a single embodiment, will use the new Spring return way of example object instance.

12, Spring framework to explain the life cycle of bean

ApplicationContext container, Bean lifecycle flow shown above, the process is as follows:

1. First the container starts, the scope is instantiated and non-singleton lazy loading of the bean,

2. Bean definition information according to the configuration information, all of the injection properties,

3. If the Bean implements BeanNameAware interface, the callback setBeanName () method of this interface, passing the Bean id, in which case the Bean to get his id in the configuration file,

4. If the Bean implements BeanFactoryAware interface, the callback setBeanFactory the interface () method, passing the Bean BeanFactory, so that Bean gained the BeanFactory where their,

5. If the Bean implements ApplicationContextAware interface, the callback setApplicationContext () method of this interface, passing the Bean of ApplicationContext, so that you get the ApplicationContext Bean where their,

6. If Bean implements BeanPostProcessor interface, the interface will postProcessBeforeInitialzation callback () method,

7. If InitializingBean Bean implements the interface, the interface will afterPropertiesSet callback () method,

8. If configured Bean init-method methods, the method will perform the configuration init-method,

9. If there is Bean implements BeanPostProcessor interface, the interface will postProcessAfterInitialization Callback () method,

9 10. After the process, you can officially use the Bean, and for the scope of a singleton Bean, ioc container Spring in cached copy of the bean instance, and for the scope for the prototype of Bean, will be called each time new a new object, of the life cycle on to the caller management, and is no longer manage the Spring container

11. After the container is closed, if DisposableBean Bean implements the interface, the destroy () method will callback interface,

12. If Bean configured destroy-method approach method destroy-method configuration will perform, thus, the entire life cycle of the end of the Bean

13, Resource how it is to find, loaded?

Spring Resource interface is an abstract resource access policy, which itself does not provide access to any resources to achieve specific resource access done by the class that implements this interface - Each implementation class represents a resource access policies. Spring provides the following Resource interface implementation class is:

UrlResource: implementation class access to network resources.
ClassPathResource: access path in the class loader implementation class resources.
FileSystemResource: access to the file system implementation class resources.
ServletContextResource: access with respect to the implementation class ServletContext path where resources:
InputStreamResource: implementation classes to access an input stream resources.
ByteArrayResource: implementation classes to access an array of bytes resources. The Resource implementation classes for different underlying resources to provide the appropriate resource access logic, and provide convenient packaging, in order to facilitate access to resources the client program.
14, explains the various modes of automatic assembly?

Automatic assembly provides for five different modes dependent Spring container for the automatic assembly beans injection between:

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

byName: automatic assembly parameter names, Spring beans container attribute lookup, which is arranged to byName beans in the XML configuration file. After trying to match the container, and the assembly of the bean property with the same name bean.

byType: automatic assembly by automatic data type parameter, Spring beans container attribute lookup, which is arranged to byType beans in the XML configuration file. After attempts to match the container assembly and the type and properties of the bean same bean. If more than one bean qualify, then throw an error.

constructor: The same byType is similar, but applies to constructor arguments. If it is not exactly one bean of the same type constructor parameters in the BeanFactory, it throws a serious mistake.

autodetect: If there is a default constructor, construct by way of automatic assembly, otherwise the automatic assembly byType manner.

15. What is dependency injection in Spring?

Dependency injection as a control level inversion (IOC), there can be a variety of ways explained. In this concept, you do not need to create an object only describes how to create them. You do not have to be directly connected together through code components and services, but by the configuration file description which components need any service. After the IOC container is responsible for convergence.

16. What are the different types of IOC (dependency injection)?

Constructor dependency injection: constructor dependency injection is completed when the container trigger constructor, this configuration has a number of parameters, each representing an object injection.

Setter injection method relies on: a first container triggers no argument constructor or no-argument static factory method to instantiate an object, then calling setter methods bean container is completed Setter method relies injection.

17, dependency injection you recommend what? Constructor dependency injection method or Setter Dependency Injection?

You can use the two methods simultaneously dependency injection, the best option is implemented using constructor dependency injection force using setter methods implemented optional dependencies.

18, Spring IOC how

Spring org.springframework.beans package and the package formed the basis org.springframework.context Spring framework IoC container.

BeanFactory interface provides an advanced configuration mechanism so that any type of configuration object becomes possible. ApplicationContex interface BeanFactory (a sub-interface) has been extended, additional functionality was added on the basis of BeanFactory, such as easier integration with Spring AOP, also provides a mechanism for handling the message resource (for internationalization), event propagation and specially configured application layer, such as for Web applications WebApplicationContext.

org.springframework.beans.factory.BeanFactory Spring IoC container is embodied, for a variety of packaging and front management bean mentioned. BeanFactory interface is a Spring IoC container core interface.

19, Spring IoC container is a what?

Spring IOC is responsible for creating an object, the object management (via dependency injection), integration objects, configuration objects, and manage the life cycle of these objects.

20, IoC What are the advantages?

IOC dependency injection or reduces the amount of code of the application. It makes the application of the test is very simple because it requires a single embodiment or JNDI lookup mechanism is no longer in the cell test. Simple implementation and less interference mechanism such that loose coupling is achieved. IOC container support ground singletons and delay loading service.

21, explained AOP module

AOP module having a cut portion used to develop applications Spring properties. Most of the services provided by the module AOP Aliance, which ensures interoperability between the Spring Framework and other AOP frameworks. Further, the metadata module programmed into the Spring.

22, Spring Aspect Oriented Programming (AOP)

Aspect Oriented Programming (AOP): allows the programmer modular horizontal business logic, or functional definition of the core portion, for example, log management and transaction management.

Section (Aspect): AOP core is cut, it acts more general classes packaged as reusable modules. The API provides a set of modules comprising a cross-cutting function. For example, the logging module logs referred AOP aspects. Depending on the requirements, an application can have several facets. In Spring AOP, cut achieved by the class annotated with @Aspect.

Notification (Advice): notification indicates that the action before and after the execution method to be executed. In fact, it is some code Spring AOP framework triggered during program execution. Spring section can perform about five types of notification:

before (pre-notification): Advice to be executed before a method.
after (final notice): When a notification when a join point exits executed (either normal or exceptional return).
after-returning (after advice): notification performed after normal completion of a connection point.
after-throwing (abnormality notice): Advice to be executed when an exception is thrown exit method.
around (around advice): notification before and after the method call triggered.
Entry point (Pointcut): or a group of entry points is a connection point, notification will be performed at these locations. Pointcut or expressions may indicate by way of matching.

Introduction: introduction allows us to add a new method or property on the existing class.

Audience: object is notified by one or more facets. It is usually a proxy object. Also referred to as notification (advised) objects.

Agent: Agent is created after the notification object to the target object. From the client's perspective, agents and target objects are the same. There are several agents:

BeanNameAutoProxyCreator: bean name autoproxy creator
The DefaultAdvisorAutoProxyCreator: default notification by autoproxy creator
Metadata autoproxying: automatic metadata Agent
weaving: connecting section and other types of applications or objects together to create a process notification object. Weaving can be done at compile, load or run time.

23, Spring AOP implementation principle

Implement AOP technology, divided into two categories:

One dynamic agent technology, the use of the embodiment intercepted message, the message decorated, instead of performing the original object behavior;
Second static weaving manner, introducing a specific syntax to create "aspect", so that the compiler It can be woven into the code related to "aspect" during compilation.
The principle is very simple Spring AOP: AOP AOP framework is responsible for dynamically generated proxy class, by the method of the proxy class and callback methods Advice target object is composed, and the object can be used as the target object. AOP proxy contains all the methods of the target object, but there are differences in methods of the target object in the AOP proxy, AOP method adds enhanced processing specific entry point, and a callback method of the target object.

Spring AOP using dynamic proxy technology enhancements at runtime woven into the code. Uses two proxy mechanism: based on JDK dynamic proxies (JDK itself provides only proxy interfaces) and based on the dynamic proxy CGlib.

(1) JDK dynamic proxy
JDK dynamic proxy relates java.lang.reflect package two classes: Proxy and InvocationHandler. Wherein InvocationHandler just an interface, the code is called by the target class reflection by implementing the interface definition logic transversely, and the logic and business logic transverse dynamics woven together. The Proxy use InvocationHandler dynamically create an instance of a line with an interface to generate proxy object target class.
Its proxy object must implement an interface, which is the target to complete the proxy object by creating a class that implements the interface during operation can only generate proxy class that implements the interface, not for the class
(2) CGLib
using CGLib bytecode underlying technology, created as a subclass of the class, and the method call interception techniques to intercept all method of the parent class in the subclass, and homeopathic weaving transverse logic generated during the operation of the proxy object which is the target class the extended sub-class, so can not be notified final, private methods, because they can not be overridden. against class implements the agent, mainly to generate a subclass of the specified class, covering which method.
use JDK dynamic default in the spring case Acting achieve AOP, if the proxy-target-class is set to true or use of optimization strategies that will be used to create a dynamic proxy CGLIB .Spring AOP basically the same as in the realization of these two methods. In JDK agent, for example, will be used to create a proxy JdkDynamicAopProxy in invoke () method first requires woven into the booster current class to the block of the chain package, and then call the interceptor weaving is recursive. The final return proxies.

Published 33 original articles · won praise 0 · Views 851

Guess you like

Origin blog.csdn.net/ninth_spring/article/details/104486176