二、SpringBoot2.0启动方式

启动方式一

方式一可以将启动类作为控制器,从而实现启动并访问。

/**
 * @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使用,感谢小伙伴们的围观。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值