Spring Framework Learning (1) Introduction to Spring

Content from: Introduction to the Spring Framework

Spring is an open source framework created to address the complexities of enterprise application development. One of the main strengths of the framework is its layered architecture , which allows you to choose which components to use while providing an integrated framework for J2EE application development.

 

Spring Framework

The Spring Framework is a layered architecture consisting of 7 well-defined modules. Spring modules are built on top of the core container, which defines how beans are created, configured, and managed.

 

Each module (or component) that makes up the Spring Framework can exist alone, or be implemented in conjunction with one or more other modules. The functions of each module are as follows:

  • Core container : The core container provides the basic functionality of the Spring Framework . The main component of the core container is  BeanFactorythat it is an implementation of the factory pattern. BeanFactory Use the Inversion of Control  (IOC) pattern to separate the application's configuration and dependency specification from the actual application code.
  • Spring Context : A Spring context is a configuration file that provides context information to the Spring Framework . The Spring context includes enterprise services such as JNDI, EJB, email, internationalization, validation and scheduling functions.
  • Spring AOP : The Spring AOP module integrates aspect-oriented programming capabilities directly into the Spring framework through configuration management features. Therefore, any object managed by the Spring Framework can easily be made AOP-aware. The Spring AOP module provides transaction management services for objects in Spring-based applications . By using Spring AOP, declarative transaction management can be integrated into applications without relying on EJB components.
  • 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 amount of exception code that needs to be written (such as opening and closing connections). Spring DAO's JDBC-oriented exceptions follow the generic DAO exception hierarchy.
  • Spring ORM : The Spring framework plugs into several ORM frameworks to provide ORM's object-relational tools, including JDO, Hibernate, and iBatis SQL Map. All of this follows Spring's generic transaction and DAO exception hierarchy.
  • Spring Web Module : The Web Context Module builds on the Application Context Module and provides context for web-based applications. So, Spring framework supports integration with Jakarta Struts. Web modules also simplify handling multipart requests and binding request parameters to domain objects.
  • Spring MVC Framework : The MVC framework is a full-featured MVC implementation for building Web applications. Through the strategy interface, the MVC framework becomes highly configurable, and MVC accommodates a number of view technologies, including JSP, Velocity, Tiles, iText, and POI.

Features of the Spring Framework can be used in any J2EE server, and most features also apply to unmanaged environments. The core point of Spring is to support reusable business and data access objects that are not tied to specific J2EE services. There is no doubt that such objects can be reused between different J2EE environments (Web or EJB), stand-alone applications, test environments.

 

IOCs and AOPs

Aspect-Oriented Programming , or AOP, is a programming technique that allows programmers to modularize behaviors that cross-cut concerns or cross typical lines of responsibility, such as logging and transaction management. The core construct of AOP is an aspect , which encapsulates behavior that affects multiple classes into reusable modules.

AOP and IOC are complementary technologies that take a modular approach to solving complex problems in enterprise application development. In a typical object-oriented development style, logging statements might be implemented in all methods and Java classes to implement logging. In the AOP approach, logging services can be modularized in turn and applied declaratively to components that require logging. The advantage, of course, is that the Java classes don't need to know about the existence of the logging service, nor do they need to think about the associated code. Therefore, application code written in Spring AOP is loosely coupled.

The functionality of AOP is fully integrated into the context of Spring transaction management, logging, and various other features.

 

At the heart of Spring's design are  org.springframework.beans packages, which are designed to work with JavaBean components. This package is usually not used directly by the user, but by the server as an underlying intermediary for most other functions . The next highest level abstraction is the  BeanFactory interface , which is an implementation of the factory design pattern that allows objects to be created and retrieved by name. BeanFactory Relationships between objects can also be managed.

BeanFactory Two object models are supported.

  • A monomorphic  model provides a shared instance of an object with a specific name that can be retrieved at query time. Singleton is the default and most commonly used object model. Ideal for stateless service objects.
  • The prototype  model ensures that a separate object is created for each retrieval. The prototype model works best when each user needs their own object.

The concept of a bean factory is the foundation of Spring as an IOC container. IOCs move the responsibility for handling things from the application code to the framework . The Spring Framework uses JavaBean properties and configuration data to indicate which dependencies must be set.

 

BeanFactory interface

Because it  org.springframework.beans.factory.BeanFactory is a simple interface, it can be implemented for various underlying storage methods. The most commonly used  BeanFactory definition is  XmlBeanFactorythat it loads beans based on definitions in an XML file.

BeanFactory factory = new XMLBeanFactory(new FileInputSteam("mybean.xml"));

Beans defined in XML files are passively loaded, which means that the bean itself is not initialized until the bean is needed. To  BeanFactory retrieve a bean from, simply call  getBean() the method, passing in the name of the bean to retrieve.

MyBean mybean = (MyBean) factory.getBean("mybean");

Each bean definition can be POJO (defined with class name and JavaBean initialization properties) or  FactoryBean. FactoryBean Interfaces add a level of indirection to applications built with the Spring Framework.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325066904&siteId=291194637