The basic concepts of spring recorded by spring

Source: Internet
Author: User

Basic concepts of Spring

The Spring framework itself does not enforce any special programming patterns. From a design perspective, Spring has a lot of freedom, like a container, that can integrate many other framework components. Spring's core functionality is DI/IOC,AOP for any Java application. The idea of design in learning is helpful for Java programming.

1. Spring Advantage

The main advantages of spring are 6 points:

    • effectively organizes middle-tier objects . Can effectively integrate existing frameworks such as Struts2,hibernate,mybatis,shiro,lucene and so on.
    • implementation of real AOP interface-oriented programming, high-level decoupling between components (decouple), AOP is a good programming habit.
    • Spring's design philosophy is that applications created with spring may rely less on spring's own APIs. Most business objects in spring apps, not dependent on spring
    • Easy Unit Testing
    • Improve code reusability and avoid hard coding whenever possible. Spring can abstract some of the code in the application and use it in other programs.
    • Provides a consistent framework for data access, simplifying the way the database is accessed at the bottom
2. Spring Framework Module

2.1 Core container (cores Container)

Let's take a look at the official documentation.

The Core Container consists of the,,,, spring-core spring-beans spring-context spring-context-support and spring-expression (Spring Expression Language) modules.

5 core jar packages, or modules.

spring-coreand spring-beans modules provide the basis for the Spring Framework core container, including control inversion and dependency injection, which BeanFactory implements the factory model.

spring-contextThe module is built on the core and beans modules, it expands (extends/inherit) the Beans module, adds internationalization support, event propagation (events propagation), resource loading, contextual information (such as a servlet container), in addition, Java EE is also supported for EJB, JMX, remote access. ApplicationContextis the focus of the module.

spring-context-supportIntegration into spring for common third-party class libraries provides support such as caching (EhCache, guava, Jcache), mailing (JavaMail), scheduling (Commonj,quartz), templates (Freemarker, jasperreports,velocity).

spring-expressionThe module provides a powerful expression Language for run-time queries or manipulating objects that extend the EL expression

2.2 AOP and Instrumentation

spring-aopmodule provides an implementation of aspect-oriented programming that conforms to the AOP Federation standard. Here you can define interceptors and pointcuts,

spring-aspectsProvides integrated support for ASPECTJ

spring-instrumentThe module provides class instrumentation support and class loader implementations. spring-instrument-tomcatcontains the instrumentation agent for Tomcat

2.3 Message

The Spring4 contains a spring-messaging module that contains the core abstract classes of message, Messagechannel, MessageHandler, and a series of annotations that map messages to methods, similar to spring MVC

2.4 Data access/integration

Includes JDBC, ORM, OXM, JMS, and transaction modules

spring-jdbcProvides the JDBC abstraction layer

spring-txProvides programmatic and declarative transactions (programmatic and declarative transaction) management for classes that need to implement a specific interface and apply to all Pojo.

spring-ormThe module provides the mainstream object-relational mapping (object-relational mapping) API, including JPA, JDO, and Hibernate

spring-oxmThe module provides an abstraction layer that supports object/xml mapping implementations, such as JAXB, Castor, XMLBeans, JiBX and XStream

spring-jmsContains attributes for generating and consuming messages, Java messaging Service

2.5 Web

The Web layer contains
spring-web, spring-webmvc, spring-websocket, and spring-webmvc-portlet

spring-webProvides basic web integration, such as file uploads, using the servlet listener to initialize the IOC container, the web context, as well as the HTTP client and spring remote supported Web Parts

spring-webmvc, also called Web-Servlet module , contains Model-view-controller (MVC), a REST Web services implementation. Spring MVC, together with other spring modules, separates the back-end model code from the Web Form

spring-webmvc-portlet, also called Web-Portlet module , provides an MVC implementation in a portlet environment, just like the functionality of spring MVC

Spring middle-tier using a third-party web framework

Typical full-fledged Spring Web application

3. Core of Spring
    • Dependency Injection (DI)
    • Control Inversion (IOC)
    • Facet-oriented programming (AOP)

A Java instance (the caller) needs to invoke another Java instance (callee), and the caller needs to implement the calling procedure, usually the "new constructor method", but this is a high degree of coupling. when the calling process is responsible (code), or control, handing over to the container to implement, this is the control reversal; from the container's point of view, it is the container that injects to the caller the instance it wants to invoke, which is dependency injection. behind, is actually using the reflection mechanism, dynamic agent

In a word, you need an example, "You don't have to come to me, I'll come to you." Object dependencies are managed by the container, enabling the decoupling of objects

4. SPRINGIOC Container

The SPRINGIOC container is a lightweight container that provides IOC support. Spring provides two types of containers:beanfactory and applicationcontext. In addition, spring provides several implementation classes for these two classes, which are also called Spring containers.

4.1 beanfactory

The Org.springframework.beans.factory.BeanFactory interface definition, which belongs to an IOC container of the underlying type . This interface defines the functional specifications of the IOC container, the following source code is not the latest 4.2.6

  Public  interface beanfactory {        //Flag back to factory, not instanceString Factory_bean_prefix ="&";//Returns the XML file with the Bean with ID nameObject Getbean (String name)throwsBeansexception;//return XML file with ID name, bean of type requiredtype<T> T Getbean (String name, class<t> requiredtype)throwsBeansexception;//Returns a bean of type requiredtype in the XML file<T> T Getbean (class<t> requiredtype)throwsBeansexception;//Returns the XML file with the ID name, mutable parameter argsObject Getbean (String name, Object ... args)throwsBeansexception;whether there is a bean with ID name in the//xml file        BooleanContainsbean (String name);//scope, XML file, scope property, whether Singleton        BooleanIssingleton (String name)throwsNosuchbeandefinitionexception;//Whether the prototype mode, once per call, creates a new instance        BooleanIsprototype (String name)throwsNosuchbeandefinitionexception;//The class named name is matched to the target class TargetType        BooleanIstypematch (String name, class<?> targetType)throwsNosuchbeandefinitionexception; Class<?> GetType (String name)throwsNosuchbeandefinitionexception;//Return aliasString[] Getaliases (String name); }

Beanfactory is responsible for initializing the various beans and invoking their life cycle methods.

Spring provides a number of implementations of beanfactory, which is commonly used XmlBeanFactory , and, after 3.1, it is recommended to use DefaultListableBeanFactory and XmlBeanDefinitionReader , based on the definition of the XML configuration file, to assemble the bean

4.2 ApplicationContext

org.springframework.context.ApplicationContextDefines an interface that is built on Beanfactory and inherits ListableBeanFactory, HierarchicalBeanFactory the subclass of two beanfactory.

In contrast to Beanfactory, ApplicationContext creates, manages, and configures beans, and provides additional functionality, such as internationalization.

In addition, all singleton beans in the container are instantiated after the ApplicationContext is initialized. Therefore, most systems choose to use applicationcontext**, only in the case of low system resources, consider using beanfactory**

The common implementation classes of ApplicationContext are

    • Annotationconfigapplicationcontext
    • Classpathxmlapplicationcontext
    • Filesystemxmlapplicationcontext
    • Xmlwebapplicationcontext

The first is obviously the annotation parsing, and the next few are parsed according to the XML configuration file.

5.3 Ways to implement dependency injection 5.1. Set injection, setter method

Service implementation layer, need to instantiate the object, set setter method, the key is the set method, the XML file read content, converted to the corresponding instance, and then injected through the set method of the Call class
Applicationcontext.xml file, registering bean, setting

Advantages:

    • Intuitive, natural and flexible
    • If the dependency relationship is complex, the construction method injection is bloated and the setter is more concise.

Disadvantages:

    • Although flexible, it is easy to break the state and behavior of the class. It is therefore necessary to know which combinations are valid and which are invalid, and the order should be noted.
5.2. Construction method Injection

class to be injected in the constructor method
Applicationcontext.xml file, registering bean, setting

Advantages:

    • Conforms to design principles, "creates a complete, legitimate object during construction"
    • Avoid tedious setter methods, where all dependencies are focused on construction methods and are more readable
    • Association relationships are expressed only in construction methods, and only component creators need to be concerned about dependencies within components. The dependency is transparent to the caller. Block unnecessary information on the upper layer, the system level is clear.

Disadvantages
-Construction method errors are sometimes difficult to exclude
-Inheritance, the construction method does not inherit automatically, you need to manually
-If you are dealing with optional attributes, cyclic dependencies, the construction method injection is a little too

5.3. Interface Injection

Interface injection requires a class to implement a specific interface or to inherit a particular class, so that the dependencies are not much improved, are intrusive, and are largely deserted. AVALON,EJB containers fall into this category. Spring does not support this type of injection.

Common development, setter-based, construction method supplemented, you can achieve very good efficiency. Now there are annotations autowired, which is even easier.

The basic concepts of spring recorded by spring

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.