The most concise Spring framework

Keywords: Java Spring mvc

Spring framework

-How to learn frame

  1. Know what a framework can do
  2. Frame syntax
  3. Internal implementation will be considered after use
  4. Through the above steps, you can implement a framework yourself

1, Overview

1. What is the framework

  • Spring is a container management object, with attribute assignment and reflection mechanism at the bottom

  • Mainly to solve the difficulty of enterprise development

  • The management decoupling between modules and classes solves the relationship between objects

  • Core technology ioc aop

2. Advantages

  • Light weight

  • Decoupling for interface programming

  • Support AOP aspect oriented programming

  • Easy integration with other frames like sockets

    3. Spring architecture

    Data access Web AOP integration container test

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-peze7a1d-1630758411105) (C: \ users \ administrator \ appdata \ roaming \ typora \ typera user images \ image-20210904113452824. PNG)]

2, IOC control reversal

1. Why use IOC:

Purpose to reduce code changes and realize decoupling of different functions

2. IOC (control reversal) is an idea.

  • Control: object creation, assignment, and management of object relationships

  • Reversal: transfer the developer's rights to the new object to the container implementation

  • Forward conversion: developers use the new object in the code

  • Container: a server software, a framework (Spring)

3. Technical realization of IOC:

DI realizes the function of IOC

DI: dependency injection only needs to provide the name of the object, and object creation and assignment are implemented inside the container

3, Introductory case

1. Add dependency

<!--//Spring dependency -- >
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.2.5.RELEASE</version>
</dependency>

2. Create classes (interface and implementation classes)

Create an interface under the service package and an implementation class under the imp package

3. Create Spring configuration file

<!--In the resource directory to create Spring of xml-->
<?xml version="1.0" encoding="UTF-8"?>
<!--beans: Root label spring hold java Object into bean-->
<!--spring-beans.xsd: Format constraint file-->
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--    statement bean tell spring create object 
    id: Custom name spring Find the object by this name
    class: Fully qualified name (cannot be an interface class)-->
    <bean id="some" class="com.baidu.service.imp.SomeServiceImp"/>
</beans>

4. Test object

//ApplicationContext: it is the container of Spring and gets objects through the container
//ClassPathXmlApplicationContext: load Spring configuration file from classpath
ApplicationContext app = 
new ClassPathXmlApplicationContext("beans.xml");

//getBean method: get
xxxx xx = 
(xxxx)app.getBean("In the configuration file bean of id");

5. Precautions:

spirng: when the object is created: when the spring container is created, all the objects of beans are created

spring: default call parameterless construction

Get the information of the objects in the container api:

getBeanDefinitionCount(): get the number of defined objects

getBeanDefinitionNames(): get the names of all definition objects

4, DI implementation syntax

1. Complete attribute assignment through spring

Basic types and strings are simple types here

quote

1.1 implementation of DI based on XML

In the spring configuration file, use tags and properties

set injection

set method of spring calling class must have

//Assign a value to a property in the spring configuration file bean
//Actual call to set method
<property name="Attribute name" value="All values should be quoted"/>

//reference type
<property name="Attribute name" ref="time"/>
<bean id="time" class="java.util.Date"/>
Structural injection

Call the parameterized constructor of the class

<constructor-arg 
    name="New parameter name of constructor"
    index="Construct method parameter position from 0"
    ref="quote"
    value="Simple type value"/> 
//There is also an index that is omitted, but according to the method parameter position

1.2 annotation based DI implementation

In the spring configuration file, use spring annotations

1.3 reference type automatic injection

byName:
//The property name of the class is consistent with the bean id name and type of the spring configuration file
<bean id=""class="" autowire="byName">
    //Simple type assignment reference automatic
</bean>
Bytype:
//spring configuration file bean class type is consistent or parent-child interface class relationship
//Unique bean
<bean id=""class="" autowire="byType">
    //Simple type assignment reference automatic
</bean>

1.4 multiple configurations

1. Advantages

  • Small files and high efficiency
  • Avoid multi user conflicts
  1. By function module

  2. Functions by class

2. Use

//Create a Srping main configuration file total.xml
//It contains other configuration files spring-*.xml
//Generally, objects are not defined
<import resoutce = "classpath:route.xml">
    
//Wildcards can be used*
//Cannot contain total.xml
//It can only be used under the directory
<import resoutce = "classpath:spring-*.xml">

Posted by Hard Styler on Sat, 04 Sep 2021 17:24:42 -0700