1. 程式人生 > >SpringBoot三種啟動方式

SpringBoot三種啟動方式

ont figure mil frame 不用 裝配 dex cati oid

SpringBoot第一種啟動方式

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

@RestController
//@RestController 修飾的類下所有方法,全部都是返回json格式,這樣就不用再方法上加@ResponseBody
@EnableAutoConfiguration //@EnableAutoConfiguration 開啟自動配置 裝配到spring容器中 public class IndexController { @RequestMapping("/demo") public String index(){ return "hello word"; } public static void main(String[] args) { //程序入口,啟動類 SpringApplication.run(IndexController.class
,args); } }

SpringBoot第二種啟動方式

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

@ComponentScan("com.example.demo.controller")//掃描包
@EnableAutoConfiguration
public class App { public static void main(String[] args) { SpringApplication.run(App.class,args); } }

SpringBoot第三種啟動方式

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

@SpringBootApplication//掃包範圍當前包下的所有子包
//@SpringBootApplication 等同於@ComponentScan("com.example.demo.controller")//掃描包 和@EnableAutoConfiguration
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

SpringBoot三種啟動方式