本篇博客做爲Spring入門系列的第一篇博客,不會講解什麼是Spring以及Spring的發展史這些太理論的東西,主要講解下如何使用IntelliJ IDEA建立第一個Spring項目以及經過一個示例講解下Spring的簡單原理。java
IDE:IntelliJ IDEAgit
若是這裏忘記了選擇"Create empty spring-config.xml",也能夠新建完項目再新建配置文件github
由於須要下載Spring依賴的包,所以須要加載一會spring
新建完的項目結構圖以下:app
新建一個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);
}
}
複製代碼
若是咱們想要輸出圖書信息,按照傳統的方式,須要如下幾步:測試
public class Main {
public static void main(String[] args) {
Book book = new Book();
book.setBookName("平凡的世界");
book.setAuthor("路遙");
book.printBookInfo();
}
}
複製代碼
運行結果:this
Book Name:平凡的世界,Author:路遙idea
那麼在Spring項目中,如何實現一樣的調用呢?spa
首先,修改spring-config.xml,添加以下配置:
<bean id="book" class="Book">
<property name="bookName" value="平凡的世界"></property>
<property name="author" value="路遙"></property>
</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容器來建立及管理。
雖然說實例的建立交給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>
<property name="author" value="路遙"></property>
</bean>
<bean id="author" class="Author">
<property name="name" value="路遙"></property>
<property name="age" value="60"></property>
</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容器中獲取實例而已。
獲取Bean時,第一個參數(bean name)要與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();
}
}
複製代碼
報錯信息以下:
【Spring】IntelliJ IDEA搭建Spring環境
源碼地址:github.com/zwwhnly/spr…,歡迎下載。