Spring Boot HelloWorld 解析

开发环境

开发工具:IDEA 2017.2.2 链接: https://pan.baidu.com/s/1qxZLZtBR-Xr7JVkgmhZWzw 密码: wsqp

激活教程:http://idea.lanyus.com/

Spring Boot:2.0.3.RELEASE 参考文档:https://docs.spring.io/spring-boot/docs/2.0.3.RELEASE/reference/htmlsingle/

Maven:apache-maven-3.5.0 链接:https://pan.baidu.com/s/14HfTeuBYlZJw3cGkONISFg 密码:vun7

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" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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 org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
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是注解驱动开发,我们下一小节,简单学习一下注解相关的知识,以及一些基本注解的讲解。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,117评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,963评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,897评论 0 240
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,805评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,208评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,535评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,797评论 2 311
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,493评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,215评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,477评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,988评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,325评论 2 252
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,971评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,807评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,544评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,455评论 2 266

推荐阅读更多精彩内容