Spring入門(一):創建Spring項目


本篇博客作為Spring入門系列的第一篇博客,不會講解什么是Spring以及Spring的發展史這些太理論的東西,主要講解下如何使用IntelliJ IDEA創建Spring項目以及通過一個示例了解下Spring的簡單使用。

1. 創建Spring項目

首先,按照下圖所示打開“新建項目”彈出框:

然后在左側選擇項目類型Spring:

如果這里忘記了選擇"Create empty spring-config.xml",也可以新建完項目再新建配置文件。

接着,確定好項目名稱和保存路徑,然后點擊"Finish"按鈕:

因為需要下載Spring依賴的包,因此需要加載一會。

新建完的項目結構圖如下所示:

2. Spring示例

新建一個Book類,定義兩個字段bookName,author和一個實例方法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);
    }
}

如果我們想要輸出圖書信息,按照傳統的方式,需要以下幾步:

  1. 創建Book類的實例對象
  2. 設置實例對象的bookName字段和author字段
  3. 調用實例對象的printBookInfo()方法
public class Main {
    public static void main(String[] args) {

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

        book.printBookInfo();
    }
}

運行結果:

Book Name:平凡的世界,Author:路遙

那么在Spring項目中,如何實現同樣的調用呢?

首先,修改spring-config.xml,添加如下配置:

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

然后修改Main的方法為:

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

運行結果:

我們會發現,運行結果和傳統方式一樣,只是多了一些Spring的日志信息。

在上面的代碼中,我們並未使用new運算符來創建Book類的實例,但是卻可以得到Book類的實例,這就是Spring的強大之處,所有類的實例的創建都不需要應用程序自己創建,而是交給Spring容器來創建及管理。

3. Spring示例講解

雖說實例的創建交給Spring容器來創建及管理,但是在上述的代碼中,什么時候創建了Book類的實例並對字段賦值了呢?

為驗證這個疑問,我們修改下Book類。

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

再添加一個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);
    }
}

然后修改下spring-config.xml文件。

<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>

最后,我們修改下Main類的代碼來Debug下,看下代碼的執行順序。

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

為更直觀的展示,請看如下的Gif圖。

從圖中,我們可以看出,在執行完 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");后,控制台先輸出了以下內容:

This is Book constructor.

This is Book setBookName().

This is Book setAuthor().

This is Author constructor.

This is Author setName().

This is Author setAge().

也就是這句代碼執行完后,Book類和Author類的實例已經被創建並且字段已經被賦值,接下來的代碼只是從Spring容器中獲取實例而已。

4. 注意事項

獲取Bean時,第一個參數beanName要與spring-config.xml定義的bean id保持一致,比如我們在spring-config.xml中定義的是book,如果在獲取時寫的是Book,就會報錯。

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

報錯信息如下所示:

5. 源碼及參考

源碼地址:https://github.com/zwwhnly/spring-action.git,歡迎下載。

【Spring】IntelliJ IDEA搭建Spring環境

idea中Spring項目創建以及實現一個小的IoC案例


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM