An Introduction to Spring

Keywords: Spring xml Java Hibernate

Working Principle of Spring

Partners who have contacted Spring know that it is a free and open source framework, and it is often used in today's enterprises because of its great advantages in the enterprise and projects are basically inseparable from the following, let's talk about Spring.

I. Advantages of Spring

1. It's not unreasonable to call it adhesives. Spring can easily integrate any excellent framework, and it does not exclude various commonly used frameworks (struts2, hibernate,mybatis,quartz, jpa) and so on.

2. Control inversion handed over objects to Spring, simplifying development and reducing code coupling.

3. AOP, one of Spring's powerful functions, facilitates the management of transactions in projects. We only need to configure to complete the management of transactions.

4. Easy to test the program. spring's support for Junit 4 can facilitate the test program through annotations.

Wait a minute.

II. Spring Understanding

IOC and AOP are the core of IOC. Dynamic injection enables developers to create objects automatically without explicit new objects. This is actually the reflection mechanism in java. Reflection is to create and invoke objects dynamically at runtime, when running Spring. The xml configuration file dynamically creates and invokes the methods in it.

Another core of Spring is AOP, which is aspect-oriented programming. It can supervise and control a certain kind of object (that is, to call the module you specified before and after calling the specific method of such object) so as to achieve the function of expanding a module. These are achieved by configuring classes.

The purpose of Spring is:
It means that there is no direct or indirect relationship between the object and the object. Through a bean factory, that is, inside our configuration file, the method can be invoked directly by reflection.

Spring is a container, so long as the object in the container will have these conveniences and functions provided by Spring.

In Spring framework, the most classic design pattern is template design pattern. Here you can learn it by yourself.
Click to learn about template design patterns
The essence of Spring is always two points.

Let's talk about the running process with examples.

entity

public class Person {

    private String name;

    private int age;
    
    private Book book;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Book getBook() {
        return book;
    }

    public void setBook(Book book) {
        this.book = book;
    }

public class Book {
    
    private String name;
    
    private int price;
    
    private String place;

    public String getName() {
        return name;
    }

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

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getPlace() {
        return place;
    }

    public void setPlace(String place) {
        this.place = place;
    }
  }

applicationContext.xml configuration file

<?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 Configuration file -->
<bean id="person" class="org.jingdong.bean.life.Person">
<property name="name" value="grl"></property>
<property name="age" value="11"></property>
<property name="book" ref="book"></property>
</bean>
<bean id="book" class="org.jingdong.bean.life.Book">
<property name="name" value="think in java"></property>
<property name="place" value="USA"></property>
<property name="price" value="79"></property>
</beans>

Test class

public class Main {
    public static void main(String[] args) {
        // Create IOC containers
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        //Getting bean instances from containers
        Person person = (Person) ac.getBean("person");
        //Using bean s
        System.out.println(person.getName());
    }
}

The above code can basically operate on our Spring IOC (DI dependency injection)

Principle analysis:
When ApplicationContext AC = new ClassPathXml ApplicationContext ("applicationContext.xml");
When execution is created, Spring objects are created, and bean s in applicationContext.xml are created into memory.

How to configure bean s:
1. Through the Application Context context container, when the XML configuration file is loaded, the bean is instantiated.

2.BeanFactory: When loading the configuration file, the bean s in the configuration file are not instantiated, and are created only when the instance is obtained through getBean().

Personal summary: Beans configured through BeanFactory save more memory than bean s configured through Application Context.

The above is a personal understanding of Spring to supplement or explore points can be sent to the mailbox

2010944641@qq.com

============== Thank you for watching.==============

Posted by john_6767 on Tue, 27 Aug 2019 04:34:02 -0700