美文网首页Spring Coud
SpringBoot的三种启动方式

SpringBoot的三种启动方式

作者: 上进的小二狗 | 来源:发表于2018-08-10 15:07 被阅读0次

    引言

    讲道理,启动方式的变化实际上是对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";
        }
    }
    

    相关文章

      网友评论

        本文标题:SpringBoot的三种启动方式

        本文链接:https://www.haomeiwen.com/subject/xjgkbftx.html