Before you learn spring, you need to recall the factory model. Here's a piece of code that you'll experience. The reason for the factory model is that he has an advantage, much like the use of spring. In real-world development, the new object is a great need for a developer to be cautious and to test people. New objects need to consider more factors, so do not easily go to new, it is best to call. In Factory mode, it encapsulates some functional classes, and developers just need to call them. There is a control inversion (IOC) in spring that looks like this, and you can feel it for yourself.
First, factory model review (code example)
Public classTest { Public Static voidMain (string[] args) {//the unused Factory mode is created here by itself (one line below)//TV tv= New TV (100,50, "green", "Panda"); //we are responsible for creating an instance of this object that generates a dependencyTV TV1=tvfactory.createtv ();//using the factory, you can decoupleTV TV2 =Tvfactory.createtv (); System.out.println (TV1==TV2);//falseTv1.play (); Tv2.play (); } } classTV {Private intheight; Private intwidth; PrivateString color; PrivateString Pinpai; TV (intHeightintwidth,string color,string Pinpai) { This. height=height; This. width=width; This. color=color; This. pinpai=Pinpai; } voidPlay () {System.out.println ("TV Appearance:"); System.out.println (Pinpai+ "" +height + width+color); System.out.println ("--TV starts to play----"); System.out.println ("Play the American program ...."); } } classTvfactory {//Production TV Public StaticTV Createtv () {TV TV=NewTV (100,50, "green", "Panda"); returnTV; } }
Two, Spring introduction
Spring is an open-source, control -Inversion (AOP)- oriented container framework that is primarily designed to simplify enterprise development.
Benefits of using Spring:
1. Reduce the coupling between components, to achieve the decoupling between the various layers of software;
2. You can use many of the services provided by the container, such as transaction management services, message services, and so on. When we use containers to manage transactions, developers no longer need to manually control transactions. There is no need to deal with complex transaction propagation;
3. The container provides a singleton mode support, the developer no longer need to write their own implementation code;
4. The container provides the AOP technology, it is easy to implement such functions as permission interception, run-time monitoring and so on;
5. The large number of ancillary classes provided by the container, the use of which can accelerate the development of applications, such as: JdbcTemplate, hibernatetemplate;
6.Spring provides integrated support for the mainstream application framework, such as: Integrating Hibernate, JPA, struts, etc., so that it is easier to develop applications;
Spring is responsible for managing all the components in the container, collectively referred to as beans, in the spring concept, everything is a bean, in fact Spring is the bean-oriented programming (Bop,bean oriented programming). The modules (components) that make up spring can be separated by the main modules as follows:
1) srping core (Core container)
Supporting Utlities
Bean container
Core container: Provides the basic functions of the spring framework, the core container is beanfactory, it is the implementation of the Factory mode. The beanfactory example uses the inversion of control (IOC) pattern to separate the application's configuration and dependency specifications from the actual application code.
2) Spring context (a configuration file for context)
Application Context
Ui Support
Validation
JNDI EJB Supprot and Remodeling Mail
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 capabilities.
3) Spring AOP
Source-level
Metadata
AOP Infrastructure
The Spring AOP module integrates aspect-oriented programming functionality directly into the spring framework through configuration management features. 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.
4) sping DAO
Transaction Infrastructure
JDBC Support
DAO Support
The DBC 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.
5) Spring ORM
Hibernate Support
IBatis Support
JDO Support
The Spring framework inserts several ORM frameworks, providing ORM object-relational tools, including JDO, Hibernate, and Ibatis Sqlmap. All of these conform to spring's common transaction and DAO exception hierarchies.
6) Spring WEB
Webapplicationcontext
Multipart Resolver
Web utilites
The Web context module is built on the application context module and provides a context appropriate for the Web application. In addition, this module provides some service-oriented support. For example: Multipart request for file upload, which also provides the integration of spring and other web frameworks, such as struts, webwork.
7) Spring MVC
Web MVC
Framework
Jsp/velocity
Pdf/export
Spring provides a full-featured MVC framework for building Web applications. Although spring can be easily integrated with other MVC frameworks, such as struts, the Spring MVC Framework uses IOC to provide a complete separation of control logic and business objects. It also allows you to declaratively bind request parameters to your business objects, and spring's MVC framework can take advantage of any other services in spring, such as internationalized information and validation.
Spring brings the complexity of the development of the Java EE. Its core is the lightweight IOC container, which aims to provide a full-fledged integration framework for the Java EE application, with a combination of multiple sub-frameworks within the spring framework that can be independent of each other or replaced with other framework schemes. Spring wants to provide a one-stop (one-stopshop) solution for enterprise applications
Iii. Important Concepts in Spring
1) IOC (control reversal)
The application itself is not responsible for the creation and maintenance of dependent objects, but rather to other programs to create, so that control is transferred from the application to the external container, control transfer, called control reversal.
// Example: Public class useracrion{ private Iuserdao dao =new Userdaoimpl (); // Create a DAO object // Business Approach Public String Execute () { dao.adduser (user); } }
//reverse form of Public classuseracrion{useracrion (Iuserdao dao) { This. dao=DAO; } PrivateIuserdao DAO; //Business Approach PublicString Execute () {dao.adduser (user); } } //when other programs are calledIuserdao dao=NewUserdaomysqlimpl ();//other programs that control the creation of this object NewUseracrion (DAO);
2) DI (Dependency injection)
When we give the dependent object to the external container for creation, the above program can be changed to
Public class useracrion{ private Iuserdao DAO; // Dependent Objects ... To generate the set method public String execute () { dao.adduser (user); } }
During the program run time, dynamically by the external container (srping), the dependent object is injected, called Dependency injection.
3) AOP (Aspect oriented programming) programming for facets
Example: Struts2 in the Interceptor
A technology that can dynamically and uniformly add functionality to a program without modifying source code is possible through precompilation and run-time dynamic proxies.
The Java Framework Spring (i)