Chapter 01-Spring Overview

Section 01 – Spring Framework Overview

Version 5.3.13-docs.spring. IO /spring-fram…

Spring makes it easy to create Java enterprise applications.

Spring is open source.

1.1 Spring Framework

  • Spring simplifies enterprise application development by managing Javabeans through Spring’s core IoC container, reducing coupling
  • Spring is an open source framework
  • Spring is non-invasive and objects in spring-developed applications do not rely on the Spring API
  • Spring DI dependency injection is the classic embodiment of IOC inversion of control
  • Spring AOP is faceted oriented programming
  • Spring componentization, javabeans managed by Spring IoC can be implemented through XML file configurations or annotations

In the narrow sense, Spring refers to the Spring Framework itself, namely, Core Container. Over time, many modules have been developed based on Core Container. In the broad sense, Spring refers to many Spring modules. For example, Spring MVC, Spring Boot, Spring Data, Spring Cloud, etc. Click Spring. IO /projects to view many modules of Spring

The term “Spring” means different things in different contexts. It can be used to refer to the Spring Framework project itself, which is where it all started. Over time, other Spring projects have been built on top of the Spring Framework. Most often, when people say “Spring”, they mean the entire family of projects. This reference documentation focuses on the foundation: the Spring Framework itself.

Spring Framework module division

1.2 The IoC Container

The Ioc container is responsible for creating the Bean instead of using the new keyword

DI stands for Dependency Injection. The IoC container can know which component or class needs another component or class to run, and inject it reflectively

1.3 Creating an IoC Container

Create a Maven project ➡️ Add a Maven dependency ➡️ Create a configuration file ➡️ test

<properties>
    <spring-version>5.3.13</spring-version>
</properties>


<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring-version}</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
</dependencies>
Copy the code

1 One ️ Add entity class Person in entity package

public class Person {
    
    private String lastName;
    private Integer age;
    private String gender;
    private String email;
    // omit the getter/setter/toString methods
}
Copy the code

2 discount ️ Create a configuration file beans.xml under the resource directory. The sample XML file can be found in the official documentation


      
<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 https://www.springframework.org/schema/beans/spring-beans.xsd">Register the Person entity class with the IoC container<bean id="person" class="com.citi.entity.Person">
    </bean>

</beans>
Copy the code

3 Discount ️ writing test class

public class ContainerTest {

    // Test getting beans from the container
    @Test
    public void testStark(a){
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:beans.xml");
        Person stark = (Person)context.getBean("stark"); System.out.println(stark); }}Copy the code

The output

This completes the process of registering the Bean to the container and getting the Bean from the container. The whole process does not use the new keyword to get the Bean. Instead, all the attributes of the Bean are written to the XML file through the XML configuration file. Get the Bean through the getBean() method of the ApplicationContext interface

The org.springframework.context.ApplicationContext interface represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the beans.

Several implementations of the ApplicationContext interface are supplied with Spring. In stand-alone applications, it is common to create an instance of ClassPathXmlApplicationContext or FileSystemXmlApplicationContext.

ApplicationContext is the IoC container interface responsible for instantiating, configuring, and assembling beans, It has two implementation class ClassPathXmlApplicationContext and FileSystemXmlApplicationContext the need when these two classes to instantiate the incoming configuration files, but into the configuration file in a different way

ApplicationContext context = new ClassPathXmlApplicationContext("classpath:beans.xml");
ApplicationContext context1 = new FileSystemXmlApplicationContext("src/main/resources/beans.xml");
Copy the code

When is the Bean instantiated? Add a no-argument constructor to the Person class and add an output statement

public class Person { private String lastName; private Integer age; private String gender; private String email; Public Person() {system.out.println (" no argument constructor called "); } // omit the getter/setter/toString method}Copy the code

Add an output statement to the ContainerTest class

@test public void testStark(){ApplicationContext context = new ClassPathXmlApplicationContext("classpath:beans.xml"); System.out.println(" container created "); Person stark = (Person)context.getBean("stark"); System.out.println(stark); }}Copy the code

Perform the test

Beans are created when the container is initialized, not when the getBean is created

In the ContainerTest method, the Bean is retrieved once, and the objects retrieved twice are verified to be equal

// Test getting beans from the container
@Test
public void testStark(a){
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:beans.xml");
    System.out.println("Container has been created");
    Person stark = (Person)context.getBean("stark");
    Person stark1 = (Person)context.getBean("stark");

    System.out.println(stark == stark1);
}
Copy the code

The output

The result is true, indicating that the Person class was instantiated only once, when the container was initialized.

conclusion

  • ApplicationContext is the IoC container interface, there are two implementation class ClassPathXmlApplicationContext and FileSystemXmlApplicationContext, through the getBean () to obtain the bean in the container
  • Register a bean with the container. The bean creation is done by the container. The bean is created when the container is initialized
  • The same bean is singleton in the container
  • “No bean named ‘XXX’ is defined”
  • When the IoC container creates a bean, the PROPERTY in the XML sets the bean’s property values using the setter of the class