SpringBoot的三种启动方式

引言

讲道理,启动方式的变化实际上是对springboot注解的理解和运用,首先搭建好项目基本的结构,就来个helloworld吧,如图

目录结构

方式一
说明:

只需要建立个Controller层,这里编写一个Hello类,在类中编写main 入口函数(必写),
@EnableAutoConfiguration 至关重要(必写),旨在加载springboot默认配置
选中main 方法 run 即可访问页面

package com.ly.springboothelloworld.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableAutoConfiguration
public class Hello {

    @GetMapping("/index")
    public String hello(){
        return "hello,这是我的第一个SpringBoot案例11";
    }

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

方式一 是 启动项 放在hello类中

方式二
说明:

建立一个Controller层的hello类,再单独建立个启动的类SpringbootHelloworldApplication
@EnableAutoConfiguration 必写
@ComponentScan(basePackages = "com.ly.springboothelloworld.controller") 必写
即配合上包扫描
SpringbootHelloworldApplication 编写

package com.ly.springboothelloworld;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;


@ComponentScan(basePackages = "com.ly.springboothelloworld.controller")
@EnableAutoConfiguration
public class SpringbootHellowolrdApplication {

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

hello类编写

package com.ly.springboothelloworld.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Hello {
    @GetMapping("/index")
    public String hello(){
        return "hello,这是我的第一个SpringBoot案例11";
    }
}
方式三
说明:

只需配置启动类加上@@SpringBootApplication 注解
表现层注意写在同包或者子包下
解释:
这里主要关注@SpringBootApplication注解,它包括三个注解:
@Configuration:表示将该类作用springboot配置文件类。
@EnableAutoConfiguration:表示程序启动时,自动加载springboot默认的配置。
@ComponentScan:表示程序启动是,自动扫描当前包及子包下所有类。
启动类编写:

package com.ly.springboothelloworld;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootHellowordApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootHellowordApplication.class, args);
    }
}

控制层编写:

@RestController
public class Hello {
    @GetMapping("/index")
    public String hello(){
        return "hello,这是我的第一个SpringBoot案例11";
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,477评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,077评论 6 342
  • 最近再研究springboot的原理😋颇有收获,现在让我分享一下springboot如何使用吧~ 想要解锁更多新姿...
    tengshe789阅读 11,201评论 4 94
  • springboot 概述 SpringBoot能够快速开发,简化部署,适用于微服务 参考嘟嘟大神SpringBo...
    一纸砚白阅读 10,851评论 2 20
  • 这一天诗歌和睡眠相遇诞生了世界诗歌日和世界睡眠日 因为两个毫不相干的词在这一天我的诗歌更加犯困我的睡眠也毫无规律 ...
    失业猎手阅读 1,157评论 2 12