Basic Introduction
- Objective: To solve the complexity of enterprise application development
- Features: Replace EJBS with Master JavaBean, which provides many other enterprise applications
- Range: With your Java app
The Spring framework is a hierarchical architecture. Consists of 7 well-defined modules.
The Spring module is built on top of the core container, and the core container defines how the beans are created, configured, and managed.
Each of the modules (or components) that make up the Spring framework can exist individually or 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 Beanfactory, which is the implementation of the factory pattern. The beanfactory uses the inversion of control (IOC) pattern to separate the application's configuration and dependency specifications from the actual application code.
? Spring Context : The spring context is a configuration file that provides contextual information to the spring framework. The Spring context contains enterprise services such as JNDI, EJB, e-mail, internationalization, and checksum scheduling capabilities.
? Spring AOP: With the configuration management feature, the Spring AOP module directly integrates aspect-oriented programming capabilities into the spring framework. So. It is very easy to enable the Spring framework to manage whatever object supports 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 DAO: The JDBC DAO abstraction layer provides a meaningful exception hierarchy. This structure can be used to manage exception handling and error messages thrown by different database vendors. The exception hierarchy simplifies error handling. It also drastically 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 an ORM object-relational tool that includes JDO, Hibernate, and IBatis SQL maps. All of these follow Spring's common transaction and DAO exception hierarchies.
? Spring Web Module : The Web context module is built on top of the application context module. Provides 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. This includes JSP, Velocity, Tiles, IText, and POI.
The functionality of the Spring framework can be used regardless of the Java EE server. Most features also apply to an environment that is not managed. The core point of Spring is to support reusable business and data access objects that are not tied to a particular Java EE service. There is no doubt that such objects can be reused across different Java EE environments (Web or EJB), standalone applications, and test environments.
Spring as a full-fledged application framework (framework)
Descriptive narrative of terms
① Lightweight (lightweight). Spring Core containment is less than 1MB in size. The ability to use spring in very many small devices.
② Non-invasive (No intrusive). enhance the reusability of application components and reduce dependency on the framework.
③ container (Container). proactively generate objects and properties based on the configuration file. You do not have to write whatever code to produce the object.
④ Inversion of Control (IOC) and Dependency Injection (DI). The IOC purpose is to rely on abstraction; the relationship between objects is injected into the specified object by the container depending on the configuration file.
⑤ AOP (aspect-oriented programming). mechanisms based on proxies and interceptors. In conjunction with the spring IOC, a variety of declarative system-level services are used in the spring framework's applications using the execute-time weaving approach.
⑥ Persistence layer (persistent).
Spring provides DAO, programmatic transactions, and declarative transactions, simplifying the integration and use of ORM Tools (Hibernate, IBATIS).
⑦ Java EE service. be able to use the various services provided by the Java EE Standard specification and seamlessly combine them.
The benefits of the spring framework
① makes Java EE easy to use and promotes good programming habits.
Spring does not develop what is already there again. Therefore, in spring you will find a packet without logging, no connection pool, and no distributed transaction scheduling.
These are available in open source projects (such as Commons Logging for all log output, or commons dbcp used as a data connection pool) or provided by your application server. For the same reason, we did not provide the O/R mapping layer for this. There are good ways to solve this, such as Hibernate and JPA.
Iimake existing technologies easier to use.
For example, although we do not have the underlying transaction coordination processing. But we provide an abstraction layer covering the JTA or whatever the other transaction strategy.
③not competing directly with other open source projects.
For example, many developers, we have never been happy with struts, and feel that there is room for improvement in the MVC framework. In some areas, such as the lightweight IOC container and the AOP framework, Spring has direct competition. But in these areas there has not been a more popular solution. (Spring is a pioneer in these areas.) )
④Spring is portable between application servers. Ensuring portability is always a challenge, but spring avoids any particular platform or non-standardization, and supports users on Weblogic,tomcat,resin,jboss,websphere and other application servers.
⑤easy to decouple and simplify development.
Through the IOC container provided by spring. We are able to control the dependencies between objects by spring, avoiding excessive program coupling caused by hard coding. With spring, users no longer have to write code for these very low-level requirements, such as single-instance pattern classes, attribute file parsing, and can focus more on upper-tier applications.
⑥support for AOP programming.
With the AOP functionality provided by spring. It facilitates aspect-oriented programming, and many features that are not easy to implement with traditional oop can be easily coped with by AOP.
⑦support for declarative transactions
In spring, we can be freed from tedious transaction management code and have the flexibility to manage transactions in a declarative manner. Improve development efficiency and quality.
⑧test of the convenience program
The ability to do almost all of the test work in a non-container dependent programming way, in spring. Testing is no longer an expensive operation, but something that can be done at your fingertips.
⑨Easy integration of a variety of excellent frameworks
Spring does not exclude a variety of excellent open source frameworks, instead, spring reduces the difficulty of using various frameworks, and spring provides direct support for a variety of excellent frameworks, such as struts,hibernate.
⑩reduce the difficulty of using Java EE APIs
Spring provides a thin layer of encapsulation for very many hard-to-use Java EE APIs such as Jdbc,javamail, remote invocation, and so on. Through spring's easy package. These Java EE APIs are less difficult to use.
download, set up spring
http://springframework.org/
Preparing the spring design environment
Write the first spring program
HelloWorld Interface:
Java code
- /**
- *
- * @Copyright (C), 2009-2010 SISE Java Team.
- * @Author: Easinchu
- * @Email: [Email protected]
- * @Description:
- */
- Public Interface HelloWorld {
- Public void SayHello ();
- }
Helloworldbean Implementation class:
Java code
- /**
- *
- * @Copyright (C), 2009-2010 SISE Java Team.
- * @Author: Easinchu
- * @Email: [Email protected]
- * @Description:
- */
- Public class Helloworldbean implements helloworld{
- Private String HelloWorld;
- Public void Sethelloworld (String helloWorld) {
- This . HelloWorld = HelloWorld;
- }
- Public void SayHello () {
- System.out.println (HelloWorld);
- }
- }
Spring XML configuration file Ioc-config.xml:
XML code
- < Bean id="helloworldbean" class=" Cn.com.sise.firstapp.HelloWorldBean ">
- < Property name="HelloWorld">
- < value > hello,welcome to Spring world! </ value >
- </ Property >
- </ Bean >
Test class:
Java code
- Import org.springframework.beans.factory.BeanFactory;
- Import org.springframework.beans.factory.xml.XmlBeanFactory;
- Import Org.springframework.core.io.ClassPathResource;
- Import Org.springframework.core.io.Resource;
- /**
- *
- * @Copyright (C), 2009-2010 SISE Java Team.
- * @Author: Easinchu
- * @Email: [Email protected]
- * @Description: Use spring's beanfactory to construct the IOC container.
- */
- Public class Firstspringdemo {
- Public static void main (String []args) {
- //-----------beanfactory IOC container---------------------//
- //Load the XML configuration information from the CLASSPATH path
- Resource Resource = new classpathresource ("Ioc-config.xml");
- //Instantiate IOC container, at which time the container does not instantiate each managed bean defined by Beans-config.xml.
- Beanfactory factory = new xmlbeanfactory (Resource);
- /
- //Get Managed bean
- HelloWorld Hello = (HelloWorld) factory.getbean ("Helloworldbean");
- Hello.sayhello ();
- }
- }
Summary of Steps
① using Xmlbeanfactory to read XML configuration files and establish Beanfactory instances
②beanfactory a complete dependency injection based on the introduction
③ The bean example implemented by the method bean name specified by Getbean ()
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Spring frame built-in notebooks