一.前面学习了springboot的介绍以及springboot的作用、以及创建springboot的项目,今天学习了springboot的注解。这里先学的是@RestController在上加上RestController 表示修饰该Controller所有的方法返回JSON格式,直接可以编写Restful接口@EnableAutoConfiguration

注解:作用在于让 Spring Boot   根据应用所声明的依赖来对 Spring 框架进行自动配置
这个注解告诉Spring Boot根据添加的jar依赖猜测你想如何配置Spring。由于spring-boot-starter-web添加了Tomcat和Spring MVC,所以auto-configuration将假定你正在开发一个web应用并相应地对Spring进行设置。

SpringBoot启动方式和传统的启动方式的比较。

这是springboot项目的介绍以及注解的解释和传统项目的对比

springboot学习笔记二

传统的启动spring项目。

 

springboot学习笔记二

springboot有三种启动方式:

SpringBoot启动方式1

Springboot默认端口号为8080

 

@RestController

@EnableAutoConfiguration

public class HelloController {

      @RequestMapping("/hello")

      public String index() {

           return "Hello World";

      }    

public static void main(String[] args) {

           SpringApplication.run(HelloController.class, args);

      }

}

启动主程序,打开浏览器访问http://localhost:8080/index,可以看到页面输出Hello World

SpringBoot启动方式2

@ComponentScan(basePackages = "com.itmayiedu.controller")---控制器扫包范围

@ComponentScan(basePackages = "com.itmayiedu.controller")

@EnableAutoConfiguration

public class App {

      public static void main(String[] args) {

           SpringApplication.run(App.class, args);

      }

}

SpringBoot启动方式3

@SpringBootApplication

@SpringBootApplication 被 @Configuration、@EnableAutoConfiguration、@ComponentScan 注解所修饰,换言之 Springboot 提供了统一的注解来替代以上三个注解

扫包范围:在启动类上加上@SpringBootApplication注解,当前包下或者子包下所有的类都可以扫到。

@ComponentScan注解

springboot学习笔记二

@springBootApplication

springboot学习笔记二

springboot学习笔记二

springbootapplication的缺点:性能不好,希望可以换成别的。

相关文章:

  • 2022-12-23
  • 2021-10-27
  • 2021-10-02
  • 2021-07-10
  • 2022-12-23
  • 2022-01-21
  • 2021-12-24
猜你喜欢
  • 2021-04-26
  • 2021-06-09
  • 2021-09-10
  • 2021-07-08
相关资源
相似解决方案