SpringBoot系列0-开始

 

1.spring-boot-starter-parent

   spring推荐我们按照下面的方式使用spring-boot-starter-parent,这个父Pom的作用是提供常用jar包版本控制的。打开源文件可以惊奇得发现,spring-boot-starter-parent的parent是 spring-boot-dependencies,spring-boot-dependencies的作用我们马上会说。

        <parent> 
		<groupId> org.springframework.boot </ groupId> 
		<artifactId> spring-boot-starter-parent </ artifactId> 
		<version> 2.0.0.BUILD- SNAPSHOT </ version> 
	</ parent>   

  但是在实际过程中我们一般使用自己公司的parent,如何解决这个问题呢?如下,在项目主POM中<dependencyManagement>节点下面定义。

<dependencyManagement>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>1.4.0.RELEASE</version>
    <type>pom</type>
    <scope>import</scope>
  </dependency>
</dependencyManagement>

需要注意spring-boot-dependencies只能在dependencyManagement中声明,而不能在子模块中出现,因为spring-boot-dependencies中没有声明引入任何jar包,只是一个pom配置,打开maven仓库可以看到,它里面定义了很多依赖声明。

 如果想在项目中使用spring-boot-dependencies中的jar包,但是不用默认版本,需要在dependencyManagement中指定版本,并且需要在spring-boot-dependencies之前。

2.starters


startrts(启动器)是里面包含了一系列依赖描述,方便你在项目中使用。比如

if you want to getstarted using Spring and JPA for database access, just include the spring-boot-starter-data-jpa dependency in your project, and you are good to go.

官方启动器一般是spring-boot-starter-*

第三方启动器命名一般是*-spring-boot-starter

下面是一些常用的Starters.

名称 描述

spring-boot-starter

核心入门者,包括自动配置支持,日志记录和YAML

spring-boot-starter-activemq

使用Apache ActiveMQ启动JMS消息传递

spring-boot-starter-amqp

使用Spring AMQP和Rabbit MQ的入门者

spring-boot-starter-aop

使用Spring AOP和AspectJ进行面向方面编程的入门者

spring-boot-starter-artemis

使用Apache Artemis开始JMS消息传递

spring-boot-starter-batch

使用Spring Batch的入门者

spring-boot-starter-cache

Starter使用Spring Framework的缓存支持

spring-boot-starter-cloud-connectors

Starter使用Spring Cloud Connectors,可简化Cloud Foundry和Heroku等云平台中的服务连接

spring-boot-starter-data-cassandra

入门使用Cassandra分布式数据库和Spring Data Cassandra

spring-boot-starter-data-cassandra-reactive

使用Cassandra分布式数据库和Spring Data Cassandra Reactive的初学者

spring-boot-starter-data-couchbase

使用Couchbase面向文档的数据库和Spring Data Couchbase的初学者

spring-boot-starter-data-couchbase-reactive

初级用于使用Couchbase面向文档的数据库和Spring Data Couchbase Reactive

spring-boot-starter-data-elasticsearch

使用Elasticsearch搜索和分析引擎和Spring Data Elasticsearch的入门者

spring-boot-starter-data-jpa

使用Spring数据JPA与Hibernate的入门者

spring-boot-starter-data-ldap

使用Spring Data LDAP的入门者

spring-boot-starter-data-mongodb

入门使用MongoDB面向文档的数据库和Spring Data MongoDB

spring-boot-starter-data-mongodb-reactive

入门使用MongoDB面向文档的数据库和Spring Data MongoDB Reactive

spring-boot-starter-data-neo4j

初学者使用Neo4j图形数据库和Spring Data Neo4j

spring-boot-starter-data-redis

使用Spring Data Redis和Lettuce客户端使用Redis键值数据存储的入门者

spring-boot-starter-data-redis-reactive

初学者使用Redis键值数据存储以及Spring Data Redis反应器和Lettuce客户端

spring-boot-starter-data-rest

Starter使用Spring Data REST通过REST公开Spring Data存储库

spring-boot-starter-data-solr

启动Spring Data Solr使用Apache Solr搜索平台

spring-boot-starter-freemarker

使用FreeMarker视图构建MVC Web应用程序的入门者

spring-boot-starter-groovy-templates

使用Groovy模板视图构建MVC Web应用程序的入门者

spring-boot-starter-hateoas

使用Spring MVC和Spring HATEOAS构建基于超媒体的RESTful Web应用程序的入门者

spring-boot-starter-integration

使用Spring Integration的入门者

spring-boot-starter-jdbc

将JDBC与Tomcat JDBC连接池配合使用的初学者

spring-boot-starter-jersey

使用JAX-RS和Jersey构建RESTful Web应用程序的入门者。替代方案spring-boot-starter-web

spring-boot-starter-jooq

使用jOOQ访问SQL数据库的入门者。替代spring-boot-starter-data-jpaspring-boot-starter-jdbc

spring-boot-starter-json

用于阅读和编写json的初学者

spring-boot-starter-jta-atomikos

使用Atomikos启动JTA交易

spring-boot-starter-jta-bitronix

使用Bitronix启动JTA交易

spring-boot-starter-jta-narayana

春季启动Narayana JTA初学者

spring-boot-starter-mail

Starter使用Java Mail和Spring Framework的电子邮件发送支持

spring-boot-starter-mustache

使用Mustache视图构建Web应用程序的入门者

spring-boot-starter-quartz

春季启动石英起动器

spring-boot-starter-security

Starter使用Spring Security

spring-boot-starter-test

Starter用于测试包含JUnit,Hamcrest和Mockito等库的Spring Boot应用程序

spring-boot-starter-thymeleaf

使用Thymeleaf视图构建MVC Web应用程序的入门者

spring-boot-starter-validation

通过Hibernate Validator使用Java Bean验证的入门者

spring-boot-starter-web

使用Spring MVC构建Web的初学者,包括RESTful应用程序。使用Tomcat作为默认的嵌入容器

spring-boot-starter-web-services

使用Spring Web Services的入门者

spring-boot-starter-webflux

使用Spring Framework的Reactive Web支持构建WebFlux应用程序的入门者

spring-boot-starter-websocket

使用Spring Framework的WebSocket支持构建WebSocket应用程序的入门者


3.Auto-configuration

如在添加了jar包,并且没有手动配置相关的bean,spring boot会自动配置到spring应用程序中。

您需要选择加入@EnableAutoConfiguration或 @SpringBootApplication注释到您的某个@Configuration类来自动配置。


4.spirngbean和依赖注入

如果是将application class放在工程的根目录,只需要在main class上添加注解@ComponentScan,并且不需要任何参数。那么在项目中所有的组件【@Component, @Service, @Repository, @Controller】都会自动注册为spring bean.


5.用@SpringBootApplication简化注解

  @SpringBootApplication等价于@Configuration,@EnableAutoConfiguration和@ComponentScan三者之和。










猜你喜欢

转载自blog.csdn.net/xtj332/article/details/79377591
-0-