启动方式一
方式一可以将启动类作为控制器,从而实现启动并访问。
/**
* @author 小吉
* @description springboot2.0启动方式一
* @date 2020/5/19
*/
@RestController
@EnableAutoConfiguration
public class HelloController {
@RequestMapping("hello")
public String hello(){
return "springboot启动方式一";
}
public static void main(String[] args) {
SpringApplication.run(HelloController.class,args);
}
}
启动方式二
方式二需要在启动类中加上**@ComponentScan**注解,注解可以配置扫描的基础包,用于指定从哪个包往下扫描组件。
/**
* @author 小吉
* @description springboot2.0启动方式二
* @date 2020/5/19
*/
@RestController
public class HelloController2 {
@RequestMapping("hello")
public String hello(){
return "springboot启动方式二";
}
}
/**
* @author 小吉
* @description springboot启动类
* @date 2020/5/19
*/
@EnableAutoConfiguration
@ComponentScan(basePackages = "com.springbootdemo2.method2")
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class,args);
}
}
启动方式三
方式三只需要在启动类中加上类注解**@SpringBootApplication**即可。
/**
* @author 小吉
* @description springboot2.0启动方式三
* @date 2020/5/19
*/
@RestController
public class HelloController3 {
@RequestMapping("hello")
public String hello() {
return "springboot启动方式三";
}
}
/**
* @author 小吉
* @description springboot启动类
* @date 2020/5/19
*/
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class,args);
}
}
这三种方式都能启动springboot应用。使用方式二指定基础包扫描,可提高springboot应用的性能,节省内存空间,因为可以只扫描需要用到的组件。方式三也是常用的启动方式,代码编写更加简洁。
关于springboot启动方式就说到这里,下一节继续讲述springboot2.0 yml使用,感谢小伙伴们的围观。