Spring入门(一):建立Spring项目

本篇博客做为Spring入门系列的第一篇博客,不会讲解什么是Spring以及Spring的发展史这些太理论的东西,主要讲解下如何使用IntelliJ IDEA建立第一个Spring项目以及经过一个示例讲解下Spring的简单原理。java

1.建立Spring项目

IDE:IntelliJ IDEAgit

1.1新建项目

1.2选择项目类型

若是这里忘记了选择"Create empty spring-config.xml",也能够新建完项目再新建配置文件github

1.3肯定项目名称及保存路径

由于须要下载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);
    }
}

若是咱们想要输出图书信息,按照传统的方式,须要如下几步:测试

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

运行结果:this

Book Name:平凡的世界,Author:路遥idea

那么在Spring项目中,如何实现一样的调用呢?.net

首先,修改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容器来建立及管理。

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>
    <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容器中获取实例而已。

4.注意事项

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

报错信息以下:

5.参考连接

【Spring】IntelliJ IDEA搭建Spring环境
idea中Spring项目建立以及实现一个小的IoC案例

6.源码

源码地址:https://github.com/zwwhnly/spring-action.git,欢迎下载。

相关文章
相关标签/搜索