Understanding the Spring Framework

Source: Internet
Author: User
Tags aop

For the spring framework, as Java developers are certainly not unfamiliar, famous, reputation, but the spring framework has not been systematically studied, starting today to learn and record the spring framework of the relatively good features.

First, spring simplifies Java development

To reduce the complexity of Java development, the spring framework has adopted four key strategies

(1) Pojo-based lightweight and minimal intrusive programming

Note: Spring strives to avoid confusing your app code with its own API. Spring does not force you to implement the Spring specification interface or the class that inherits the spring specification, but in the spring-based application, its classes usually have no traces that indicate that you are using spring. The worst scenario is that a class might use spring annotations, but it's still pojo.

(2) loose coupling via dependency injection and interface oriented

Note: Dependency injection (Dependency Injection,di) through DI, the object's dependencies are set by the third-party component in the system that coordinates the objects when the object is created. Objects do not have to create or manage their own dependencies, and dependencies are automatically injected into the objects that need them. Two kinds of configuration, one is XML-based configuration, one is Java-based configuration.

(3) Declarative programming based on facets and conventions

Note: facet-oriented (aspect-oriented programming AOP) AOP enables the modularity of services such as log modules, security modules, and things modules, and uses them declaratively to use them in components that need their impact. The result is that these components are more cohesive and more focused on their own business, without the need to understand the complexities involved with system services. In short, AOP ensures the simplicity of Pojo.

(4) Reduce boilerplate code with facets and templates

Note: Spring encapsulates implementations of Java APIs that implement functionality, such as: JdbcTemplate in spring is the encapsulation of JDBC access to a database.

Second, spring container

The container is the core of spring. The spring container uses DI to manage the components that make up the app, and it creates associations between the components that work together. Spring container is not only one, spring comes with the implementation of multiple containers, can be summed up into two different types:

(1) The Bean factory (defined by the Org.springframework.beans.factory.BeanFactory interface) is the simplest container that provides basic DI support.

(2) The application context (defined by the Org.springframework.context.ApplicationContext interface) is built on Beanfactory and provides service at the application framework level.

Note: Spring provides several types of application contexts, such as:

1) Annotationconfigapplicationcontext: Load the Spring application context from one or more Java-based configuration classes

2) Annotationconfigwebapplicationcontext: Load the Spring Web application context from one or more Java-based configuration classes

3) Classpathxmlapplicationcontext: Loads the context definition from one or more XML configuration files under the Classpath and uses the class resource of the application context as a class resource

4) Filesystemxmlapplicationcontext: Load context definitions from one or more XML configuration files under System files

5) Xmlwebapplicationcontext: Load context definitions from one or more XML configuration files under Web application

Third, the Bean's life cycle

Specific text description:

(1) Spring to instantiate a bean

(2) Spring injects the value and the bean reference into the bean's corresponding attribute

(3) If the bean implements the Beannameaware interface, spring passes the Bean's ID to the Setbeanname () method

(4) If the Bean implements the Beanfactoryaware interface, spring calls the Setbeanfactory () method

(5) If the Bean implements the Applicationcontextaware interface, spring calls the Setapplicationcontext () method, passing in a reference to the application context where the Bean resides

(6) If the bean implements the Beanpostprocessor interface, spring will call their postprocessbeforeinitialization () method

(7) If the Bean implements the Initializingbean interface, spring will call their Afterpropertiesset () method. Similarly, if the bean declares a method of initialization using Init-method, the method is also called

(8) If the bean implements the Beanpostprocessor interface, spring will call their postprocessafterinitialization () method

(9) At this point, the beans are ready to be used by the application, and they will remain in the application context until the application context is destroyed

(10) If the bean implements the Disposablebean interface, spring will call its destroy () interface method. Similarly, if the bean declares the destroy method using Destroy-method, the method is also called

Note: Spring Framework Module classification

Understanding the Spring Framework

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.