走进springboot

核心思想---自动装配---约定大于配置

开发环境:jdk1.8、maven、springboot、idea

一、快速构建一个springboot项目 1.1、进入springboot官网

走进springboot

1.2、选择配置并下载

走进springboot

1.3、项目的导入

走进springboot

二、自动装配原理

pom.xml

<?xml version="1.0" encoding="UTF-8"?> <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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.2</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!-- 启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.4.2</version> </dependency> <!-- jsr303校验 后台校验数据的格式,配合@Validated //数据校验 ,使用!--> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>2.0.1.Final</version> </dependency> <!-- 自动导入web使用的所有依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- springboot使用yml注入数据必备! --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> </dependencies> <!-- 打包插件 --> <build> <plugins> <!-- <plugin>--> <!-- <groupId>org.springframework.boot</groupId>--> <!-- <artifactId>spring-boot-maven-plugin</artifactId>--> <!-- </plugin>--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> <configuration> <skipTests>true</skipTests> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.2.6.RELEASE</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project> 2.1、怎么进行自动装配?

spring boot的自动装配:

1、spring boot启动时会加载大量的自动配置类

2、在开发中看需要实现的功能是否存在springboot写好的配置类,如果没有就需要手动配置

3、在yml中修改springboot属性值时(也就是配置),当它提示时,也就相当于你在修改springboot配置好的配置类中的属性

4、springboot配置好的配置类可以在spring.factories中进行查找出来,它会有一个xxxAutoConfiguration(自动配置类:给容器中添加组件),它上面的xxxProperties配置的类属性就是yml中可以进行修改的属性,以此来达到想要配置的功能!!!

结论:主启动器@SpringBootApplication通过扫描依赖中导入的Spring-boot-autoconfigure下的jar包下的META-INF下的spring.factories中的配置进行装配,但是不一定生效,如果没有相应的启动器,就不会生效,需要导入相应的启动器才能自动装配,springboot的自动装配的东西(以前需要自己写的包或者配置文件xml。。。)现在只需要在sprint-boot-autoconfigure-xxx.xx.RELEASE.jar包下都存在,不需要自己配置,只需要调用即可。。

三、yml写法

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wsxpxx.html