第一个SpringBoot 程序
(约定大于配置)
置文件 apploction.properties
#更改端口号
server.port=8081
原理初探
自动配置:
po.xml
-
spring-boot-dependencies : 核心依赖再父工程中
-
我们在写或者引入 Spirngnoot依赖的时候,不需要指定版本,就是因为有这些仓库
启动器
-
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency>
-
启动器就是Spirngboot的启动场景
-
比如spring-boot-starter-web ,就会帮我们自动导入 web环境的所有依赖
-
springboot 会把所有的功能场景,都变成一个个的启动器
-
我们要使用什么功能,就只需要找到对应的启动器
名称 | 描述 | Pom |
---|---|---|
spring-boot-starter | 核心入门工具,包括自动配置支持,日志记录和YAML | Pom |
spring-boot-starter-activemq | 使用Apache ActiveMQ的JMS消息传递入门 | Pom |
spring-boot-starter-amqp | 使用Spring AMQP和Rabbit MQ的入门 | Pom |
spring-boot-starter-aop | 使用Spring AOP和AspectJ进行面向方面编程的入门 | Pom |
spring-boot-starter-artemis | 使用Apache Artemis的JMS消息传递入门 | Pom |
spring-boot-starter-batch | 使用Spring Batch的入门 | Pom |
spring-boot-starter-cache | 开始使用Spring Framework的缓存支持 | Pom |
spring-boot-starter-cloud-connectors | 使用Spring Cloud Connectors的入门程序,可简化与Cloud Foundry和Heroku等云平台中服务的连接。不推荐使用Java CFEnv | Pom |
spring-boot-starter-data-cassandra | 使用Cassandra分布式数据库和Spring Data Cassandra的入门 | Pom |
spring-boot-starter-data-cassandra-reactive | 使用Cassandra分布式数据库和Spring Data Cassandra Reactive的入门 | Pom |
spring-boot-starter-data-couchbase | 使用Couchbase面向文档的数据库和Spring Data Couchbase的入门 | Pom |
spring-boot-starter-data-couchbase-reactive | 使用Couchbase面向文档的数据库和Spring Data Couchbase Reactive的入门 | Pom |
spring-boot-starter-data-elasticsearch | 使用Elasticsearch搜索和分析引擎以及Spring Data Elasticsearch的入门者 | Pom |
spring-boot-starter-data-jdbc | 使用Spring Data JDBC的入门 | Pom |
spring-boot-starter-data-jpa | 将Spring Data JPA与Hibernate结合使用的入门 | Pom |
spring-boot-starter-data-ldap | 使用Spring Data LDAP的入门 | Pom |
spring-boot-starter-data-mongodb | 使用MongoDB面向文档的数据库和Spring Data MongoDB的入门 | Pom |
spring-boot-starter-data-mongodb-reactive | 使用MongoDB面向文档的数据库和Spring Data MongoDB Reactive的入门 | Pom |
spring-boot-starter-data-neo4j | 使用Neo4j图形数据库和Spring Data Neo4j的入门 | Pom |
spring-boot-starter-data-redis | 使用Redis键值数据存储与Spring Data Redis和Lettuce客户端的入门 | Pom |
spring-boot-starter-data-redis-reactive | 将Redis键值数据存储与Spring Data Redis Reacting和Lettuce客户端一起使用的入门 | Pom |
spring-boot-starter-data-rest | 使用Spring Data REST通过REST公开Spring数据存储库的入门 | Pom |
spring-boot-starter-data-solr | 将Apache Solr搜索平台与Spring Data Solr结合使用的入门 | Pom |
spring-boot-starter-freemarker | 使用FreeMarker视图构建MVC Web应用程序的入门 | Pom |
spring-boot-starter-groovy-templates | 使用Groovy模板视图构建MVC Web应用程序的入门 | Pom |
spring-boot-starter-hateoas | 使用Spring MVC和Spring HATEOAS构建基于超媒体的RESTful Web应用程序的入门者 | Pom |
spring-boot-starter-integration | 使用Spring Integration的入门 | Pom |
spring-boot-starter-jdbc | 结合使用JDBC和HikariCP连接池的入门 | Pom |
spring-boot-starter-jersey | 使用JAX-RS和Jersey构建RESTful Web应用程序的入门。的替代品spring-boot-starter-web | Pom |
spring-boot-starter-jooq | 使用jOOQ访问SQL数据库的入门。替代spring-boot-starter-data-jpa 或spring-boot-starter-jdbc | Pom |
spring-boot-starter-json | 读写JSON入门 | Pom |
spring-boot-starter-jta-atomikos | 使用Atomikos的JTA交易入门 | Pom |
spring-boot-starter-jta-bitronix | 使用Bitronix的JTA交易入门 | Pom |
spring-boot-starter-mail | 使用Java Mail和Spring Framework的电子邮件发送支持的入门 | Pom |
spring-boot-starter-mustache | 使用Mustache视图构建Web应用程序的入门 | Pom |
spring-boot-starter-oauth2-client | 使用Spring Security的OAuth2 / OpenID Connect客户端功能的入门 | Pom |
spring-boot-starter-oauth2-resource-server | 使用Spring Security的OAuth2资源服务器功能的入门 | Pom |
spring-boot-starter-quartz | 入门使用Quartz Scheduler | Pom |
spring-boot-starter-rsocket | 用于构建RSocket客户端和服务器的入门。 | Pom |
spring-boot-starter-security | 使用Spring Security的入门 | Pom |
spring-boot-starter-test | 用于使用包括JUnit,Hamcrest和Mockito在内的库测试Spring Boot应用程序的入门程序 | Pom |
spring-boot-starter-thymeleaf | 使用Thymeleaf视图构建MVC Web应用程序的入门 | Pom |
spring-boot-starter-validation | 通过Hibernate Validator使用Java Bean验证的入门 | Pom |
spring-boot-starter-web | 使用Spring MVC构建Web(包括RESTful)应用程序的入门者。使用Tomcat作为默认的嵌入式容器 | Pom |
spring-boot-starter-web-services | 使用Spring Web Services的入门 | Pom |
spring-boot-starter-webflux | 使用Spring Framework的Reactive Web支持构建WebFlux应用程序的入门者 | Pom |
spring-boot-starter-websocket | 使用Spring Framework的WebSocket支持构建WebSocket应用程序的入门 | Pom |
主程序
//@SpringBootApplication 标注这个类是一个springboot的应用
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
-
注解
-
@SpringBootConfiguration:springboot的配置 @Configuration:spring 的配置类 @Compoent: 组件 证明 他还是spring的组件 @EnableAutoConfiguration: 自动配置 @AutoConfigurationPackage:自动配置包 @Import(AutoConfigurationPackages.Registrar.class) 自动配置'包注册器' @Import(AutoConfigurationImportSelector.class) 自动配置 导入选择 获取所有的配置 List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);
获取候选的配置
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) { List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader()); Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you " + "are using a custom packaging, make sure that file is correct."); return configurations; }
自动配置的核心文件: META-INF/spring.factories
-
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-m1JvNu07-1585723014582)(D:\笔记包\笔记\图解\自动配置核心文件.png)]
结论:springboot所有自动配置 ,都在启动时被扫描并加载,spring.factories里面的所有配置类都在这个里面但是不一定生效 ,要判断条件是否成立,需要导入对应的启动器(spring-boot-starter),有了启动器,自动装配就会生效,然后就配置成功了;
1.springboot在启动的时候,从类类路径下/META-INF/spring.factories获得指定的值
2.将这些配置的类导入容器,自动配置类就会生效,帮我们进行自动配置
3.以前我们需要自动配置的东子,现在spring boot帮我们自动配置
4.整合javaEE,解决方案何自动配置的东西都在 spring-boot-autoconfigure-.2.0RELEASE.jar这个包下
5.他会把所有需要导入的组件,以类名的方式返回,这组件就会被添加到容器;
6.容器也会存在 所有xxxAutoConfiguration的文件(@Bean),就是这些类给容器导入了这个场景所需要的所有组件;并自动配置 @Configuration ,javaConfig(java配置类)
7.有了自动配置,免去了我们手动编写配置文件的工作
Run
我最初以为就是运行了一个main方法,没想到却开启了一个服务;
@SpringBootApplication
public class SpringbootDemo02Application {
public static void main(String[] args) {
//该方法返回一个ConfigurableApplicationContext对象
//参数一:应用入口的类 参数类:命令行参数
SpringApplication.run(SpringbootDemo02Application.class, args);
}
}
SpringApplication.run分析
分析该方法主要分两部分,一部分是SpringApplication的实例化,二是run方法的执行;
SpringApplication
这个类主要做了以下四件事情
- 推断应用的类型是普通的项目还是Web项目
- 查找并加载所有可用初始化器 , 设置到initializers属性中
- 找出所有的应用程序监听器,设置到listeners属性中
- 推断并设置main方法的定义类,找到运行的主类
查看构造器
public SpringApplication(ResourceLoader resourceLoader, Class... primarySources) {
this.sources = new LinkedHashSet();
this.bannerMode = Mode.CONSOLE;
this.logStartupInfo = true;
this.addCommandLineProperties = true;
this.addConversionService = true;
this.headless = true;
this.registerShutdownHook = true;
this.additionalProfiles = new HashSet();
this.isCustomEnvironment = false;
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must not be null");
this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));
this.webApplicationType = WebApplicationType.deduceFromClasspath();
this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
this.mainApplicationClass = this.deduceMainApplicationClass();
}
run方法
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YBf4DbJR-1585723014584)(https://blog.kuangstudy.com/usr/uploads/2019/10/700745206.png)]
JSR-303
@Validated
JSR-303 是JAVA EE 6 中的一项子规范,叫做Bean Validation,Hibernate Validator 是 Bean Validation 的参考实现 . Hibernate Validator 提供了 JSR 303 规范中所有内置 constraint 的实现,除此之外还有一些附加的 constraint。
Bean Validation 中内置的 constraint
Hibernate Validator 附加的 constraint
SpringBoot 自动装配原理
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-S51ANYvv-1585723014588)(D:\笔记包\笔记\图解\自动装配原理.png)]