A conceptual explanation of spring

Source: Internet
Author: User

First, Spring overview
1.Spring is an open source framework created to address the complexities of enterprise application development, but is now more than just applications and enterprise applications
2.Spring is a lightweight control inversion (IOC) and aspect-oriented (AOP) container framework
-Both sides of spring are lightweight in terms of size and overhead
-decoupling is achieved by controlling the reversal technology
-Provides rich support for aspect-oriented, enabling the development of cohesive applications by separating the business logic of the application from the system-level services
-Contains and manages the configuration and declaration cycles of the Application object, in this sense a container
-The configuration of simple components, combined into complex applications, in this sense is the framework
3.Spring Development application Simple, aspect, fast Spring
3.1 Features of the framework
-Semi-finished products
-Encapsulation of specific processing processes and logical controls
-Mature, constantly improving software
The difference between a frame and a class library
-The framework is typically packaged with logical, high cohesion, and class libraries that are loosely set of tools
-The framework is focused on one area, and the class library is more general
3.2 Why use Frames
-Software systems are becoming more complex
-high degree of reuse, development efficiency and quality improvement
-software designers should focus on understanding the domain to make the needs analysis more fully
-Easy to get started and solve problems quickly

Ii. Spring IOC
Thematic one IOC
-Interface and interface-oriented into
-What's the IOC
Bean Configuration for-spring
Initialization of the-bean
Two packs of 1.org.springframework.beans Org.springframework.context
Beanfactory provides configuration structure and basic functionality to load and initialize beans
ApplicationContext saves bean objects and is widely used in spring

           ApplicationContext-local File Filesystemxmlapplicationcontext context = new Filesystemxmlapplicationconte           XT ("F:/worksapace/application.xml"); -classpath Classpathxmlapplicationcontext context = new Classpathxmlapplicationcontext ("Calsspath:           Spring-context.xml "); The-web application relies on a servlet or listener spring container to load IOC <listener> &LT;LISTENER-CLASS&GT              ;org.springframework.web.context.contextloaderlistener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-v Alue>classpath:applicationcontext.xml</param-value> </context-param> Spring MVC Plus IOC can not use spring container <servlet> <servlet-name>seckill-dispatcher</servlet -name> <servlet-class>org.springframework.web.servlet.dispatcheRservlet</servlet-class> <init-param> <param-name>contextconfi                    Glocation</param-name> <param-value>classpath:spring/spring-*.xml</param-value>                    </init-param> </servlet> <servlet-mapping> <servlet-name>seckill-dispatcher</servlet-name> <url-pattern>/</url-pattern&                Gt Common injection methods of </servlet-mapping>-spring

1.IOC control inversion: The transfer of control, the application itself is not responsible for the creation and maintenance of dependent objects, but is created and maintained by an external container (Spring container or Spring MVC child container)
to create and maintain
- What controls are reversed: the process of acquiring a dependent object is reversed after control is reversed, the process of acquiring a dependent object is managed by the IOC container to actively inject
Dependency injection: It is the IOC container that dynamically injects some kind of dependency into the object during runtime
2. Di Dependency injection is an implementation
Purpose: Create objects and assemble relationships between objects
feature: Do not care about object creation only to use
3.Spring injection
-spring injection is when the Spring container is started to load the bean configuration, Complete assignment of variables
-common two injection methods
1, set value injection is set through the XML configuration file Settings
<bean id= "Injesctionservice" class= " Org.seckill.dao.injection.service.InjectionServiceImpl,
<property name= "Injectiondao" ref= " Injectiondao "/>
</bean>
<bean id=" Injectiondao "class=" Org.seckill.dao.injection.dao.InjectionDaoImpl "/>
2, Construction injection
<bean id=" Injesctionservice "class=" Org.seckill.dao.injection.service.InjectionServiceImpl,
<constructor-arg name= "Injectiondao" ref= " Injectiondao "/>
</bean>
<bean id=" Injectiondao "class=" Org.seckill.dao.injection.dao.InjectionDaoImpl "/>

Three, Spring Bean
-bean configuration item
ID: Unique identifier of this Bean in the entire IOC container
Class: Which class to instantiate
Scope: Scope
-singleon: Singleton, Only one bean container photo There's only one copy.
-protoptype: Creates a new instance per request (per use), destroy mode does not take effect
-request: Each HTTP request creates an instance and is valid within the current request
- Session: Each HTTP request creates an instance and is valid within the current session
-global session: Portlet-based Web is valid (Portlet defines global session) if you are in the Web, Same session
Constructor arguments:
Properties: Property
Autowiring Mode: Automatic assembly mode
Lazy-initialization mode: Lazy load mode
Methods for Initialization/destruction method initialization and destruction
-bean scope
-singleon: Singleton, only one Bean container photo that only exists one copy
-protoptype: Each request (per use) creates a new instance, the Destroy method does not take effect
-request: Each HTTP request creates an instance and is valid within the current request
-session: Each HTTP request creates an instance and is valid within the current session
-global session: Valid in Portlet-based web (Portlet defines global session) if in the Web, same session
- Bean life cycle
1-definition

    2-Initialize-implement Org.springframework.beans.factory.InitalizingBean interface, override Afterpropertiesset method public class Red Isdao implements initalizingbean{@Override public void Afterpropertiesset () throws Exceptio n{}}-Configuration Init-method <bean id= "Redisdao" class = "org.seckill.dao.RedisDa O "init-method=" init "/> There is an init () method in this class that automatically executes when the class is created 3-use 4-destroy-implement Org.springframework.beans.factory.Disposa                Blebean interface, overriding destroy method public class Redisdao implements initalizingbean{@Override public void Destroy () throws exception{}-configuration Init-method <bean id= "Redi SDao "class =" Org.seckill.dao.RedisDao "destroy-method=" Destroy "/> This class has a destroy () method that automatically executes when the class is created summarizing 1, the default global        Initialization and destruction methods, 2, implementation of the interface initialization and destruction methods, 3, configuration file configuration initialization and destruction methods, the three methods are used at the same time, 1 default is not executed, and 232 will be executed, and is 2 implementation of the interface before the implementation of 3 in the configuration. 1 The default global initialization and destruction methods can have noYes, there is no significant impact on the configuration, of course, if a bean does not take 23 initialization of the destruction method, and there are 1 default methods, these two methods will be executed. Even if not, the system will not error.

-bean Automatic Assembly (autowiring)
-no: Do nothing
-byname: Automatically assemble according to the property name, this option checks the container and finds beans that are exactly the same as the property by name, and the auto-assemble of the side and property
<bean Id= "DAO" class= "Com.etoak.dao.DaoImpl" ></BEAN>
//There are encapsulated property values and their set methods in this class
//or can be configured through the namespace of the table header Default-autowrie= "Bybane" is not configured by default
<bean id= "action" class= "Com.etoak.action.LoginAction" init-method= "Init
<property name= "di" ref= "DAO" ></property>
</bean>
-bytype: If there is a bean in the container that is the same as the specified property type, the property is automatically assembled, and if there are more than one type Bean, an exception is thrown
and it is indicated that it cannot be automatically assembled using the Bytype method, and if no matching bean is found, it does not happen
- Constructor: Similar to the bytype approach, except that he applies the constructor parameter if the container does not find a bean with the constructor parameter type, throwing an exception

-resources&resourceloader
Unified interface of resources detachment in resource files
-urlresource:url corresponding resources, based on a URL address can be built
-classpathresource: Obtaining a resource file under the Classpath (also known as CLASSPATH)
-filesystemresource: Get the resources inside the file system
-servletcontextresource:servletcontext encapsulated resources for accessing resources in the ServletContext environment (web-related)
-inputstreamresource: Resources encapsulated for the input stream
-bytearrayresource: Resources for ground-up array encapsulation
Resourceloader all ApplicationContext that load the resource implement the interface to get the resource instance
public interface resourceloader{
Resource getresource (String location);
}
Figure


Start diagram

1.bean class implementation Applicationcontextaware All APPLICATIONCONTEXT implementations of this interface can acquire resource instances

Applicationconext.getresource (); There are 4 in

Test class

-aware This temporary neglect has not been used (the above to achieve resource time is to use Aware)
In-spring, some interfaces with aware end are provided, and the bean that implements the aware interface can obtain the corresponding resources after initialization.
-Through the Aware interface, you can use the resources of spring to operate (be cautious)
-Provides an entry point for a simple extension of spring
-applicationcontextaware the class that implements this interface provides the information implementation class for the spring Contxt context that must be configured in the Spring configuration file (Bean container loading)
-beannameaware Ibid.

A conceptual explanation of spring

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.