写在前面
希望你们看了能够有所收获,同时觉得不错的朋友可以点赞和关注下我,以后还会有更多精选文章分享给大家!大家可以关注一下java提升专栏java提升zhuanlan.zhihu.com
![fa39a8a4b0e7f1dff333a1b4266f0ee6.png](https://i-blog.csdnimg.cn/blog_migrate/a06fa1ebb7b1ae3bf843e7ffeb4f8848.jpeg)
本篇博客作为Spring入门系列的第一篇博客,不会讲解什么是Spring以及Spring的发展史这些太理论的东西,主要讲解下如何使用IntelliJ IDEA创建Spring项目以及通过一个示例了解下Spring的简单使用。
1. 创建Spring项目
首先,按照下图所示打开“新建项目”弹出框:
![93d1e970205917cb388bee3f42a88db3.png](https://i-blog.csdnimg.cn/blog_migrate/3dec20ccb7ba51e9e4b0a074161c15e5.jpeg)
然后在左侧选择项目类型Spring:
![33c09f7c52eccf61e936b4d5228961ee.png](https://i-blog.csdnimg.cn/blog_migrate/93c2046654e327408ace045d5c647528.jpeg)
如果这里忘记了选择"Create empty spring-config.xml",也可以新建完项目再新建配置文件。
![3ba7e2a57e0b7ea5ae7be805970d1bcc.png](https://i-blog.csdnimg.cn/blog_migrate/ace02d5126cbfdc8c0454b8c3439af64.jpeg)
![bf37f2a78fe06244e2933fb797f5cba6.png](https://i-blog.csdnimg.cn/blog_migrate/fd6a0de47bcb89eb7409ece3731e5a12.jpeg)
接着,确定好项目名称和保存路径,然后点击"Finish"按钮:
![dfb023f68df253d0831b959ecbe1b0fc.png](https://i-blog.csdnimg.cn/blog_migrate/cc7c8589521479ab90c08c83c8df059f.jpeg)
因为需要下载Spring依赖的包,因此需要加载一会。
![d6590fea75e8da66ca16d0cd0920ab66.png](https://i-blog.csdnimg.cn/blog_migrate/ef6173861957749308a78800b345dee3.jpeg)
新建完的项目结构图如下所示:
![e7f076b49055b362b4432bb04d3a44a6.png](https://i-blog.csdnimg.cn/blog_migrate/7a377c8530de97937e31d62eae4e7233.jpeg)
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);
}
}
如果我们想要输出图书信息,按照传统的方式,需要以下几步:
- 创建Book类的实例对象
- 设置实例对象的bookName字段和author字段
- 调用实例对象的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();
}
}
运行结果:
![ce7c9880dd891889921dbd84001f6302.png](https://i-blog.csdnimg.cn/blog_migrate/af3b8129d9580eebcb2c298f21bc0fb6.jpeg)
我们会发现,运行结果和传统方式一样,只是多了一些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();
}
}
为更直观的展示,
![cd76fe3c25f986d71856340ecbb6fe0f.png](https://i-blog.csdnimg.cn/blog_migrate/91c484f4872937c42a7c534e1a13ac52.jpeg)
从图中,我们可以看出,在执行完 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();
}
}
报错信息如下所示:
![b042f7b7729bec7049e1fd3ee5eac5fd.png](https://i-blog.csdnimg.cn/blog_migrate/d1d0c484415c937d9265359d47fbf64f.jpeg)
作者:申城异乡人
链接: https:// juejin.im/post/5d5261e8 f265da03d316b684