1 ========pom.xml========== 2 3 <parent> 4 <groupId>org.springframework.boot</groupId> 5 <artifactId>spring-boot-starter-parent</artifactId> 6 <version>2.3.4.RELEASE</version> 7 </parent> 8 9 10 <dependencies> 11 <dependency> 12 <groupId>org.springframework.boot</groupId> 13 <artifactId>spring-boot-starter-web</artifactId> 14 </dependency> 15 </dependencies>
1 /** 2 * 主程序类 3 * @SpringBootApplication告诉SpringBoot这是一个SpringBoot应用 4 */ 5 @SpringBootApplication 6 public class MainApplication { 7 public static void main(String[] args) { 8 SpringApplication.run(MainApplication.class, args); 9 } 10 }
1 @RestController 2 public class HelloController { 3 4 @RequestMapping("/hello") 5 public String handle01() { 6 return "Hello, Spring Boot 2!"; 7 } 8 }
直接运行main方法
1 =====application.properties===== 2 server.port=8888
1 <build> 2 <plugins> 3 <plugin> 4 <groupId>org.springframework.boot</groupId> 5 <artifactId>spring-boot-maven-plugin</artifactId> 6 </plugin> 7 </plugins> 8 </build>
把项目打成jar包,直接在目标服务器执行即可。
1 <parent> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-parent</artifactId> 4 <version>2.3.4.RELEASE</version> 5 </parent>
它的父项目
7 <parent>
8 <groupId>org.springframework.boot</groupId>
9 <artifactId>spring-boot-dependencies</artifactId>
10 <version>2.3.4.RELEASE</version>
11 </parent>
几乎声明了开发中常用的依赖版本号,自动版本仲裁机制
1 见到很多spring-boot-starter-* : *就表示某种场景。 2 只要引入starter,这个场景的所有常规需要的依赖我们都自动引入。 3 SpringBoot所有支持的场景 4 https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-starter 5 见到的 *-spring-boot-starter: 第三方为我们提供的简化开发的场景启动器。 6 所有场景启动器最底层的依赖。 7 8 <dependency> 9 <groupId>org.springframework.boot</groupId> 10 <artifactId>spring-boot-starter</artifactId> 11 <version>2.3.4.RELEASE</version> 12 <scope>compile</scope> 13 </dependency>
1 引入依赖默认都可以不写版本。 2 引入非版本仲裁的jar,要写版本号。
1 查看spring-boot-dependencies里面规定当前依赖的版本用的key 2 在当前项目里面重写配置 3 <properties> 4 <mysql.version>5.1.43</mysql.version> 5 </properties>
配置Tomcat
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-tomcat</artifactId> 4 <version>2.3.4.RELEASE</version> 5 <scope>compile</scope> 6 </dependency>
自动配好SpringMVC常用功能(组件)
代码清单,查看当前项目中容器里的组件
1 @SpringBootApplication 2 public class MainApplication { 3 public static void main(String[] args) { 4 //1.返回IOC容器 5 ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args); 6 //2.查看容器里的组件 7 String[] beanDefinitionNames = run.getBeanDefinitionNames(); 8 9 for (String beanDefinitionName : beanDefinitionNames) { 10 System.out.println(beanDefinitionName); 11 } 12 } 13 }
组件列表
1 org.springframework.context.annotation.internalConfigurationAnnotationProcessor 2 org.springframework.context.annotation.internalAutowiredAnnotationProcessor 3 org.springframework.context.annotation.internalCommonAnnotationProcessor 4 org.springframework.context.event.internalEventListenerProcessor 5 org.springframework.context.event.internalEventListenerFactory 6 mainApplication 7 org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory 8 helloController 9 org.springframework.boot.autoconfigure.AutoConfigurationPackages 10 org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration 11 propertySourcesPlaceholderConfigurer 12 org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration$TomcatWebSocketConfiguration 13 websocketServletWebServerCustomizer 14 org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration 15 ......
无需以前的包扫描配置。
1 @SpringBootApplication 2 等价于 3 @SpringBootConfiguration 4 @EnableAutoConfiguration 5 @ComponentScan("/**/*")
配置文件的值最终会绑定每个类上,这个类会在容器中创建对象。
SpringBoot所有的自动配置功能都在 spring-boot-autoconfigure 包里面。
原文:https://www.cnblogs.com/maxiangzhao/p/15056711.html