"Java" Summary of the spring Framework (i)

Source: Internet
Author: User
Tags to domain

This article summarizes some of the spring framework's understanding, attention points and underlying operations. If there is a wrong place, welcome criticism and advice. Let's all work together!

Introduction to Spring Framework

Spring is an open source framework that is created to address the complexities of enterprise application development. One of the main advantages of the framework is its layered architecture, which allows you to choose which components to use while providing an integrated framework for Java EE application development. Spring is dedicated to the various solutions for Java EE applications, rather than just focusing on one layer of solutions, so spring is the "one-stop" choice for enterprise application development, spring runs through the presentation, the business, and the persistence layer. However, Spring does not want to replace those existing frameworks, but rather to integrate them seamlessly with a high degree of exploitation.

Spring Frame Structure

The spring framework consists of approximately 20 functional modules. The modules are divided into six parts, namely Core Container, Data access/integration, Web, AOP (Aspect oriented programming), instrumentation, and Test.

  • Spring Core: Is the most fundamental part of the framework. The main component of the core container is BeanFactory that it is the implementation of the factory pattern. BeanFactoryUse the inversion of Control (IOC) pattern to separate the application's configuration and dependency specifications from the actual application code.
  • SpringContexts: The spring context is a configuration file that provides contextual information to the spring framework. The Spring context includes enterprise services such as JNDI, EJB, e-mail, internationalization, checksum scheduling.
  • Spring AOP: With the configuration management feature, the Spring AOP module directly integrates aspect-oriented programming capabilities into the spring framework. Therefore, it is easy to enable any object managed by the Spring framework to support AOP. The spring AOP module provides transaction management services for objects in spring-based applications. By using Spring AOP, you can integrate declarative transaction management into your application without relying on EJB components.
  • Spring JDBC: provides a JDBC abstraction layer that simplifies the JDBC encoding while making the code more robust.
  • Spring DAO: The JDBC DAO abstraction layer provides a meaningful exception hierarchy that can be used to manage exception handling and error messages thrown by different database vendors. The exception hierarchy simplifies error handling and greatly reduces the number of exception codes that need to be written (such as opening and closing connections). Spring DAO's JDBC-oriented exception conforms to the common DAO exception hierarchy.
  • Spring ORM: The Spring Framework inserts several ORM frameworks, providing ORM object-relational tools, including JDO, Hibernate, and IBatis SQL Map. All of these conform to Spring's common transaction and DAO exception hierarchies.
  • Spring Web Module : The Web context module is built on top of the application context module and provides the context for Web-based applications. Therefore, the Spring framework supports integration with Jakarta Struts. The Web module also simplifies the process of working with multipart requests and binding request parameters to domain objects.
  • Spring MVC Framework : The MVC framework is a full-featured MVC implementation of building WEB applications. With the policy interface, the MVC framework becomes highly configurable, and MVC accommodates a large number of view technologies, including JSP, Velocity, Tiles, IText, and POI.
Spring IoC

Control inversion (inversion of CONTROL,IOC), also known as Dependency injection (Dependency Injection,di), is a design concept in object-oriented programming to reduce the coupling between program code. The basic concept of control reversal mode (also known as dependency injection) is that you do not create objects, but describe how they are created. The code does not directly connect to objects and services, but describes in the configuration file which component requires which service. Containers (which are IOC containers in the Spring framework) are responsible for linking these together.

IoC Implementation Ideas

Idea 1 Assigning dependencies through JavaBean properties, such as setter methods
Idea 2 Dependencies are provided as constructors and are not exposed as JavaBean properties

IoC implementation Steps

1. Download the spring and add it to the project.

Spring runs on the commons-logging component and requires that the associated jar file be imported with the Spring jar file.

2. Create persistent classes, data access layer interfaces, and implementation classes and business logic layer interfaces and implementation classes.

The persistence class here must provide a public setter method for Spring invocation.

3. Write the Spring configuration file.

Create the Applicationcontext.xml file under the Classpath root path of the project.

In the Spring configuration file, use the <bean> element to define an instance of the bean (also known as a component). This element has two common properties:

ID: Represents the name of the defined Bean instance.

Class: Represents the type of the defined Bean instance.

Use child elements <property> assign a value to a property in its scope. This element has two common properties:

Name: Represents the names of the Bean instance properties.

Value: Assigns a value to the property.

4. Write the code to get an instance of the target class through spring.

1) using the Beanfactory interface to get the target class instance
New Xmlbeanfactory (new fileinputsteam ("mybean.xml"= (Mybean) factory.getbean ("Mybean");

Because org.springframework.beans.factory.BeanFactory it is a simple interface, it can be implemented for a variety of underlying storage methods. The most common BeanFactory definition is XmlBeanFactory that it loads beans based on the definitions in the XML file.

The beans defined in the XML file are negatively loaded, which means that the bean itself will not be initialized until the bean is needed. To BeanFactory retrieve a bean, simply call the getBean() method and pass in the name of the bean that will be retrieved.

Each bean's definition can be POJO (with the class name and JavaBean initialization property definition) or FactoryBean . FactoryBeaninterface adds an indirect level to an application built using the Spring framework.

2) using the ApplicationContext interface to get the target class instance

The ApplicationContext interface is responsible for reading the Spring configuration file, managing the loading of objects, generating, maintaining the dependency between bean objects and bean objects, and responsible for the bean's life cycle. Classpathxmlapplicationcontext is an implementation class for the ApplicationContext interface that reads the Spring configuration file from the Classpath path.

Spring AOP

Aspect-oriented programming (Aspect oriented PROGRAMMING,AOP) is a product of the development of software programming ideas to a certain stage, and is a useful supplement to object-oriented programming (objects oriented Programming,oop). The functionality of AOP is fully integrated into the context of Spring transaction management, logging, and various other features. Aspect-oriented programming is simply to add new functionality to code snippets without changing the original program, and to enhance the processing of code snippets. Its design idea originates from the agent design pattern.

Basic concepts

Facets (Aspect): A modular crosscutting logic (or crosscutting concern) that may cross over multiple objects.

Join Point : A specific point of execution in the execution of a program.

Enhanced Processing (Advice): The code logic that slices perform on a particular connection point.

pointcut (Pointcut): Describes the characteristics of a connection point, and you can use regular expressions. The enhanced processing is associated with a pointcut expression and runs on a connection point that matches the pointcut.

target Object : An object that is enhanced by one or more facets.

AOP Proxy : An object created by the AOP framework that implements features such as enhanced processing.

weaving (Weaving): Processes that connect enhanced processing to types or objects in an application.

AOP Implementation ideas (demonstrated here using simple logging enhancement operations)

1. Add Spring AOP-related jar files to the project.

2) Create enhanced classes, write pre-enhancement and post-enhancement implementation logging capabilities.

Extract the code that needs to be enhanced into the enhanced class. This is demonstrated using the logging method.

3) Write a Spring configuration file to enhance the processing of business methods.

In the <Bean> element, you need to add an AOP namespace to import the AOP-related labels.

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "Http://www.springframework.org/schema/beans"3 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"4     xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx"5 Xmlns:context= "Http://www.springframework.org/schema/context"6 xsi:schemalocation= "Http://www.springframework.org/schema/beans7 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd8 Http://www.springframework.org/schema/tx9 http://www.springframework.org/schema/tx/spring-tx.xsdTen Http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd A Http://www.springframework.org/schema/context - http://www.springframework.org/schema/context/spring-context-3.0.xsd "> - </Beans>

The following is an enhanced configuration code

AOP-related configurations are placed in the <aop:config> element, such as the label <aop:pointcut> for configuring Pointcuts.

In the configuration shown in <aop:config>, use <aop:aspect> to reference the bean that contains the enhanced method. Declare methods as pre-and post-enhancements, respectively, through <aop:before> and <aop:after-returning>.

The expression property of <aop:pointcut> can be configured with pointcut expressions.

The above configuration configures code snippets for AOP, where expression is part of the expressions that define the Pointcut, as follows:

Execution (* cn.zdpz.service). *.*(..))

The expression structure is represented as follows

identifiers meaning
execution () Body of expression
First" * "Symbol Indicates the type of the return value any
cn.zdpz.service AOP The package name of the service being cut, that is, the business class that needs to be crosscutting
Second "*" denotes class name, * that is, all classes
.* (..)

Official documents about Expression structure Introduction

Execution (< modifier mode >?< return type mode >< method name mode > (< parameter mode >) < exception mode;?)

4) Write code to get the business object with enhanced processing.

"Java" Summary of the spring Framework (i)

Related Article

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.