Spring Boot学习之----idea基础配置

一. Spring Boot入门

1、 Spring Boot简介

简化Spring应用开发的一个框架

整个Spring技术栈的一个大整合

J2EE开发的一站式解决方案

Spring Boot用来简化Spring应用开发,约定大于配置

​ Spring全家桶时代

​ Spring Boot --> J2EE 一站式解决方案

​ Spring Cloud-->分布式整体解决方案

一、优点:

* 快速创建独立运行的Spring项目以及与主流框架集成
* 使用嵌入式的Servlet容器,应用无需打成WAR包
* starters(启动器)自动依赖与版本控制
* 大量的自动配置,简化开发,也可修改默认值
* 无需配置XML,无代码生成,开箱即用
* 准生产环境的运行时应用监控
* 与云计算的天然集成

二、缺点:

​ 入门容易,精通难

2、Spring Boot HelloWorld

一、创建一个 maven 工程

二、导入Spring Boot相关的依赖

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.9.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
</dependencies>

三、编写启动类

@SpringBootApplication 来标注一个主程序类,说明这是一个SpringBoot应用

psvm main方法的快捷键

@SpringBootApplication
public class Application {
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}

四、编写相关的Controller,Service

@Controller
public class HelloController {
  @ResponseBody
	@RequestMapping("/hello")
	public String hello() {
		return "Hello word!";
	}
}

五、运行测试

六、部署

导入Maven插件

<!--可以将应用打包成一个可执行的jar包-->
<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
</build>

打包 :MavenProjects--->Lifecycle--->package

运行:java -jar xxxx.jar

3、HelloWorld探究

1、POM文件

1、父项目

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>2.2.0.RELEASE</version>
</parent>
它的父项目是
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.2.0.RELEASE</version>
    <relativePath>../../spring-boot-dependencies</relativePath>
  </parent>
它来真正管理Spring Boot应用里面的所有依赖版本

Spring Boot的版本仲裁中心;

以后我们导入依赖默认是不需要写版本

2、启动器

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

spring-boot-starter-web:

​ spring-boot-stater:spring-boot场景启动器;帮我们导入了web模块正常运行所依赖的组件

Spring Boot将所有的功能场景都抽取出来,做成一个个的state(启动器),只需要在这些项目里面引入这些stater相关场景的所有依赖就会导入进来。要用什么功能就导入什么场景的启动器

2、@SpringBootApplication

主程序类,主入口类

@SpringBootApplication: Spring Boot应用标注在某个类上说明这个类是SpringBoot的主配置类,SpringBoot就应该运行这个类的main方法来启动SpringBoot应用;

@SpringBootApplication
public class SpringBootHelloWorldApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootHelloWorldApplication.class,args);
    }
}

@SpringBootApplication ------>

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {
      @Filter(type = FilterType.CUSTOM,classes = {TypeExcludeFilter.class}),
      @Filter(type = FilterType.CUSTOM,classes = {AutoConfigurationExcludeFilter.class})})
@ConfigurationPropertiesScan
public @interface SpringBootApplication {

@SpringBootConfiguration

@SpringBootConfiguration:Spiring Boot 的配置类 标注在某个类上,表示这是一个Spring Boot的配置类

@SpringBootConfiguration ------>

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
@Configuration

Configuration: 配置类上来标注这个注解 配置类-------配置文件;配置类也是容器中的一个组件;@Component

@EnableAutoConfiguration

@EnableAutoConfiguration:开启自动配置功能;以前我们需要配置的东西,Spring Boot 帮我们自动配置

@EnableAutoConfiguration ----->

@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
@AutoConfigurationPackage

AutoConfigurationPackage:自动配置包,将主配置类的所在包及下面所有包里面的所有组件扫描到Spring容器中

@Import({Registrar.class})

Spring的底层注解,给容器中导入一个组件;导入的组件由 Registrar.class 来指定

@Import({AutoConfigurationImportSelector.class})

给容器中导入组件?

​ AutoConfigurationImportSelector.class:导入那些组件的选择器

​ 会给容器中导入非常多的自动配置类(xxxAutoConfiguration),就是给容器中导入这个场景需要的所有组件,并配置这些组件

![image-20201029211625848](D:\笔记\spring-boot\Spring Boot\Spring Boot笔记\images\image-20201029211625848.png)

有了自动配置类,免去了我们手动配置注入功能组件等的工作

4、使用Spring Initializer快速创建Spring Boot项目

默认生成的Spring Boot项目

  • 主程序已经生成好了,我们只需要写我们自己的逻辑
  • resources文件夹中目录结构
    • static:保存所有的静态资源;
    • templates:保存所有的模板页面;(Spring Boot默认jar包使用嵌入式的Tomcat,默认不支持JSP页面),可以使用模板引擎(freemarker、thymeleaf);
    • application.properties: Spring Boot应用的配置文件;可以修改一些默认设置

二、Spring Boot配置文件

1、配置文件

SpringBoot使用一个全局的配置文件,配置文件名是固定的;

  • application.properties
  • application.yaml

配置文件的作用:修改SpringBoot自动装配的默认值;SpringBoot在底层给我们自动配置好

2、YAML语法:

https://www.runoob.com/w3cnote/yaml-intro.html

3、配置文件注入

@ConfigurationProperties(prefix = "person")

@ConfigurationProperties: 告诉SpringBoot将本类中所有属性和配置文件中相关的配置进行绑定;prefix = "person":配置文件中哪个下面的所有属性进行一 一映射,只有这个组件是容器中的组件,@Component才能使用容器中提供的功能

导入配置文件处理器,以后编写配置就有提示

https://www.youtube.com/watch?v=RBXSr9DJCAk&list=PLmOn9nNkQxJEFsK2HVO9-WA55Z7LZ2N0S&index=12

posted @ 2020-11-02 12:53  WANGXIN_YU  阅读(314)  评论(0)    收藏  举报