SpringBoot快速开始

第一个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核心入门工具,包括自动配置支持,日志记录和YAMLPom
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 CFEnvPom
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-webPom
spring-boot-starter-jooq使用jOOQ访问SQL数据库的入门。替代spring-boot-starter-data-jpaspring-boot-starter-jdbcPom
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 SchedulerPom
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

这个类主要做了以下四件事情

  1. 推断应用的类型是普通的项目还是Web项目
  2. 查找并加载所有可用初始化器 , 设置到initializers属性中
  3. 找出所有的应用程序监听器,设置到listeners属性中
  4. 推断并设置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

img

Hibernate Validator 附加的 constraint

img

SpringBoot 自动装配原理

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-S51ANYvv-1585723014588)(D:\笔记包\笔记\图解\自动装配原理.png)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值