I. Spring Principle Explained
Spring is a lightweight container framework (open source); The core of spring is the IoC (inversion of control) and AOP (aspect-oriented programming);
Spring consists of 7 modules:
- Spring Core (Core container): The core container provides the basic functions of the spring framework;
- SpringContext: The spring context is a configuration file that provides contextual information to the spring framework;
- Spring AOP: Through the configuration management feature, the Spring AOP module directly integrates the aspect-oriented programming functions into the Spring framework;
- Spring DAO: The Jdbcdao abstraction layer provides a meaningful exception hierarchy that can be used to manage exception handling and error messages thrown by different database vendors;
- Spring ORM: The Spring Framework inserts several ORM frameworks, providing ORM object-relational tools, including JDO, Hibernate, and Ibatissql Map;
- Spring Web: The Web context module is built on top of the application context module, providing context for Web-based applications;
- Spring MVC Framework : The MVC framework is a full-featured MVC implementation of building Web applications;
√ What is a framework?
- explanation One: develop a set of norms or rules (ideas) that everyone (programmers) in the specification or rule (thought) under work;
- Explanation Two: Some cattle predecessors or a summary of the cattle predecessors of a set of solutions;
Two. Spring IoC (Control inversion)
control inversion (inversion of the English abbreviation for IOC) is an important object-oriented programming principle to reduce the coupling problem of computer programs, is also the core of the lightweight spring framework.
Design principle: The coupling is removed from the code, placed in the same XML file, through a container when needed to implement this dependency, and to inject the required interface implementation into the class that needs it.
The most basic Java technology in the IoC is "reflection" programming, the Reflection programming mode is more than 10 times times slower than normal programming, and the object generated by the reflection mode after SUN's improvement is not much different from the ordinary way (there is still more than one gap);
Control reversal is generally divided into two types
- Dependency Injection (Dependency injection, referred to as DI): The component does not do a location query, only provides a common Java method for the container to determine the dependency relationship. The Assembly of the container's full-fledged component, which passes the object that conforms to the dependency to the desired object through the JavaBean property or constructor function. The practice of injecting dependencies through the JavaBean attribute is called SetPoint method injection (Setter injection); The practice of passing dependencies as constructor arguments is called constructor injection (Constructor injection);
- Dependency lookup (Dependency Lookup): The container provides callback interfaces and context conditions to the component. Both EJB and Apache Avalon Use this approach. In this way, the component must use the API provided by the container to find resources and collaboration objects, and only control reversals are reflected on those callback methods (that is, type 1 above): The container invokes these callback methods, allowing the application code to obtain the relevant resources.
three. Spring AOP (facet-oriented)
AOP is the abbreviation for Aspect oriented programming; AOP is a programming idea with OOP, and AOP is actually a continuation of OOP , which we understand with analogy:
AOP's association with OOP: 1. are all programming ideas;
The difference between AOP and OOP: 1. OOP is concerned with vertical business, while AOP is concerned with horizontal business;
2. From a business perspective, OOP is an abstract encapsulation of entity business properties and behaviors, and AOP is confronted with a step or stage in the process;
Description
The Chinese Ministry of Commerce, the International Ministry of Commerce and the Ministry of Advertising production (abstract) to the Ministry of Commerce, as well as the development department, testing Department, Design department (abstract) to the technical department is the use of OOP programming ideas;
The International Business Department, the domestic Ministry of Commerce and the development department have access to the customer's business, advertising production department and Design department, at this time with the abstract can not be reflected in the image (other departments in the same class does not correspond to the business), here you can use the idea of AOP programming;
Four. Spring Module description
The Spring framework is a layered architecture consisting 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, and each module (or component) that makes up the spring framework can exist separately or be implemented in conjunction with one or more modules.
Bean Concept: The objects that comprise your application that are managed by the IoC container; beans are the basic unit of spring management, and in spring-based Java EE applications, all components are treated as beans (including data sources, Hibernate's sessionfactory, things manager, etc.). Beans are objects that are initialized, assembled, and managed by the Spring container, except that the bean is no different from the other objects in the application.
1. Spring core (Core container):
Conceptual understanding: The core container provides the basic functionality of the spring Framework (Spring Core). The main component of the core container is Beanfactory, which is the implementation of the factory pattern. Beanfactory uses IoC mode to separate the configuration and dependency specifications of the application from the actual application code.
Practical application: Spring has two core interfaces (Beanfactory and ApplicationContext), where ApplicationContext is the sub-interface of Beanfactory. They can all represent the spring container, the spring container is the factory that generates the bean instance, and the bean in the container is managed.
- 1. Defining the interface
1 Public Interface HelloService {2 Public void SayHello (); 3 }
- 2. Defining an interface implementation class
1 Public class Implements helloservice{2 Public void SayHello () {3 System.out.println ("Hello world!" ); 4 }5 }
- 3. Registering bean information in the configuration file (Helloworld.xml)
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"4 Xmlns:context= "Http://www.springframework.org/schema/context"5 xsi:schemalocation="6 Http://www.springframework.org/schema/beans7 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd8 Http://www.springframework.org/schema/context9 http://www.springframework.org/schema/context/spring-context-3.0.xsd ">Ten One <!--ID represents the name of the component, class represents the Component class - A <BeanID= "HelloService"class= "Com.ljq.test.HelloServiceImpl" /> - - </Beans>
1 Public classHelloservicetest {2 @Test3 Public voidTesthelloworld () {4 //1. Read the configuration file instantiation of an IOC container5ApplicationContext context =NewClasspathxmlapplicationcontext ("Helloworld.xml");6 //2. Get the beans from the container, and note that this is fully "interface-oriented programming, not implementation-oriented"7HelloService HelloService = Context.getbean ("HelloService", HelloService.class);8 //3. Execute business logic9 Helloservice.sayhello ();Ten } One}
Description: Appeal 1-4 is the actual application of a spring Bean, in fact, the following 4 steps
- Defining the service Layer code
- Declaring in a configuration file
- Read the configuration file instantiate an IOC container (there are complex source code, another analysis)
- Get the bean from the container
- Execute business logic
The three implementations that ApplicationContext often use:
- 1.ClassPathXmlApplicationContext: Load context definition information from an XML file in the Classpath. The context definition file is treated as a classpath resource.
- 2.FileSystemXmlApplicationContext: Load context definition information from an XML file in the file system.
- 3.XmlWebApplicationContext: Load context definition information from an XML file in the Web system.
2. Spring context (Spring contexts):
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.
In spring usage, it is common practice to instantiate all beans when the server is started, which in some way increases the speed of the Web application.
There are usually two ways to configure the server to start loading applicationcontext:
- Contextloaderlistener specific configuration is added in Web. xml:
1 < Listener > 2 < Listener-class > </ Listener-class > 3 </ Listener >
- Contextloaderservlet specific configuration is added in Web. xml:
1 <servlet> 2 <Servlet-name>Context</Servlet-name> 3 <Servlet-class> 4 Org.springframework.web.context.ContextLoaderServlet5 </Servlet-class> 6 <Load-on-startup>1</Load-on-startup> 7 </servlet>
Regardless of which of the above configurations can reach the purpose of server startup is to instantiate the bean, if you want to specify the location of the applicationcontext.xml can be increased:
1 < context-param > 2 > contextconfiglocation param-name > 3 < param-value > /web-inf/appcontext.xml</ Param-value > 4 </ context-param >
With the above configuration, you can instantiate the specified applicationcontext.xml configuration file when the server starts.
3. Spring AOP
By configuring the management features, the Spring AOP module directly integrates the aspect-oriented programming functionality 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
4. Spring DAO
The Jdbcdao 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 ORMThe Spring framework inserts several ORM frameworks, providing ORM object-relational tools, including JDO, Hibernate, and Ibatissql Map. All of these conform to Spring's common transaction and DAO exception hierarchies.
6. Spring Web ModuleThe 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.
7. Spring MVC FrameworkThe 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. The model is composed of JavaBean, which is stored in map, the view is an interface, is responsible for displaying the model, and the controller represents the logic code. The functionality of the spring framework can be used in any Java EE server, and most features are also available in an out-of-management environment. 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.
Description: Detailed applications for 1-7 are explained in the following chapters in conjunction with examples
Java Programmer's Spring (a) Getting Started