Spring basics (1)

Introduction to Spring

Spring is derived from the core of the article "Expoer One-on-one j2ee development and Design".

Spring was launched in 2003:Spring's two core IOCs and APOs

Spring data, Spring boot, Spring cloud, and Spring framework are now launched. These are all extended from Spring.

Spring is a lightweight inversion of control (IOC) and aspect-oriented (AOP) container framework.

  • Spring's design philosophy: to make the existing technology easier to use (simplify enterprise-level development). It is a hodgepodge in itself, integrating the existing technical framework.

  • SSH:struct2 + spring + hibernate

  • SSM:springMVC + spring + mybatis

(Spring is a fusion agent)

Spring official website: https://spring.io/projects/spring-framework#overview

Spring official download address: https://maven.springframework.org/release/org/springframework/spring/

Spring official documentation: https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html

GitHub address: https://github.com/spring-project/spring-framework

Spring Chinese documentation: https://www.docs4dev.com/docs/zh/spring-framework/4.3.21.RELEASE/reference/spring-introduction.html

Spring advantages:

  • spring is a free framework.
  • Spring is a lightweight, non-invasive framework (the original situation of the code will not be changed after the introduction of spring).
  • Inversion of Control (IOC), Aspect-Oriented Programming (AOP)
  • Support transaction processing, support for framework integration

Disadvantages of spring:

  • The configuration is very cumbersome.

Set up the Spring environment

Manen depends on:

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.0.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.0.RELEASE</version>
</dependency>

Spring seven modules

Insert picture description here

Spring's learning route
Insert picture description here

Modern java development is based on spring development.

  • Spring boot
    • Is a scaffold for rapid development
    • A single microservice can be quickly developed based on spring boot. (Just a small module)
    • The convention in spring boot development is greater than the configuration
  • Spring Cloud
    • Spring cloud is implemented by the user's spring boot.

IOC theory

Previous business use:

  1. Dao layer interface
  2. DaoImpl implementation class
  3. Service layer interface
  4. ServiceImpl implementation class

In the code I wrote before, the service layer calls the dao layer to be hard-coded. That is, a fixed dao class is created for use in a business method. This way of writing is flawed, and changes in user needs will affect our code. If the amount of program code is large, the cost of modification once is very large!

E.g:

// 原来的代码
public interface UserDao {
    
    
    void print();
}

public class UserDaoImpl implements UserDao{
    
    
    @Override
    public void print() {
    
    
        System.out.println("使用了UserDao");
    }
}

public interface UserService {
    
    
    void userServicePring();
}

public class UserServiceImpl implements UserService{
    
    
    private UserDaoImpl userDao;
    @Override
    public void userServicePring() {
    
    
        userDao = new UserDaoImpl();
        userDao.print();
    }
}

public class MyTest {
    
    
    public static void main(String[] args) {
    
    
        UserService userService = new UserServiceImpl(userDao);
        userService.userServicePring();
    }
}

If we add a UserDaoUseOracleImpl (using Oracle for database connection, the method is exactly the same as UserDao), then we must re-modify the code in UserServiceImpl , which is very bad at this time.

public class UserDaoUseOracleImpl implements UserDao{
    
    
    @Override
    public void print() {
    
    
        System.out.println("使用了UserDaoUseOracle");
    }
}
public class UserServiceImpl implements UserService{
    
    
    private UserDaoUseOracleImpl userDao;
    @Override
    public void userServicePring() {
    
    
        userDao = new UserDaoUseOracleImpl();
        userDao.print();
    }
}

Dynamic injection (IOC prototype)

We can use set to achieve dynamic value injection, so there is no need to modify the code of the service layer.

public interface UserDao {
    
    
    void print();
}

public class UserDaoImpl implements UserDao{
    
    
    @Override
    public void print() {
    
    
        System.out.println("使用了UserDao");
    }
}

public class UserDaoUseOracleImpl implements UserDao{
    
    
    @Override
    public void print() {
    
    
        System.out.println("使用了UserDaoUseOracle");
    }
}

public interface UserService {
    
    
    void userServicePring();
}

public class UserServiceImpl implements UserService{
    
    
    private UserDao userDao;
    @Override
    public void userServicePring() {
    
    
        userDao.print();
    }
    
    public void setUserDao(UserDao userDao){
    
    
        this.userDao = userDao
    }
}

public class MyTest {
    
    
    public static void main(String[] args) {
    
    
        UserService userService = new UserServiceImpl();
        userService.setUserDao(new UserDaoImpl());
        userService.userServicePring();
        userService.setUserDao(new UserDaoUseOracleImpl())
        userService.userServicePring();
    }
}
  • Before, the program created the object on its own initiative, and the control was in the program.
  • After using set injection, the control is in the hands of the user programmer.

This kind of thinking essentially solves the problem. We business programmers no longer need to manage the creation of objects (created by user programmers). It reduces the coupling of the system and can focus more on business realization.

This kind of injection idea is widely used at the bottom of Spring! It allows user programmers to create objects without changing the underlying code, which improves the dynamics.

The essence of IOC: Inversion of control is a kind of thought. After the use of inversion of control, the creation of objects is handed over to a third party (the essence of inversion of control is that the way to obtain dependent objects is reversed: originally the program creates dependent objects, and now it is the user who creates dependencies. Object)

There are many ways to implement IOC, DI (Dependency Injection) is just one of them.
Insert picture description here

Inversion of control is a way of generating specific objects through description (XML or annotation) and through a third party. It is the IOC container that implements the inversion of control in Spring, and its implementation method is dependency injection (DI)

For example, the small example written before: The Service class is similar to an IOC container, which generates service objects with different functions through set injection.

As shown above: Spring can manage objects (components)

Hello Spring

A simple spring demo:

  1. Create a bean class
    • Note that Bean must have set and get methods (because dependency injection is injected using set methods)
public class Hello {
    
    
    private String name;
    
    public Hello() {
    
    
    }

    public Hello(String name) {
    
    
        this.name = name;
    }

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    @Override
    public String toString() {
    
    
        return "Hello{" +
                "name='" + name + '\'' +
                '}';
    }
}
  1. Configure Bean xml configuration file (used to configure Bean object)
    • A bean is equivalent to an object, and the id is the logical object name. This object is placed in the spring container and managed by the spring container.
    • Configure it to the spring container, and create the corresponding object based on the xml in the container.
<!-- beans.xml -->

<?xml version="1.0" encoding="UTF-8"?>
<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">

    <bean id="hello1" class="com.qiu.pojo.Hello">
        <property name="name" value="spring"/>
        <!-- ref引用的是一个bean对象 -->
        <property name="h" ref="hello2"/>
    </bean>
    
    <bean id="hello2" class="com.qiu.pojo.Hello">
    </bean>

</beans>
  1. Use the ClassPathXmlApplicationContext class to create a spring container
    • The container is generated through the beans.xml configuration, that is, the container owns and manages the objects declared in the xml file.
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Hello hello = (Hello) context.getBean("hello1");
System.out.println(hello.toString());

This is called inversion of control: the traditional program object is explicitly new out of the program, and the object is obtained by configuring the xml file using the spring container after using spring.The control is in the hands of the user, and the business can be modified by modifying the xml file without modifying the entire system code.

The traditional program object is the program itself, so the program is highly dependent on the specific object. If the object code of the entire system needs to be changed if the business is changed, this is the embodiment of high coupling!Now the management of objects is carried out through the spring container. Spring is equivalent to a bridge between the program and the object, so that the impact of changing the object on the program is minimal. This is the embodiment of decoupling!

How IOC creates objects

Objects are automatically created in the IOC container, and the timing of object creation is done when the xml file is loaded.The no-parameter construction of the bean called when creating the object to create the object.

Use parameterized construction to create Bean

Use the constructor tag constructor-arg (the property tag is used when assigning a value to the property, the parameterless construction called when assigning a value to the property)In other words, choose one: to use property is to call parameterless, and set to assign; to use constructor-arg is to call a parameterized construct to create an object.

class User{
    
    
    private String name;
    private Child child;
    public User{
    
    }
    public User(String name, Child child){
    
    
        this.name = name;
        this.child = child;
    }
    public String getName() {
    
    
        return name;
    }
    public void setName(String name) {
    
    
        this.name = name;
    }
    public Child getChild() {
    
    
        return child;
    }
    public void setChild(Child child) {
    
    
        this.child = child;
    }
}
  1. Use subscripts for parameterized construction assignment
<beans>
    <bean id="child" class="com.qiu.pojo.Child"></bean>
    
    <!-- 使用下标进行有参构造 -->
	<bean id="user" class="com.qiu.pojo.User">
    	<constructor-arg index="0" value="useruser"/>
        <constructor-arg index="1" ref="child"/>
    </bean>
</beans>
  1. Assignment of parameter structure according to the type

Not recommended for use! If the constructor has multiple parameters with the same parameter value type, it will not be recognized!

<beans>
    <bean id="child" class="com.qiu.pojo.Child"></bean>
    
    <!-- 使用下标进行有参构造 -->
	<bean id="user" class="com.qiu.pojo.User">
    	<constructor-arg type="java.lang.String" value="useruser"/>
        <constructor-arg type="com.qiu.pojo.Child" ref="child"/>
    </bean>
</beans>
  1. Assignment of parameter structure by parameter name
<beans>
    <bean id="child1" class="com.qiu.pojo.Child"></bean>
    
    <!-- 使用下标进行有参构造 -->
	<bean id="user" class="com.qiu.pojo.User">
    	<constructor-arg name="name" value="useruser"/>
        <constructor-arg name="child" ref="child1"/>
    </bean>
</beans>

Guess you like

Origin blog.csdn.net/qq_43477218/article/details/113836445