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

本篇博客做爲Spring入門系列的第一篇博客,不會講解什麼是Spring以及Spring的發展史這些太理論的東西,主要講解下如何使用IntelliJ IDEA建立Spring項目以及經過一個示例瞭解下Spring的簡單使用。java

1. 建立Spring項目

首先,按照下圖所示打開「新建項目」彈出框:git

而後在左側選擇項目類型Spring:程序員

若是這裏忘記了選擇"Create empty spring-config.xml",也能夠新建完項目再新建配置文件。github

接着,肯定好項目名稱和保存路徑,而後點擊"Finish"按鈕:spring

由於須要下載Spring依賴的包,所以須要加載一會。微信

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

2. Spring示例

新建一個Book類,定義兩個字段bookName,author和一個實例方法printBookInfo()ide

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

若是咱們想要輸出圖書信息,按照傳統的方式,須要如下幾步:this

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

運行結果:idea

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

原創不易,若是以爲文章能學到東西的話,歡迎點個贊、評個論、關個注,這是我堅持寫做的最大動力。

若是有興趣,歡迎添加個人微信:zwwhnly,等你來聊技術、職場、工做等話題(PS:我是一名奮鬥在上海的程序員)。

相關文章
相關標籤/搜索