Spring of the interview question

Source: Internet
Author: User

1, Spring's understanding

Spring is a lightweight container, non-intrusive framework. The most important core concept is the IOC, and provides an approach to implementing the AOP concept, providing support for persistent layers, transactions, and some of the current popular frameworks (STRUTS,HIBERNATE,MVC), Sping also offers a solution that integrates with them. With spring, we can reduce the dependency between classes and the degree of coupling between programs, maximizing loose coupling, making programs more flexible and more scalable.

IOC, Chinese translation for "inversion control,". di->; " Dependency Injection "means that we do not have to maintain the dependency of the object in the program code, but rather to inject the properties and instances of the class into the class silently through an XML configuration file. The dynamic loading of the class makes it easy to switch between classes and classes (via interfaces).

The idea of this design approach is that the high-level modules should not rely on the lower modules, but the modules must rely on abstraction. The program should not be dependent on implementation, but rather on an abstract interface. The application does not look for containers, but containers give us all the objects we want.

Another important aspect of spring is support for AOP implementations: The Chinese translation of AOP is: aspect-oriented programming.

Aspect-oriented programming (AOP) is a complement to object-oriented programming (OOP), object-oriented programming breaks down programs into objects of all levels, and facets-oriented programming breaks down the program's running process into facets. AOP considers the structure of the program from the point of view of the program, extracts the facets of the business process, and OOP is a static abstraction, and AOP is a dynamic abstraction that abstracts the steps in the application execution process, thus obtaining the logical division between the steps.

The AOP framework has two characteristics: 1. Good isolation between steps 2. Source Code Independence

2. Spring annotations

In addition to providing @Component annotations in Spring 2.5, several annotations with special semantics are defined, namely, @Repository, @Service, and @Controller. In the current Spring version, these 3 comments and @Component are equivalent, but from the name of the annotation class it is easy to see that the 3 annotations correspond to the persistence layer, the business layer, and the control layer (the WEB layer), respectively. Although there is nothing new about these 3 annotations and @Component, Spring will add special features to them in future releases. Therefore, if a WEB application uses a classic three-tier hierarchy, it is best to annotate the classes in the hierarchy by using @Repository, @Service, and @Controller, respectively, in the persistence layer, the business layer, and the control layer @Component Comment on those classes that are more neutral.

In a slightly larger project, there are usually hundreds of components, and if these components are configured with an XML bean definition, it will obviously increase the volume of the configuration file, and it may not be convenient to find and maintain them. Spring2.5 introduced the component Auto-scan mechanism, where he could look under the classpath to annotate @Component, @Service, @Controller, @Repository annotated classes, and incorporate these classes into the spring container for management. Its role is the same as when you configure a component with a bean node in an XML file.

@Service is used to label business layer components, @Controller for labeling control layer components (such as action in struts), @Repository for labeling data Access Components, DAO components, @Component generic components, when components are poorly categorized , we can use this annotation for labeling.

3. What are the advantages of spring?

IOC decoupling improves the reusability of the code, manages the object's life cycle, does not cause the process of object duplication creation, saves the memory consumption of the JVM, manages the things, and provides support for many other frameworks .

4. Bean Life cycle

In this spring framework, once a bean is incorporated into the spring IOC container, the bean's life cycle is managed by the container, typically beanfactory or ApplicationContext as the manager's role. Knowing the life cycle activities of a bean is a great help in making better use of it.

The following is an example of a bean's life cycle activity in Beanfactory:
    • The creation of beans

The bean definition file is read by beanfactory and each instance is generated.

    • Setter Injection

Executes the Bean's property dependency injection.

    • Beannameaware's Setbeanname ()

If the Bean class implements the Org.springframework.beans.factory.BeanNameAware interface, its setbeanname () method is executed.

    • Beanfactoryaware's Setbeanfactory ()

If the Bean class implements the Org.springframework.beans.factory.BeanFactoryAware interface, its setbeanfactory () method is executed.

    • Beanpostprocessors's Processbeforeinitialization ()

If there is an instance of the Org.springframework.beans.factory.BeanPostProcessors interface implemented in the container, any bean will perform the processbeforeinitialization of the instance before it is initialized () method.

    • Initializingbean's Afterpropertiesset ()

If the Bean class implements Org.springframework.beans.factory. Initializingbean Interface, its afterpropertiesset() method is executed.

    • Defined in the Bean definition file Init-method

Use the "Init-method" property in the bean definition file to set the method name, as follows:

<bean id= "Demobean" class= "Com.yangsq.bean.DemoBean" init-method= "Initmethod" > ... </bean>

The Initmethod () method is executed, and note that this method is not with parameters.

    • Beanpostprocessors's Processafterinitialization ()

If there is an instance of the Org.springframework.beans.factory.BeanPostProcessors interface implemented in the container, any bean will perform the processafterinitialization of the instance before it is initialized () method.

    • Disposablebean's Destroy ()

If the Bean class implements the Org.springframework.beans.factory.DisposableBean interface when the container is closed, its destroy () method is executed.

    • Defined in the Bean definition file Destroy-method

The method defined by "Destory-method" can be used in the bean definition file when the container is closed

<bean id= "Demobean" class= "Com.yangsq.bean.DemoBean" destory-method= "Destroymethod" > ... </bean>

The Destroymethod () method is executed, and note that this method is not with parameters.

5. How to configure database driver in spring?

Use the "Org.springframework.jdbc.datasource.DriverManagerDataSource" data source to configure the database driver.

6, Spring inside Applicationcontext.xml file can be changed to other file name?

Contextloaderlistener is a servletcontextlistener that is initialized when your web app starts. By default, it will find spring's configuration in the Web-inf/applicationcontext.xml file. You can change the location of the spring configuration file by defining an element named "Contextconfiglocation". Examples are as follows:

Org.springframework.web.context.ContextLoaderListener Contextconfiglocation/web-inf/xyz.xml

7. Some important concepts of terminology are explained in AOP

Facets (Aspect): A focus of modularity, which may be crosscutting across multiple objects. Transaction management is a good example of a crosscutting concern in the Java EE application. In spring AOP, facets can be implemented using generic classes (pattern-based styles) or @Aspect annotations (@AspectJ styles) in ordinary classes.

Connection point (Joinpoint): a particular point in the execution of a program, such as when a method is called or when an exception is handled. In spring AOP, a connection point always represents the execution of a method. By declaring a parameter of type Org.aspectj.lang.JoinPoint, you get the connection point information for the body part of the notification (Advice).

Notification (Advice): An action performed on a particular connection point (joinpoint) of a slice. Notifications are available in various types, including notifications such as "Around", "before", and "after". The type of notification is discussed later in this section. Many AOP frameworks, including spring, are notification models with interceptors, and maintain a chain of interceptors centered on the connection point.

Pointcut (Pointcut): an assertion that matches a connection point (Joinpoint). The notification is associated with a pointcut expression and runs on the connection point that satisfies the pointcut (for example, when a method for a particular name is executed). How Pointcut expressions match connection points is the core of AOP: Spring defaults to using the ASPECTJ pointcut syntax.

Introduced (Introduction): (also known as an internal type declaration (Inter-type declaration)).  Declare additional methods or fields of a certain type. Spring allows the introduction of new interfaces (and a corresponding implementation) to any Proxied object. For example, you can use an introduction to make the bean implement the IsModified interface to simplify the caching mechanism.

Target object: An object that is notified (advise) by one or more facets (aspect).  It is also called the notified (advised) object. Since spring AOP is implemented by the runtime proxy, this object is always a proxy (proxied) object.

AOP Proxy: An object created by the AOP framework to implement a facet contract (aspect contract), including functions such as notification method execution. In spring, an AOP proxy can be either a JDK dynamic agent or a cglib proxy. Note: The latest introduction to Spring 2.0 is the pattern-based (schema-based) style and @aspectj annotation style facet declarations, which are transparent to the users who use these styles.

Weaving (Weaving): Connects a facet (aspect) to another application type or object and creates an object that is notified (advised).  These can be done at compile time (for example, with the AspectJ compiler), at class load time, and at run time. Spring, like other pure Java AOP frameworks, completes weaving at run time.

Type of notification:

Pre-notification (before advice): A notification that is executed before a connection point, but this notification does not prevent execution before the connection point (unless it throws an exception).

Notification after return (after returning advice): A notification that is executed after a connection point is completed normally: for example, a method does not throw any exceptions and returns normally.

Notification after an exception is thrown (after throwing advice): the notification that is executed when the method throws an exception exits.

Post notification (after (finally) advice): A notification that is executed when a connection point exits (whether it is a normal return or an unexpected exit).

Surround notification (Around Advice): A notification that encloses a connection point, such as a method call. This is the most powerful type of notification. Surround notifications can accomplish custom behavior before and after a method call. It also chooses whether to continue executing the connection point or to return directly to their own return value or throw an exception to end the execution.

Surround notification is one of the most common types of notifications. Most interception-based AOP frameworks, such as Nanning and JBOSS4, provide only surround notifications.

The concept of pointcut (pointcut) and connection point matching is the key to AOP, which makes AOP different from other old technologies that simply provide interception functionality. The pointcut enables the location notification (advice) to be independent of the OO hierarchy. For example, a around notification that provides declarative transaction management can be applied to a set of methods that span multiple objects, such as all business operations at the service layer.

IoC

IOC is the right to create objects to spring to manage the side of this involves when the creation of the XML bean is lazy loading. Reflection, single case, dynamic agent, factory

Spring of the interview question

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.