eclipse创建spring项目_Spring成长之路【一】:创建Spring项目

写在前面

希望你们看了能够有所收获,同时觉得不错的朋友可以点赞和关注下我,以后还会有更多精选文章分享给大家!大家可以关注一下java提升专栏
java提升​zhuanlan.zhihu.com
fa39a8a4b0e7f1dff333a1b4266f0ee6.png

本篇博客作为Spring入门系列的第一篇博客,不会讲解什么是Spring以及Spring的发展史这些太理论的东西,主要讲解下如何使用IntelliJ IDEA创建Spring项目以及通过一个示例了解下Spring的简单使用。
1. 创建Spring项目
首先,按照下图所示打开“新建项目”弹出框:

93d1e970205917cb388bee3f42a88db3.png


然后在左侧选择项目类型Spring:

33c09f7c52eccf61e936b4d5228961ee.png


如果这里忘记了选择"Create empty spring-config.xml",也可以新建完项目再新建配置文件。

3ba7e2a57e0b7ea5ae7be805970d1bcc.png

bf37f2a78fe06244e2933fb797f5cba6.png


接着,确定好项目名称和保存路径,然后点击"Finish"按钮:

dfb023f68df253d0831b959ecbe1b0fc.png


因为需要下载Spring依赖的包,因此需要加载一会。

d6590fea75e8da66ca16d0cd0920ab66.png


新建完的项目结构图如下所示:

e7f076b49055b362b4432bb04d3a44a6.png


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


运行结果:

ce7c9880dd891889921dbd84001f6302.png


我们会发现,运行结果和传统方式一样,只是多了一些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


从图中,我们可以看出,在执行完 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:// juejin.im/post/5d5261e8 f265da03d316b684
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值