Spring Framework Foundation Learning

Source: Internet
Author: User
Tags naming convention maven central

Spring Framework Foundation Learning

Core support for dependency injection,transaction Management,web applications,data access,messaging,testing and more

Recommended Official article: http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/index.html

I.instroduction ( Introduction)

1. Introduction (Instroduction)

The Spring Framework provides a comprehensive program and configuration model based on modern Java enterprise-class applications, a deployment platform, and spring's critical connectivity is the infrastructure that supports the application layer: Spring focuses on enterprise-class applications, So a team can focus on application-level business logic without needing to be aware of a particular deployment environment

Spring enables-to-build applications from ' Plain old Java objects ' (pojos) and to apply Enterprise Services Non-invasi Vely to POJOs.

2. Features (Features)

    • Dependency Injection (Dependency injection,di)
    • Tangent-oriented programming (Aspect-oriented programming) includes spring-style declarative transaction management (spring's declarative transaction management)
    • Spring MVC Web Application (Spring MVC Web application) and RESTful Web Services Framework (Restfulweb Service Framework)
    • Basic support for JDBC,JPA,JMS (foundational supports)

The Spring Framework is a layered architecture consisting of more than 20 modules, and the core Container defines how beans are created, configured, and managed, and each module that makes up the spring framework is independent .

3.Core Container

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

Core Container by {spring-core, Spring-beans,spring-context,spring-context-support and Spring-expression (Spring Expression Language)} module.

The Spring-core and Spring-beans modules provide a fundamental part of the framework, including the IOC (control inversion) and Di (Dependency injection) features, while the beanfactory is complex to implement the factory pattern (factory pattern), It eliminates the configuration and specification of programmatic singleton and allowable dependencies from the actual program logic separation.

While context, the Spring-context module is built on a strong foundation, implemented through the core and beans modules (it is a means to): A framework style similar to the Jndi registry ( Framework-style) accessing objects

This context module (Spring-context) inherits features and functionality from the Beans module and adds internationalization (internationalization), such as the use of resource bindings (resouce bundles), event propagation (events propagation), resource loading (Resource loading) and transparent creation of the context (the transparent creation of contexts), for example, a Serlvet container, This context also provides Java EE functionality like EJB (Enterprise-Class JavaBean), JMS (Java Messaging Service), and basic remote

The ApplicationContext interface is the focus of the context module

Spring-context-support provides the Spring application context with an integrated, common third-party class library, such as caching (Encache,guava,jcache), mailing ( JavaMail), scheduling (Commonj,quartz) and template engines (freemarker,jasperreports,velocity),etc.

The Spring-expression module provides a powerful expression language (expressions Language) for runtime querying and manipulating object graphs, which are in the JSP 2.1 specification, the Unified Expression language (Unified expressions Language, Unified EL) extension (extension), this language provides settings and get property values, property acquisition, method invocation, access to array contents, collections and indexes, logical arithmetic operations, named variables, and retrieval of objects by name in the Spring ' IOC container, It also supports list projection and selection and common list aggregation

4.AOP and meter (instrumentation)

This SPRING-AOP module provides an AOP alliance-compliant (does not know how to translate) aspect-oriented programming (a tangent-oriented programming language) that runs you to define, For example, method interception and Pointcut isolation implement code that should separate the functionality, using the source-level meta function (sourcelevel metadata functionality), you can also use a similar. NET attribute to merge the behavior information into the code.

Separate spring-aspects module provides integration with ASPECTJ

The Spring-instrument module provides class instrumentation (instrumentation) Support and class loading implementations for use with some application servers

Spring-instrument-tomcat module contains spring's instrument agent for Tomcat

5.Messaging

Spring Framework 4 includes a spring-messaging module, which includes key abstractions from the spring Integration (integration) entry, such as Message,messagechannel, MessageHandler, and other applications as service messaging, this module also contains a set of annotations as mapping message methods, similar to spring MVC's annotation-based programming model.

6.Data access/integration

Data access/integration by JDBC,ORM,OXM,JMS, and transaction model

The SPRING-JDBC module provides the jdbc-abstraction layer, which eliminates the need for cumbersome JDBC coding to encode and parse database vendor-specific error codes.

The SPIRNG-TX module supports programmatic and declarative transaction management for classes that implement special interfaces and all Pojo (plain Java objects).

The Spring-orm module provides an integration layer for popular Object relational mapping APIs, including JPA,JDO and Hibernate, using the Spring-orm module, which you can combine with all the O/R mapping frameworks, with all the other features provided by spring, Like I said before. Declarative transaction management features

The SPRING-OXM module provides an abstraction layer to support object/xml mapping implementations such as JAXB,CASTOR.XMLBEANS,JIBX and XStream

The SPRING-JMS module (Java Messaging Service) contains generation and consumption messaging features that have been integrated with the Spring-messaging module since spring Framework 4.1

7.Web

The WEB layer consists of spring-web,spring-webmvc,spring-websocket and Spring-webmvc-portlet modules

The Spring-web module provides basic web-oriented integration features like multi-file uploads and the use of servlet listeners to initialize the IOC container and the Web-oriented application context, as well as an HTTP client and a Web-related section of spring's remote support.

The Spring-webmvc module (also called the Web-servlet module) contains spring Model-view-controller (MVC) and RESTful Web Services (service) implementations of Web applications, spring

The MVC framework provides a clear separation between domain model code and Web Forms, and integrates with all other features of the spring framework.

The Spring-webmvc-portlet module (also called the Web-portlet module) provides implementation of MVC to use Porlet (Portlets are Java-based Web Components, managed by portlet containers, and processed by containers for requests, producing dynamic content) Environment and reflects the functionality of the servlet-based SPRING-WEBMVC module.

8.Test

The Spring-test module provides unit tests (units testing) and integration tests (integration testing) with JUnit or testng components for spring, and supports spring Applicationcontexts's consistent loading and caching of these contexts also provide mock-up objects that can be used independently of the test code.

two. Usage scenarios (usage scenario)

First, the previously described spring-provided functionality makes it available for many scenarios (Scenarios), from embedded applications that run resource-constrained to enterprise-class applications that use spring's transaction management capabilities and web frameworks for maturity (full-fledged)

A typical, mature spring Web application

Spring's declarative management features make Web applications completely transactional, just as with the EJB container-managed transactions, all your custom logic can be implemented with simple Pojo, and managed through the spring IOC container, Additional services include support for sending email and web-independent authentication (Validation), allowing you to choose to perform validation rules, and Spring ORM provides integration with Jpa,hibernate and JDO, such as when you use Hibernate, You can continue to use the additional mapping files and standard hibernate sessionfactory configuration (config), the form controller seamlessly integrates the network layer with the domain model, Eliminates the need for other classes when actionforms or converting an HTTP parameter to a domain model value.

Spring Middle tier (Middle-tier) uses a third-party (third-party) Web framework

Sometimes you can't switch completely to a different frame, and the spring framework won't force you to use it, not a solution without any solution. If the existing front end is built with struts, TAPESTRY,JSF or other UI frameworks can also be integrated with spring-based middle tier, which allows you to use the spring transaction functionality, you only need to use ApplicationContext (application context) Connect your business logic and use Webapplicationcontext (Web application context) to integrate your Web tier.

Remote usage Scenarios

When you need to access code that already exists through Web Services (service), you can use spring's hessian-burlap,rmi-or jaxrpcproxyfactory class to enable remote access to existing applications without difficulty.

Three. Dependency management and naming conventions

dependency Management and dependency injection are different things that want to add these spring features to your application (application), you need to add all the required class libraries (jar files) and import them into your classpath at runtime or at compile time (classpath), These dependencies are not injected virtual components, but physical resources in the file system (typically)

The process of relying on management involves (involves) locating these resources, storing them and adding them to the classpath, either directly (for example, my application relies on spring at run time) or indirectly (for example, my application relies on COMMONS-DBCP, It depends on Commons-pool), and this indirect dependency is also called "transitivity (transitive)"

If you want to use spring, you need to copy the Spring class libraries that make up your needs, in order to make this easier, spring is packaged into a set of modules, separating as many dependencies as possible, so for example, if you don't want to write a Web application, you don't need spring-web module, referring to the Spring class library model in the guide, we used a simple naming convention spring-* or Spirng-*.jar, where * represents a short name for the module (such as SPRING-CORE,SPRING-WEBMVC, SPRING-JMS , etc.), these actual jar filenames are usually the module names (for example, Spring-core-4.3.10.release.jar) that are connected to the version number.

Each version of the Spring framework publishes artifacts to the following location:

The default repositories that Maven Central,maven gets, many of the common libraries that spring relies on are also available from Maven Central, and most of the spring community uses Maven for dependency management.

The jar name here is spring-*-<version> jar, and Maven GroupID is org.springframework.

So the first thing you need to decide is how to manage your dependencies: We usually recommend using an automated system like maven,gradle or ivy, but you can also do it manually by downloading all the jars yourself.

Below you can find a set of spring artifacts to get a more detailed description of the module, see section 2.2, "Framework Modules".

Spring Dependencies and depending on Spring

Although spring provides integration and support for a wide range of enterprises and other external tools, it intentionally keeps its mandatory dependencies at absolute minimum: you do not need to locate and download (or even automate) a large number of jar libraries, using spring for simple use cases, for basic dependency injection, There is only one mandatory external dependency, that is, for logging (see below for a more detailed description of logging options)

Maven Dependency Management

If you use Maven for dependency management, you don't even need to explicitly provide a record dependency. For example, to create an application context and use dependency injection to configure an application, your Maven dependency would look like this:

<dependencies>    <dependency>        <groupId>org.springframework</groupId>        < artifactid>spring-context</artifactid>        <version>4.3.10.RELEASE</version>        < Scope>runtime</scope>    </dependency></dependencies>

, note that this scope can be declared as runtime, if you do not need to compile spring APIs, typically this is the case of a basic dependency injection case

The above example uses MAVEN central repository, if you want to use spring Maven repository, you need to specify the warehouse address in the MAVEN configuration of the year:

<repositories>    <repository>        <id>io.spring.repo.maven.release</id>        <url >http://repo.spring.io/release/</url>        <snapshots><enabled>false </enabled></snapshots>    </repository></repositories>
Maven "Bill of Materials" Dependency

With Maven, there may be a mix of different versions of spring JARs, for example, you might find that a third-party library or another spring project will pass the old version's dependencies to the old version. If you forget that you have a clear statement of direct Reliance, there may be unexpected problems.

To overcome these problems, MAVEN provides a "Bill of Materials (BOM)" concept that you can import Spring-framework-bom in your dependencymanagement to ensure that spring is dependent ( Direct and indirect) are the same version.

<dependencyManagement>    <dependencies>        <dependency>            <groupId> org.springframework</groupid>            <artifactId>spring-framework-bom</artifactId>            < version>4.3.10.release</version>            <type>pom</type>            <scope>Import </scope>        </dependency>    </dependencies></dependencyManagement>

The advantage of using the BOM is that you do not need lengthy designations <version> attributes when specifying the spring Framework artifacts dependent

<dependencies>    <dependency>        <groupId>org.springframework</groupId>        < artifactid>spring-context</artifactid>    </dependency>    <dependency>        < groupid>org.springframework</groupid>        <artifactId>spring-web</artifactId>    </ Dependency><dependencies>

2.Logging

Loggind is a very important (important) dependency in spring because:

    1. The only one mandatory external dependency (mandatory external dependency)
    2. Everyone wants to be able to output some information from their use.
    3. Spring integration with many other tools, optionally logging dependent (dependency)

A very important goal in development application is to configure all application in a central location using a unified logging, including all external components

This is more difficult than before because there are so many log frames to choose from.

The mandatory log dependency in spring is the Jakarta Commons Logging API (JCL), which we compile for JCL, and we also make the JCL log object visible to classes that extend the spring framework.

For users, it is important that all versions of spring use the same log library: migration is easy because even applications that extend spring still retain backward compatibility. The way we do this is to make a module in spring explicitly dependent on the commons-logging (JCL specification implementation) so that they compile all the other modules that depend on it at compile time, for example, if you use Maven, and wondering where to get a dependency on commons-logging, it comes from spring, especially from a central module called Spring-core.

The advantage of using commons-logging is that you do not need any other action to make your application work properly, it has a runtime discovery algorithm that can find other logging frameworks in the classpath of a well-known place, and use it to think of the appropriate method, or you tell it, if there is no other available, You can get logs from the JDK (for short, java.util.logging or Jul).

You should find that in most cases your spring application can log in to the console, which is important.

Spring Framework Foundation Learning

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.