In our daily use, Spring is often bundled with “SSM”, “SSH”, and especially with SpringMVC. This is accompanied by a plethora of configuration files, which can lead to a lack of a clear view of Spring as just one big framework. Spring itself is a lightweight framework that redefines Java. This article will briefly build on a Spring quickstart project and talk about “clean” Spring.

Build the Spring Project

A Spring project theoretically requires only one Maven dependency.

 <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring.version>5.1.3. RELEASE</spring.version>
  </properties>    

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

After introducing this dependency, the project adds four of Spring’s core jars:

Then create a Bean to be managed by Spring:

import org.springframework.stereotype.Service;

@Service
public class Student {

    private String username = "xuan";

    private String password;

    public String getUsername(a) {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword(a) {
        return password;
    }

    public void setPassword(String password) {
        this.password = password; }}Copy the code

With annotations, Spring manages this class at initialization. Let’s see if we can get it:

    @Test
    public void test1(a) {
        AnnotationConfigApplicationContext appcationContext = new 
                 AnnotationConfigApplicationContext("com.lele");
        Student student = (Student)appcationContext.getBean("student");
        System.out.println(student.getUsername());
    }
Copy the code

This works, but under normal circumstances we still need to see the Spring logs so that we can debug if something goes wrong:

 <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>LATEST</version>
  </dependency>
Copy the code

Print again:

Ok, so now that we have implemented the Spring project setup, isn’t it refreshing? It’s just a container that helps you manage your objects.

XSD introduced

In Spring, you can also create XML to manage objects through configuration files. Simply paste a template:

<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"
    default-lazy-init="false">

    <! -- This tag is used to replace annotations -->
    <bean class="com.lele.Student" id="student"/>
</beans>
Copy the code

In addition to the Spring default tags, there will be some extended custom tags in the configuration file. If you want to import a custom tag, you need to import its corresponding XSD file:

It is worth noting that this XSD file is not fetched from the network, but is mapped locally through a file.

In this local XSD file, we can see the Component-scan tag:

Container loading mode

When we fetch a Spring-managed object, Spring builds the object and puts it in the container. To get an object, we need to create a container and then get it from the container. There are four types of containers in Spring, each for different scenarios:

1. Obtain the configuration file from the classpath

ClassPathXmlApplicationContext appcationContext = new 
    ClassPathXmlApplicationContext("spring.xml");
Copy the code

2. File system path to obtain configuration files (absolute path, rarely used)

3. There is no configuration file

AnnotationConfigApplicationContext appcationContext = new 	
    AnnotationConfigApplicationContext("com.lele");
Copy the code

4. Inline loading

You need to import the Spring Boot dependency, which loads the embedded Tomcat when you start the container.

 new EmbeddedWebApplicationContext();
Copy the code

At this point, we see the specific approach that Spring uses. Is it easy? Of course, this is just the beginning of Spring. Keep an eye out for future articles that will take a step-by-step look at Spring’s internal workings.