Hello everyone, I believe the Java Senior Engineer to the spring framework are very familiar with it! I would like to summarize spring in my personal opinion and I hope you will be helpful.
I. Outline of the content
1. Spring's origins and background
2. Spring Introduction
3, Spring's core module
4, SPRINGIOC and AOP
5. Why to use Spring
6, Spring's purpose? Significance?
7. Spring's advantages and disadvantages
8. What spring brings to us
9. What spring can do
Ii. Specific Content
1. Spring's origins and background
In his book "Expert One to one, development," in 2002, Rod Johson questioned the state of the Java EE Orthodox Framework's bloated, inefficient, out-of-the-box realities and actively sought to explore the path of innovation. With this book as the guiding ideology, he wrote the INTERFACE21 framework, an attempt to break through the traditional development of Java EE, from the actual needs of the light, agile, easy to develop, test and deploy a lightweight development framework. The spring framework, which is based on the INTERFACE21 framework, has been redesigned and enriched, and has been released on March 24, 2004, with 1.0 official editions.
Spring is dedicated to the solutions of each layer of the Java EE application, rather than just focusing on one layer of the solution. It can be said that spring is the "one-stop" choice of enterprise application development, and runs through the performance layer, the business layer and the persistence layer. However, spring does not want to replace the existing frameworks and seamlessly integrate them.
Spring's design goal: to follow a series of interface standards, the benefit is that only simple Java objects or JavaBean can be used for Java EE Development, so that the development of the introduction, testing, application deployment has been simplified.
2. Spring Introduction
Spring is an open source, a layered framework for simplifying the development of enterprise-class programs in the Java language.
Spring is an open source framework that is created by Rod Johnson. It is created to address the complexities of enterprise application development. Spring uses basic JavaBean to accomplish things that were previously only possible by EJBS. However, the use of spring is not limited to server-side development. From the standpoint of simplicity, testability, and loose coupling, any Java application can benefit from spring.
3, Spring's core module
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. The functions of each module are as follows:
1) Spring Core: Core container, Beanfactory provides the component lifecycle management, component creation, assembly, destruction and other functions
Springcontext:applicationcontext, expand the core container, provide event processing, internationalization and other functions. It provides the functionality of some enterprise-class services and provides Jndi,ejb,rmi support.
2) Spring AOP: Provide slice support
3) Spring DAO: Provides transactional support, Jdbc,dao support
4) Spring ORM: Package or support for popular O/R mapping
5) Spring Web: Provides Web application context, provides functional support for web development, such as requests, forms, exceptions, etc.
6) Spring Web MVC: A full-featured MVC framework that acts as a struts.
7) 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.
4, SPRINGIOC and AOP
4, 1, the concept of the IOC
Ioc,inversion of control, inversion of controls.
The object's collaborative relationship is the responsibility of the object itself.
Dependency Injection: An object's collaborative relationship has a container to build.
4, 2, type of IOC
(1) Based on specific interface (intrusive scheme)
(2) based on set method
(3) constructor-based
4, 3, Spring container
The spring container is responsible for generating, assembling, destroying components, and is responsible for event handling, internationalization and other functions.
(1) beanfactory<interface>
① Core container, responsible for component generation and assembly
② implementations mainly include XML beanfactory
(2) ApplicationContext
(3) Webapplicationcontext
(4) ...
4, 4, use of the IOC
Resource:interface, used to package resources
An implementation of xmlbeanfactory:beanfactory, using the resource object to find the configuration file
Beanfactory.gerbean ("Beanid"): Gets a bean instance named with a parameter or ID equal to the parameter value.
Beanfactory (container) By default, the object is returned in a single-case manner. A container instantiates an object only when it calls the Getbean method.
(1) Spring can be configured with XML or. properties files
(2) configuration file (XML)
The root element <beans> can have multiple <bean> child elements, each <bean> represents an object that needs to be assembled.
1. Setter Injection
(1) Inject simple attributes (basic types in string and 8)
<beans>
<bean id= "Beanid" class= "Classpath" autowire= "dependency-check=" ">
<property name= "ParameterName" >
<value>parameterValue</value>
</property>
</bean>
</beans>
For basic types, the spring container automatically converts the type to assign a value.
(2) Injected object
<bean>
<ref local= "Beanid" >
</bean>
A, let the spring container find the appropriate bean in the current configuration file, and call the set method to inject the bean.
B, the definition of one bean is nested in another bean (poor readability), the nested bean cannot be returned with Getbean ()
C, use <ref bean= "" > Search multiple configuration files to inject
(3) Injection set type
①set
Set holds strings, objects, collections, duplicates are not allowed
②list
List can be placed in a string, object, list
③map
MAP has <entry> child elements to access Key,value,key can only be string
④properties
Properties have <props> child elements
Spring's AOP
First, AOP (aspect-oriented programming, aspect-oriented programming):
What is AOP?
Definition: The cross-business logic in the program is extracted, called the slice. The process of dynamically weaving these facets into the target object and then generating a proxy object.
Ii. the core concept of AOP
1. Aspect (Slice)
Facets are collectively referred to as cross-business logic.
2, Joinpoint (connection point)
A connection point that refers to the position (method, property, and so on) in which the slice can be woven into the target object.
3, Advice (notice)
Notice, refers to the specific implementation of facets.
4, Pointcut (entry point)
A pointcut is a rule that informs which methods or properties of a class are applied to which classes.
5, Introduction (introduced)
Introduces a special notification that dynamically adds a method or property to an object.
6. Weaving (weave in)
Weaving, refers to inserting a notification into the target object.
7. Target (object)
The target object, which is the object that needs to be woven into the plane.
8. Proxy (Agent object)
A proxy object that refers to the object formed after the slice is woven into the target object.
Three, Spring AOP principle
Use dynamic proxy mode.
Spring AOP takes the process of dynamic proxy:
(1) Dynamically weave the slice into the target object (the proxy class) and form a proxy object by using the dynamic proxy method;
(2) If the target object does not implement the proxy interface, Spring uses CGLIB to generate the proxy object, which is the subclass of the target object;
(3) If the target object is the final class and the proxy interface is not implemented, AOP cannot be applied.
Iv. Notice of Spring
1. Spring Notification type
(1) Methodbeforeadvice
Class full Name: Org.springframework.aop.MethodBeforeAdvice
Before the method call, do the processing.
Can not change the return value
Cannot change the process of the target method, nor can it interrupt the processing of the process (unless an exception is thrown)
(2) Afterreturningadvice
Class full Name: Org.springframework.aop.AfterReturningAdvice
After the method call, do the processing.
Can not change the return value
Cannot change the process of the target method, nor can it interrupt the processing of the process (unless an exception is thrown)
(3) Methodinterceptor
Class full Name: Org.aopalliance.intercept.MethodInterceptor
Do the processing before and after the method call.
You can change the return value, or you can change the process.
(4) Throwsadvice
Class full Name: Org.springframework.aop.ThrowsAdvice
After the method throws an exception, do the processing.
When the notification finishes processing the exception, it simply throws the exception again to the target invocation method.
2, the configuration process:
(1) Configuring the target object
(2) Configuring notifications
(3) using Proxyfactorybean to weave notifications into target objects to form a dynamic proxy object
(4) The client uses dynamic proxies to access the target object's methods.
By default, notifications are applied to all methods.
Pointcut:
Depending on the method and class decide where to weave the notice
Advisor:
Combine the pointcut with the advice.
5. Why to use Spring
Because as long as an application developed by OOP is done by collaborating between objects and objects, the Relationship management (dependency) between objects and objects is cumbersome to maintain with code. With spring, you can save the trouble.
Spring is unique for a number of reasons:
It locates areas that are not in many other popular frameworks. Spring focuses on providing a way to manage your business objects.
Spring is comprehensive and modular. Spring has a layered architecture, which means that you can choose to use any part of it that is isolated, and that its architecture remains inherently stable. So from your study, you can get the greatest value. For example, you might choose to use spring only to simplify the use of JDBC, or to manage all business objects.
It's designed from the bottom to help you write code that is easy to test. Spring is the ideal framework for testing drive engineering.
Spring does not require more than one framework for your project. Spring is a potentially one-stop solution for most of the infrastructure associated with typical applications. It also relates to content that is not considered by other frameworks.
6, Spring's purpose? Significance?
Objective: To solve the complexity of enterprise application development
Function: Use basic JavaBean instead of EJB, and provide more enterprise application function
Scope: Any Java application
Significance: 1, reduce development costs. 2, easy to use.
7. Spring's advantages and disadvantages
7.1. Provides a way to manage objects that can be effectively organized by middle-tier objects. A perfect framework for "adhesives".
7.2. Adopt A hierarchical structure that can be incrementally introduced into the project.
7.3. Facilitates the development of interface-oriented programming habits.
7.4. One of the purposes is to write code that is easy to test.
7.5. Non-intrusive, the application's dependency on the spring API can be minimized.
7.6. A consistent data access interface.
7.7. A lightweight architecture solution.
8. What spring brings to us
Easy decoupling for simplified development
With the IOC container provided by spring, we can 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
The AOP functionality provided by spring facilitates aspect-oriented programming, and many features that are not easily implemented with traditional OOP can be easily coped with by AOP.
Support for declarative Transactions
In spring, we can get rid of the tedious transaction management code, make the transaction management with the declarative way, improve the development efficiency and quality.
Easy testing of programs
Almost all of the testing can be done with non-container-dependent programming, and in spring, testing is no longer an expensive operation, but something that can be done at hand.
Easy integration of a variety of excellent frameworks
Spring does not exclude a variety of excellent open source frameworks, instead, spring can reduce the difficulty of using various frameworks, and spring provides direct support for a variety of excellent frameworks such as Struts,hibernate, Hession, Quartz, etc.
Reduce the difficulty of using Java EE APIs
Spring provides a thin layer of packaging for many of the hard-to-use Java EE APIs, such as Jdbc,javamail, remote invocation, and so on, and the ease of use of these Java EE APIs is greatly reduced by spring's simple encapsulation.
Java source code is a classic learning paradigm
9. What spring can do
The main purpose of spring is to make the Java EE easy to use and promote good programming habits.
Spring's goal is to make existing technologies easier to use.
Summary of the spring framework