Spring Starter (a): Creating a Spring project

As this blog first blog entry Spring series, is something that does not explain what the history of the Spring and Spring these too theoretical, the next Spring mainly on how to create a project using IntelliJ IDEA as well as through a simple example to understand the next Spring's use .

1. Create a Spring project

First, according to the open position shown in FIG. "New Project" pop-up:

Then select the project type Spring on the left:

If you forget where you select "Create empty spring-config.xml", can also create complete projects and then create a new profile.

Next, determine a good project name and save path, and then click on the "Finish" button:

Because the need to download the Spring dependent package, and therefore needs to be loaded for a while.

After the new project structure shown in FIG follows:

2. Spring Example

Book create a class that defines two fields bookName, author, and an example of a method printBookInfo ()

public class Book {
    private String bookName;

    private String author;

    public String getBookName() {
        return bookName;
    }

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

    public String getAuthor() {
        return author;
    }

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

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

If we want to output book information, according to the traditional way, you require the following steps:

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

        Book book = new Book();
        book.setBookName("平凡的世界");
        book.setAuthor("路遥");

        book.printBookInfo();
    }
}

operation result:

Book Name: Ordinary World, Author: Lu Yao

So in the Spring project, how to achieve the same call it?

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

<bean id="book" class="Book">
    <property name="bookName" value="平凡的世界"/>
    <property name="author" value="路遥"/>
</bean>

Then modify the Main method as follows:

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();
    }
}

operation result:

We will find, operating results and the traditional way, like, just a few more Spring log information.

In the above code, we did not use the new operator to create an instance of the Book class, but you can get an instance of the Book class, which is the power of the Spring, all create an instance of the class do not need to create their own applications , but to the Spring container to create and manage.

3. Spring example to explain

Although to create an instance of the Spring container to create and manage, but in the above code, when to create an instance of the Book class and field assignment of it?

To verify this question, we revise down the Book class.

public class Book {
    private String bookName;

    private String author;

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

    public String getBookName() {
        return bookName;
    }

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

    public String getAuthor() {
        return author;
    }

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

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

Add a class Author:

public class Author {
    private String name;

    private int age;

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

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

Then modify the next spring-config.xml file.

<bean id="book" class="Book">
    <property name="bookName" value="平凡的世界"/>
    <property name="author" value="路遥"/>
</bean>
<bean id="author" class="Author">
    <property name="name" value="路遥"/>
    <property name="age" value="60"/>
</bean>

Finally, we modify the code under Main Debug class to the next, look at the code execution order.

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();
    }
}

A more intuitive display, see below Gif FIG.

From the figure, we can see that, after executing ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");the console to output 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().

This code is executed, the instance of the Book and Author class class has been created and the field has been assigned, the next instance of the code just get it from the Spring container.

4. Notes

Getting Bean, bean id beanName first parameter to define consistent with the spring-config.xml, such as we defined in spring-config.xml in a book, if written at the time of acquisition is the Book, will give an error.

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");

        // 错误的beanName
        Book book = applicationContext.getBean("Book", Book.class);
        book.printBookInfo();
    }
}

Given the information as follows:

The source and reference

Source Address: https://github.com/zwwhnly/spring-action.git , welcome to download.

[Spring] Spring built environment IntelliJ IDEA

idea to create the Spring project and the achievement of a small case of IoC

The original is not easy, if that article can learn something, like a welcome point, a commentary on, off a note, this is my greatest motivation insist on writing.
If you are interested, please add my micro letter: ** zwwhnly **, waiting for you to talk technology, workplace, work and other topics (PS: I am a programmer struggle in Shanghai).

Published 34 original articles · won praise 107 · views 20000 +

Guess you like

Origin blog.csdn.net/zwwhnly/article/details/100072484