码迷,mamicode.com
首页 > 其他好文 > 详细

注解与main方法

时间:2021-06-04 19:47:18      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:spec   delegate   tap   支持   get   starting   功能   add   als   

EnableAutoConfiguration

This class-level annotation tells Spring Boot to “guess” how you want to configure Spring, based on the jar dependencies that you have added. Since spring-boot-starter-web added Tomcat and Spring MVC, the auto-configuration assumes that you are developing a web application and sets up Spring accordingly.

  • 功能:开启自动配置
  • 修饰范围:只能用于类
  • 实际作用:根据pom.xml文件中的依赖(如spring-boot-starter-web),来构建相关环境(Tomcat—web容器和Spring MVC

ComponentScan

  • 作用:开启注解扫描
  • 修饰范围:只能用于类
  • 扫描范围:当前包及其子包

RestController

This is known as a stereotype annotation. It provides hints for people reading the code and for Spring that the class plays a specific role. In this case, our class is a web @Controller, so Spring considers it when handling incoming web requests.

  • 作用:用来实例化当前对象为一个控制器对象,并将类上所有方法的返回值转为json,响应给浏览器
    • 相当于@Controller实例化当前类为一个控制器)与@ResponseBody(将当前方法返回值转换为json,响应给浏览器)的组合,支持RESTful访问方 式,返回结果都是json字符串。区别见[Controller与RestController区别](# Controller与RestController区别)
  • 修饰范围:只能用于类

RequestMapping

The @RequestMapping annotation provides “routing” information. It tells Spring that any HTTP request with the /chenzf path should be mapped to the hello method. The @RestController annotation tells Spring to render the resulting string directly back to the caller.

  • 作用:用来加入访问路径
  • 修饰范围:(加入命名空间)与方法(指定具体路径)
  • @GetMapping限定请求方式只能是Get,并指定路径,可以被RequestMapping替代,一般只用于修饰方法
  • @PostMapping@PutMapping@DeleteMapping类似,也用于限定请求方式

SpringBootApplication

@SpringBootApplication 注解等价于

  • @SpringBootConfiguration:标识注解,标识这是一个Spring Boot的配置类
  • @EnableAutoConfiguration :自动与项目中集成的第三方技术进行集成
  • @ComponentScan:扫描入口类所在子包以及子包后代包中注解

main方法

SpringApplication.run(Application.class, args);

This is a standard method that follows the Java convention for an application entry point. Our main method delegates to Spring Boot’s SpringApplication class by calling run. SpringApplication bootstraps our application, starting Spring, which, in turn, starts the auto-configured Tomcat web server. We need to pass Application.class as an argument to the run method to tell SpringApplication which is the primary Spring component. The args array is also passed through to expose any command-line arguments.

  • 通过标准Java入口方式,委托给SpringApplication,并告知当前Spring Boot主应用类是谁,从而启动Spring Boot中Tomcat容器
  • args可以在启动时指定外部参数,来覆盖Spring Boot中一些默认配置
    • 不修改配置文件,在运行时将端口号临时改为9999:
    技术图片

注解与main方法

标签:spec   delegate   tap   支持   get   starting   功能   add   als   

原文地址:https://www.cnblogs.com/chenzufeng/p/14850001.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!