This blog, the first in a series of getting started with Spring, will not explain what Spring is and the history of Spring. Instead, it will explain how to create a Spring project using IntelliJ IDEA and use a simple example to understand how to use Spring.

Create a Spring project

First, open the “New Project” pop-up box as shown below:

Then select the project type Spring on the left:

If you forget to select “Create empty spring-config. XML “, you can Create a new configuration file after the project is created.

Next, determine the project name and save path, and click the “Finish” button:

Because you need to download the Spring dependent packages, it will take a while to load.

The structure diagram of the newly built project is as follows:

2. The Spring sample

Create a new Book class and define two fields bookName,author and an instance method printBookInfo().

public class Book {
    private String bookName;

    private String author;

    public String getBookName(a) {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getAuthor(a) {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public void printBookInfo(a) {
        System.out.println("The Book Name:" + this.bookName + ", the Author:" + this.author); }}Copy the code

If we want to output book information, in the traditional way, we need the following steps:

  1. Create an instance object of the Book class
  2. Sets the bookName and Author fields of the instance object
  3. Call the printBookInfo() method of the instance object
public class Main {
    public static void main(String[] args) {

        Book book = new Book();
        book.setBookName("Ordinary world.");
        book.setAuthor("路遥"); book.printBookInfo(); }}Copy the code

Running results:

Book Name: Ordinary World,Author: Lu Yao

So how do you implement the same invocation in a Spring project?

First, modify spring-config.xml to add the following configuration:

<bean id="book" class="Book">
    <property name="bookName" value="Ordinary world."/>
    <property name="author" value="路遥"/>
</bean>
Copy the code

Then change the Main method to:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
        Book book = applicationContext.getBean("book", Book.class); book.printBookInfo(); }}Copy the code

Running results:

We will find that the results are the same as the traditional way, but with some more Spring logging information.

In the above code, we do not use the new operator to create instances of the Book class, but we can get instances of the Book class. This is the power of Spring. All instances of the class are created by the Spring container without the application itself.

3. Spring example explanation

Instance creation is left to the Spring container to create and manage, but in the above code, when is an instance of the Book class created and a field assigned?

To verify this, let’s modify the Book class.

public class Book {
    private String bookName;

    private String author;

    public Book(a){
        System.out.println("This is Book constructor.");
    }

    public String getBookName(a) {
        return bookName;
    }

    public void setBookName(String bookName) {
        System.out.println("This is Book setBookName().");
        this.bookName = bookName;
    }

    public String getAuthor(a) {
        return author;
    }

    public void setAuthor(String author) {
        System.out.println("This is Book setAuthor().");
        this.author = author;
    }

    public void printBookInfo(a) {
        System.out.println("The Book Name:" + this.bookName + ", the Author:" + this.author); }}Copy the code

Add an Author class:

public class Author {
    private String name;

    private int age;

    public Author(a) {
        System.out.println("This is Author constructor.");
    }

    public String getName(a) {
        return name;
    }

    public void setName(String name) {
        System.out.println("This is Author setName().");
        this.name = name;
    }

    public int getAge(a) {
        return age;
    }

    public void setAge(int age) {
        System.out.println("This is Author setAge().");
        this.age = age;
    }

    public void printAuthorInfo(a) {
        System.out.println("Name:" + this.name + ", the Age:" + this.age); }}Copy the code

Then modify the spring-config.xml file.

<bean id="book" class="Book">
    <property name="bookName" value="Ordinary world."/>
    <property name="author" value="路遥"/>
</bean>
<bean id="author" class="Author">
    <property name="name" value="路遥"/>
    <property name="age" value="60"/>
</bean>
Copy the code

Finally, let’s Debug the Main code to see the order of execution.

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");

        Book book = applicationContext.getBean("book", Book.class);
        book.printBookInfo();

        Author author = applicationContext.getBean("author", Author.class); author.printAuthorInfo(); }}Copy the code

For a more intuitive illustration, see the Gif below.

From the graph, we can see that the execution of the ApplicationContext ApplicationContext = new ClassPathXmlApplicationContext (” spring – config. XML “); After that, the console first outputs the following:

This is Book constructor.

This is Book setBookName().

This is Book setAuthor().

This is Author constructor.

This is Author setName().

This is Author setAge().

After this code executes, instances of the Book and Author classes have been created and fields assigned, and the rest of the code simply fetches instances from the Spring container.

4. Precautions

When fetching the Bean, the first parameter beanName must be the same as the Bean ID defined by spring-config. XML. For example, we defined book in spring-config. XML.

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");

        // Error beanName
        Book book = applicationContext.getBean("Book", Book.class); book.printBookInfo(); }}Copy the code

The following error message is displayed:

5. Source code and reference

Source code address: github.com/zwwhnly/spr… Welcome to download.

【Spring】IntelliJ IDEA build Spring environment

Create and implement a small IoC case for the Spring project in IDEA