在controller类中启动 有局限性

package org.djd.web.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/user")
@EnableAutoConfiguration
public class HelloController {
@RequestMapping("/hello")
public @ResponseBody String hello(){
return "helloword";
}

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

单独创建启动文件 配置注解扫描

package org.djd.app;

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


//配置扫描的包 加载多个控制器
@ComponentScan(basePackages = "com.djd.web.controller")
//其他控制器 不需要再添加该注解
@EnableAutoConfiguration

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