Spring - bernardopnunes/SoftwareEngineeringSDUW GitHub Wiki

Groups:

  1. EDG

What is Spring

Spring is an open source framework. Spring is a lightweight java development framework that emerged in 2003. It is derived from some concepts and prototypes that rod Johnson elaborated in his book expert one on one J2EE development and design. It is created to solve the complexity of enterprise application development. One of the main advantages of the framework is its layered architecture, which allows users to choose which component to use, and provides an integrated framework for J2EE application development. Spring uses basic JavaBeans to do things that were previously only possible with EJBs. However, spring's use is not limited to server-side development. Any Java application can benefit from spring in terms of simplicity, testability, and loose coupling. The core of spring is inversion of control (IOC) and aspect oriented (AOP). In short, spring is a layered Java Se / EE full stack (one-stop) lightweight open source framework.

Advantage of Spring

Easy decoupling and development (high cohesion and low coupling)
AOP programming support
Support for declarative transactions
Convenient program testing
Convenient integration of various excellent frameworks
Reduce the difficulty of using Java EE API

Interface

Beanfactory and ApplicationContext are two core interfaces of Spring, which can be used as containers of spring. Where ApplicationContext is the sub interface of Beanfactory

BeanFactory

(1)The original interface of spring, the implementation class function for the original interface is relatively single
(2)Beanfactory interface implements the container of the class, which is characterized in that the object is created every time the object is obtained

ApplicationContext

(1)Every time the container starts, all objects configured in the container are created
(2)More features available
(3)Load the configuration file from the classpath: classpathxmlapplicationcontext
(4)Load the configuration file from the absolute path of the hard disk: filesystemxtapplication

Element in Spring

bean: use this element to describe the objects that need to be managed by the spring container
name: name the managed object, getBean ("name value") when getting the object
class: the full class name of the managed object
id: as like as two peas name attribute, the name is not repeatable, and no special characters can be used

Three ways to create spring objects

1.Empty parameter

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"xsi:schemaLocation
    <!-- Empty parameter construction -->
    <bean name="hello" class="cn.itcats.spring.hello.HelloWorld"></bean>
</beans>

2.Static factory

<bean name="user" class="cn.itcats.UserFactory" factory-method="createUser"></bean>

3.Instance factory

<bean name="user2" factory-bean="userFactory" factory-method="createUser"></bean>
<bean name=“userFactory” class=“cn.itcats.UserFactory”></bean>

Spring injection mode

1.set

Value type inject with value reference type inject with ref

<!-- set -->  <!-- student -->
<bean name="student" class="cn.itcats.spring.domain.Student">
    <!-- Value injection -->
    <property name="name" value="zhaoboy"></property>
    <property name="grade" value="80"></property>
    <!-- Reference injection -->
    <property name="teacher" ref="teacher"></property>
</bean>

<!-- teacher -->
<bean name="teacher" class="cn.itcat.spring.domain.Teacher">
    <property name="name" value="doctor"></property>
    <property name="class" value="software engineering"></property>
</bean>

2.Constructor

<!-- Constructor injection -->
<bean name="student" class="cn.itcats.spring.domain.Student">
    <!-- name:Parameter name of constructor -->
    <!-- name:Parameter index of constructor -->
    <!-- name:Parameter type of constructor -->
    <constructor-arg name="name" index="1" type="java.lang.Sring" value="zhaoboy"></constructor-arg>
    <constructor-arg name="grade" index="0" type="java.lang.Integer" value="80"></constructor-arg>
    <constructor-arg name="name" index="2" ref="teacher"></constructor-arg>
</bean>

Design pattern

1.Factory mode:

Beanfactory is the embodiment of simple factory mode, which is used to create instances of objects

2.Singleton mode:

Bean defaults to singleton mode

3.Proxy mode:

Spring's AOP function uses JDK's dynamic proxy and CGLIB bytecode generation technology

4.Template method:

used to solve the problem of code duplication. For example. Resttemplate, Jmstemtemplate, Jpatemplate

5.Observer pattern:

define an object key as a one to many dependency. When the state of an object changes, all objects that depend on it will be notified to be braked and updated, such as the implementation of listener in Spring -- Applicationlistener

IOC and AOP

IOC keeps the collaborative components loosely coupled, while AOP programming allows you to separate the functions throughout the application layers to form reusable functional components.

IOC understanding

(1) IOC is the inversion of control, which refers to the transfer of control over the creation of objects. The initiative and opportunity of creating objects were controlled by itself before, but now this power is transferred to the spring container, and the container creates instances and manages the dependency between each instance according to the configuration file. The loose coupling between objects is conducive to the reuse of functions. Di dependency injection and inversion of control are different descriptions of the same concept, that is, the application depends on IOC container to dynamically inject external resources needed by the object at runtime.
(2) The most intuitive expression is that IOC lets the creation of objects not go to new. It can be automatically produced by spring. Using Java's reflection mechanism, it can dynamically create objects and manage objects according to the configuration file at runtime, and call object methods.
(3) Spring's IOC can be injected in three ways: constructor injection, setter method injection, and annotation injection.

AOP understanding

AOP, commonly known as facet oriented, is a complement of object-oriented. It is used to extract and encapsulate the common behaviors and logic that have nothing to do with business but have an impact on multiple objects into a reusable module. This module is named "Aspect", which reduces the repeated code in the system, reduces the coupling between modules, and improves the system's performance Maintainability. It can be used for authority authentication, log and transaction processing.
The key of AOP implementation lies in agent mode, which is mainly divided into static agent and dynamic agent. The static proxy is represented by AspectJ; the dynamic proxy is represented by spring AOP.
(1) AspectJ is the enhancement of static agent. The so-called static agent is that AOP framework will generate AOP agent classes in the compilation phase, so it is also called compile time enhancement. It will weave AspectJ (facet) into Java bytecode in the compilation phase, and the runtime is the enhanced AOP object.
(2) The dynamic proxy used by spring AOP, means that the AOP framework does not modify the bytecode, but temporarily generates an AOP object for the method in memory every time it runs. This AOP object contains all the methods of the target object, and enhances the processing at a specific point of tangency, and calls back the methods of the original object.

⚠️ **GitHub.com Fallback** ⚠️