热门搜索 :
考研考公
您的当前位置:首页正文

Spring Boot HelloWorld 解析

来源:东饰资讯网

开发环境

JDK:1.8

配置IDEA及新建项目

  1. 打开IDEA


    idea初始界面
  2. 配置MEAVEN
    点击右下方configure--Settings-->搜索maven maven将maven_home配置为安装目录,并重新指定maven配置文件地址,如下图


    idea-maven配置
  3. 创建项目
    利用Spring Initializr创建一个Spring Boot项目,注意SDK,选用1.8,如果不是新建即可,剩下的按照图下一步即可。


    idea-sdk.png
    springboot-helloworld.png

项目分析

项目新建完成之后会出现如下包结构:


springboot-helloworld-package.png

整个包结构是一个标准的maven工程结构,其中HelloWorldApplication是项目的主要启动类,我们可以看一下pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.itbofeng</groupId>
    <artifactId>helloworld</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>HelloWorld</name>
    <description>HelloWorld project for Spring Boot</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

我们看一下spring-boot-starter都有哪些依赖

spring-boot-starter依赖图
spring-boot-starter依赖javax.annotation-api,这说明SpringBoot是一个注解驱动的应用程序,SpringBoot的目的也包括去除xml配置,snakeyaml是yaml配置文件的解析包,说明springboot也支持yaml格式的配置文件,spring-boot-starter-logging是日志包,最重要的是spring-boot-autoconfigure是springboot的自动配置包,在此我们只需知道这个包帮我们做了很多的默认配置即可,我们在以后的文章中在详细分析这个包,后面是spring-boot他又依赖spring-context、aop、beans等功能,关于pom.xml我们就先简单了解到这里,下面我们看一下SpringBoot 的主程序类HelloWorldApplication:
package com.itbofeng.helloworld;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloWorldApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloWorldApplication.class, args);
    }
}

其有个@SpringBootApplication注解,点击进去看一下其究竟

package org.springframework.boot.autoconfigure;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.context.TypeExcludeFilter;
import org.springframework.context.annotation.Bean;
import 
import 
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.core.annotation.AliasFor;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
        @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
        @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
    //...
}

通过源码我们知道@SpringBootApplication注解是一个组合注解包括@SpringBootConfiguration和@EnableAutoConfiguration以及@ComponentScan注解,我们在进入@SpringBootConfiguration注解看一下源码发现其集成子@Configuration注解,说明其是一个配置类,@EnableAutoConfiguration和@ComponentScan注解,根据字面意思我们也能猜出@EnableAutoConfiguration注解的作用就是开启自动配置即可,@ComponentScan注解作用是包扫描注解,关于这两个注解的详细作用以及原理,我们在后面的文章进行详细学习(一口吃不成个大胖子,饭要一口一口吃,路要一步一步走,学习也一样要循序渐进),回过头我们再看一下main方法其通过SpringApplication.run(HelloWorldApplication.class, args);方法把当前类以及命令行参数传给了SpringBoot,至此SpringBoot的HelloWorld的主要原理已经有个初步了解。

我们总结一下:Spring Boot 是通过@SpringBootApplication注解告诉SpringBoot该类是一个SpringBoot的配置类,并且该类所在的包是根包(basePackages),并且开启自动配置,即会配置一些基础功能例如IOC(DI),如果依赖web模块,也会默认配置好springmvc。
以为完了吗?都没有看到效果,怎么可能完了呢
我们编写一个HelloController如下:

package com.itbofeng.helloworld.controller;
​
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
​
@Controller//标准这是一个controller
public class HelloController {
​
 @ResponseBody//标注将返回值  相当于response.getWriter().write("...");
 @RequestMapping("/helloworld")//标注方法处理/helloworld的请求
 public String helloworld(){
     return "Hello world Spring Boot!";
 }
}

如果有学习过SpringMVC的同学一定熟悉这里的注解,其实这里的注解是依赖于spring-boot-starter-web包,如果没有springmvc学习过的童鞋,关于这里的注解我们后面再详细说明,这里只需要知道该注解的作用即可,参考注释,最后,启动应用程序查看结果


HelloWorld

至此,SpringBoot HelloWorld也结束了,由于SpringBoot是注解驱动开发,我们下一小节,简单学习一下注解相关的知识,以及一些基本注解的讲解。

Top