Spring boot阅读记录

文章目录

      • maven 搭建spring boot 项目 最简单的pom文件
      • spring-boot-starter-parent
      • @RestController @RequestMapping @EnableAutoConfiguration
      • main方法
      • Auto-configuration

maven 搭建spring boot 项目 最简单的pom文件

spring boot 使用org.springframework.boot 作为groupID。
通常你的maven pom文件,继承 spring-boot-starter-parent,然后再依赖几个starter。
Spring boot 也提供了一个可选的maven插件 用来生成可执行jar。


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 	<modelVersion>4.0.0modelVersion>
 	<groupId>com.examplegroupId>
 	<artifactId>myprojectartifactId>
 	<version>0.0.1-SNAPSHOTversion>
 	
 	<parent>
 		<groupId>org.springframework.bootgroupId>
 		<artifactId>spring-boot-starter-parentartifactId>
 		<version>1.5.15.RELEASEversion>
	 parent>
 	
	 <dependencies>
		 <dependency>
			 <groupId>org.springframework.bootgroupId>
			 <artifactId>spring-boot-starter-webartifactId>
		 dependency>
	 dependencies>
 	
	 <build>
		 <plugins>
			 <plugin>
				 <groupId>org.springframework.bootgroupId>
				 <artifactId>spring-boot-maven-pluginartifactId>
			 plugin>
		 plugins>
	 build>
project>

spring-boot-starter-parent

不同的starter提供不同的依赖。
spring-boot-starter-parent是一个特殊的starter, 继承 spring-boot-dependencies 、提供默认的maven配置。 本身并不提供任何依赖。

@RestController @RequestMapping @EnableAutoConfiguration

  1. @RestController注解相当于@ResponseBody + @Controller合在一起的作用。RestController使用的效果是将方法返回的对象直接在浏览器上展示成json格式,而如果单单使用@Controller会报错,需要ResponseBody配合使用。
  2. @RequestMapping 说明Controller或者某个方法对应的请求路径
  3. @EnableAutoConfiguration 这个注解告诉spring boot,根据你依赖的jar包,猜测你想要如何配置spring。比如你依赖了spring-boot-starter-web,spring boot猜测你开发web应用,并据此帮你配置spring。:这里容易误解为自动配置是和starter配合使用的,其实并不是。你可以添加任何jar包,spring会尽最大努力帮你做好配置的。

main方法

入口类的main方法。这只是一个普通java main方法,作为程序的入口。在其中调用SpringApplication的run方法,SpringApplication将会部署我们的应用。启动spring,spring会启动自动配置的Tomcat。传入Example.class 作为参数,用来告诉StringApplication 主要的spring组件是什么。args 数组可以传入命令行参数。
pluginManagement标签??传送门
因为在spring-boot-starter-parent中添加了spring-boot-maven-plugin

If you don’t want to use @SpringBootApplication, the @EnableAutoConfiguration and
@ComponentScan annotations that it imports defines that behaviour so you can also use that
instead.

Auto-configuration

spring boot 尝试根据你所依赖的jar包,来自动的配置你的项目

你可能感兴趣的:(spring,spring,boot)