Spring Practical reading Notes (1)

Source: Internet
Author: User

Spring What is the fundamental mission?

Simplify Java Development

in order to reduce what are the 4 key strategies that Spring has taken in the complexity of Java development ?

1. POJO-based lightweight and minimal intrusive programming

2. Loose coupling via dependency injection and interface oriented

3. Declarative programming based on facets and conventions

4. Reduce boilerplate code through slices and templates

Dependency Injection and AOP Programming

1, if you want to have more knowledge of dependency injection, recommend reading Dhanji R Prasanna "Dependency injection"

2.Dependency Injection keeps loosely coupled software components that work together, while AOP programming allows you to isolate functionality throughout the application to form reusable components

Boilerplate code

as JDBC,JMS,JNDI, and using REST services often involve a lot of duplicate code

Spring Container

There is no single spring container, Spring bean factory (Bean factories Org.springframwork.beans.factory.beanfactory interface definition ) Di support. Application Context (Application org.springframework.context.applicationcontext< Span style= "font-family: Microsoft JAS Black" > interface definition ) beanfactory builds on and provides application-oriented services, such as the ability to parse text information from a property file, and the ability to publish application events to listeners of interest event

Although we can either the Bean factory and the application context, but the Bean Factory is often too low-level for most applications, so the application context is more popular than the Bean factory, and we focus on the app's online text instead Bean Factory

Using the app context

Spring comes with several types of application contexts, listed below for 3 of the most likely users to encounter

1,classpathxmlapplicationcontext-- loading context definitions from an XML configuration file under the Classpath, and applying the context definition file as a class resource

2.filesystemxmlapplicationcontext-Read the XML configuration file under the file system and load the context definition

3,xmlwebapplicationcontext-Read The XML configuration file under the Web application and load the context definition

will be Bean loaded into bean factory

How to use a Filesystemxmlapplicationcontext

ApplicationContext context = new Filesystemxmlapplicationcontext ("C:/foo.xml")

How to use a Classpathxmlapplicationcontext

ApplicationContext context = new Classpathxmlapplicationcontext ("Foo.xml")

is in all the classpath (including JAR file) to find the foo.xml file

with an existing application context reference, you can invoke the Getbean () method of the application context to get the Bean from the Spring container

Bean the life cycle

of traditional Java applications, The Bean life cycle is simple. The bean is instantiated using the Java keyword new , and then the Bean is ready to be used. Once the Bean is no longer in use, Java is automatically garbage collected

Bean load to Spring a typical declaration cycle process in the context of an application

Beans have undergone several stages from creation to destruction in the spring container, each of which can be customized for how Spring manages beans

1.Spring to instantiate the Bean

2. Spring injects the value and The Bean reference into the bean corresponding attribute

3.if The bean implements the beannameaware interface,Spring passes the Bean's ID to the Setbeanname () interface method

4.if the Bean implements the Beanfactoryaware interface,Spring will call the Setbeanfactory () interface method, which will Beanfactory Container Instance incoming

5.if the Bean implements the Applicationcontextaware interface,Spring will call Setapplicationcontext () interface method to pass in a reference to the application context

6.if the Bean implements the beanpostprocessor interface,Spring will call their Postprocessbeforeinitialization () interface method

7.if the Bean implements the Initializingbean interface,Spring will call their afterpropertiesset () interface method. Similarly, if the Bean declares the initialization method using Initmethod, the method is also called

8.if the Bean implements the beanpostprocessor interface,Spring will call their postporessafterinitialization () 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

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

Spring Module

The Spring framework is composed of several different modules. When we download Spring and unzip it, you will see a different JAR file in the Dist directory , such as

composition these three JAR files of Spring can be divided into 6 different function modules according to the function they belong to.

Such as

Overall, these 6 modules provide everything you need to develop an enterprise-class application, but you don't have to base your application on the entire Spring framework, and you're free to choose the Spring module that fits your application needs When Spring does not meet the requirements, it is entirely possible to consider other options. In fact,Spring even integrates with other third-party frameworks and class libraries, and you can use them freely.

Core Spring Container

Container is The core part of the spring framework, which is responsible for the creation, configuration, and management of beans in spring applications. In this module, you will find that Spring 's Bean Factory provides dependency injection. On top of the Bean factory, you'll find implementations of several spring application contexts, each providing a different way of configuring spring

except the Bean factory and application context, which also provides many enterprise services, such as mail,JNDI access,EJB integration, and scheduling

as you can see, all the the Spring modules are built on top of the core container. These classes are actually used implicitly when you configure an app.

Spring the AOP Module

in the In the AOP module,Spring provides rich support for slicing-oriented programming. This module is the basis for the development facets of Spring application systems. As with dependency injection,AOP helps to decouple application objects. With AOP, you can decouple your application-focused concerns ( such as transactions and security ) from the objects they apply to

Data access and integration

use jdbc Writing code often results in a lot of boilerplate code, such as getting a database connection, creating statements, Processes the result set to the last closed data connection. spring jdbc DAO (data access objects) modules encapsulate these boilerplate code, make our database code simple and clear, and avoid problems caused by the failure to release database resources. The module constructs a semantic-rich anomaly layer on top of the error messages of several database services, and we no longer need to explain the obscure proprietary sql error message

Spring for those like ORM (object-relational mapping) The developer of the tool provides the Orm module. spring orm module was built on the Dao support, and for some orm Framework provides a build Dao simple way. spring did not try to create its own orm solution, but for many popular orm framework for integration, including Hibernate Java Persisternce api, jdo and Ibatis spring Orm Framework and JDBC

This module also contains the A Spring abstraction layer built on top of JMS that integrates with other applications asynchronously using messages. starting with Spring3.0 , this module also contains an object-to -XML mapping feature that was originally part of the Spring Web Service Project

In addition, this module uses Spring AOP Module provides transaction management services for objects in spring applications

Web and Remote calls

Mvc mode has been widely accepted as a build web Application method, It helps separate the user interface logic from the application logic. java never missing mvc framework, Apache struts JSF , Webwork tapestry are popular Mvc framework

most certainly Spring integrates a variety of popular MVC frameworks, but its web and remote invocation modules bring a powerful MVC framework that helps to apply loosely coupled web-layer technologies. The framework provides two forms: a Servlet-based framework for traditional WEB applications and a Portlet- based application for Java Porlet APIs

In addition to user-facing Web app, the module also provides choices for several remote calls that are built to interact with other applications. the Spring Remote Call service integrates RMI, Hessian,Burlap, Jax-ws, and Spring also comes with a remote bounce frame: HTTP Invoker

Test

Given the importance of the developer self-test, Spring provides a test module to test the Spring application

through this module, you will find Spring provides a series of mock object implementations for JNDI, Servlet, and Portlet writing unit tests. For integration testing, this module provides support for loading a collection of beans in the Spring application context and for interacting with beans in the spring context

Spring Protfolio

when it comes to Spring , it's far beyond our imagination. In fact,Spring is much more than what the Spring framework brings. If we just stay on the core of the Spring Framework, we will miss the wealth that the huge spring Portfoliot has to offer. The entire spring Portfolio includes several frameworks and class libraries built on the IQ of the core Spring framework. In a nutshell, the spring Portfolio provides a spring programming model for almost every area of Java development

Spring Web Flow

Spring Web Flow is built on the spring MVC Framework and provides support for process-based conversational Web Applications ( think shopping carts, or wizards ) . Spring Web Flow 's home page (http://www.springsource.org/webflow)

Spring Web Service

while the core The Spring Framework provides a declarative way to publish a spring Bean as a Web Service

but these services are based on a controversial framework built on top of a poorly contracted post-model. The contract for these services is determined by the Bean's interface. Spring Web Service provides a contract-first Web service model, and the implementation of the service is written to meet the contract of the service

Home:http://static.springsource.org/spring-ws/sites/2.0

Spring Security

security is a very critical facet for many applications. Spring AOP provides declarative security for spring applications

Home:http://static.springsource.org/spring-security/site

Spring Integration

Many enterprise-class applications need to interact with other applications. Spring Integration provides several common application integration patterns for the spring declarative style implementation

related Books of Spring integration <spring Integration in action>

Home:http://www.springsource.org/spring-integration

Spring Batch

when we need to do a lot of work on the data, no technology can be more capable of this scenario than batch processing. If you need to develop a batch application, you can use spring batch to implement the spring 's powerful POJO -oriented programming model

Spring Batch related books <spring batch in action>

Home:http://www.springsource.org/spring-batch

Spring Social

social networking is a rising star of the Internet, and more and more applications are being incorporated into social networking sites, such as Facebook or Twitter. If you're interested in this, you can learn about the spring social, a social network extension module for Spring

Home:http://www.springsource.org/spring-social

Spring Practical reading Notes (1)

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.