Spring boot应用程序采用各种Starters启动器,入口类是包含@SpringBootApplication
注解和main方法的类,然后使用@ComponentScan
注解自动扫描项目中的所有组件,并且Spring Boot会根据@EnableAutoConfiguration
注解将项目中的依赖项自动配置到应用程序中.例如,如果MySQL数据库在类路径上,但尚未配置任何数据库连接,则Spring Boot会自动配置内存数据库.
Spring Boot应用程序的入口点是包含@SpringBootApplication
注解的类,该类中包含运行Spring Boot应用程序的main方法.默认一般是一个* application 结尾。
@SpringBootApplication注解包括自动配置,组件扫描和Spring Boot配置.
如果将@SpringBootApplication注解添加到类中,则无需添加@EnableAutoConfiguration,@ComponentScan和@SpringBootConfiguration注解.@SpringBootApplication注解包括其他所有的注解.
@SpringBootApplication注解是Spring Boot的核心注解,用于标注程序是一个springboot 程序,它是一个组合注解,由多个注解组合而成
@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) })
public @interface SpringBootApplication {
//此处忽略接口内部内容
}
@SpringBootConfiguration注解可以用Java代码的形式实现spring中xml配置文件配置的效果。也就是用来加载配置文件。
@Target({
ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
}
启用自动配置,该注解会使Spring Boot根据项目中依赖的jar包自动配置项目的配置项,这也是 springboot 的核心注解之一,我们只需要将项目需要的依赖包导入进来,它会自动帮我们配置这个依赖需要的基本配置
比如我们的项目引入了spring-boot-starter-web依赖,springboot 会自动帮我们配置 tomcat 和 springmvc
默认扫描@SpringBootApplication类所在包的同级目录以及它的子目录。用来扫描组件和自动装配。
springboot 支持的自动配置可以查看到
在spring-boot-autoconfigure包中包含了支持的自动装配依赖,截图只是其中一部分,我们开发中常见的依赖都做了基本配置
SpringBoot使用的全局配置文件有两种格式:
• application.properties
• application.yml
虽然后缀不一样,但是项目中有一个就可以了,yml格式比properties格式的简洁一些。
这个文件的位置在配置文件放在src/main/resources目录或者类路径/config下。
properties 和 .yml,它们的区别主要是书写格式不同。
1).properties
server.port=8088
server.servlet.context-path=/testboot
2).yml
server:
port: 8088
servlet:
context-path: /testboot
另外yml 格式不支持 @PropertySource 注解导入配置
:和值之间有一个空格
全局配置文件的作用是:可以对一些默认配置值进行修改。
springboot 提供了很多 starter, 可以帮我们快速构建相应的开发环境,导入相关的依赖,并设置相关配置
Name | Description | Pom |
---|---|---|
spring-boot-starter |
Core starter, including auto-configuration support, logging and YAML 核心启动器,包括自动配置,日志以及 YAML 支持 | Pom |
spring-boot-starter-activemq |
Starter for JMS messaging using Apache ActiveMQ 对Apache ActiveMQ提供支持 | Pom |
spring-boot-starter-amqp |
Starter for using Spring AMQP and Rabbit MQ 对Spring AMQP 和 Rabbit MQ支持 | Pom |
spring-boot-starter-aop |
Starter for aspect-oriented programming with Spring AOP and AspectJ 对 spring AOP和AspectJ提供支持 | Pom |
spring-boot-starter-artemis |
Starter for JMS messaging using Apache Artemis 对Apache Artemis提供支持 | Pom |
spring-boot-starter-batch |
Starter for using Spring Batch | Pom |
spring-boot-starter-cache |
Starter for using Spring Framework’s caching support | Pom |
spring-boot-starter-cloud-connectors |
Starter for using Spring Cloud Connectors which simplifies connecting to services in cloud platforms like Cloud Foundry and Heroku | Pom |
spring-boot-starter-data-cassandra |
Starter for using Cassandra distributed database and Spring Data Cassandra | Pom |
spring-boot-starter-data-cassandra-reactive |
Starter for using Cassandra distributed database and Spring Data Cassandra Reactive | Pom |
spring-boot-starter-data-couchbase |
Starter for using Couchbase document-oriented database and Spring Data Couchbase | Pom |
spring-boot-starter-data-couchbase-reactive |
Starter for using Couchbase document-oriented database and Spring Data Couchbase Reactive | Pom |
spring-boot-starter-data-elasticsearch |
Starter for using Elasticsearch search and analytics engine and Spring Data Elasticsearch | Pom |
spring-boot-starter-data-jpa |
Starter for using Spring Data JPA with Hibernate | Pom |
spring-boot-starter-data-ldap |
Starter for using Spring Data LDAP | Pom |
spring-boot-starter-data-mongodb |
Starter for using MongoDB document-oriented database and Spring Data MongoDB | Pom |
spring-boot-starter-data-mongodb-reactive |
Starter for using MongoDB document-oriented database and Spring Data MongoDB Reactive | Pom |
spring-boot-starter-data-neo4j |
Starter for using Neo4j graph database and Spring Data Neo4j | Pom |
spring-boot-starter-data-redis |
Starter for using Redis key-value data store with Spring Data Redis and the Lettuce client | Pom |
spring-boot-starter-data-redis-reactive |
Starter for using Redis key-value data store with Spring Data Redis reactive and the Lettuce client | Pom |
spring-boot-starter-data-rest |
Starter for exposing Spring Data repositories over REST using Spring Data REST | Pom |
spring-boot-starter-data-solr |
Starter for using the Apache Solr search platform with Spring Data Solr | Pom |
spring-boot-starter-freemarker |
Starter for building MVC web applications using FreeMarker views | Pom |
spring-boot-starter-groovy-templates |
Starter for building MVC web applications using Groovy Templates views | Pom |
spring-boot-starter-hateoas |
Starter for building hypermedia-based RESTful web application with Spring MVC and Spring HATEOAS | Pom |
spring-boot-starter-integration |
Starter for using Spring Integration | Pom |
spring-boot-starter-jdbc |
Starter for using JDBC with the Tomcat JDBC connection pool | Pom |
spring-boot-starter-jersey |
Starter for building RESTful web applications using JAX-RS and Jersey. An alternative to spring-boot-starter-web |
Pom |
spring-boot-starter-jooq |
Starter for using jOOQ to access SQL databases. An alternative to spring-boot-starter-data-jpa or spring-boot-starter-jdbc |
Pom |
spring-boot-starter-json |
Starter for reading and writing json | Pom |
spring-boot-starter-jta-atomikos |
Starter for JTA transactions using Atomikos | Pom |
spring-boot-starter-jta-bitronix |
Starter for JTA transactions using Bitronix | Pom |
spring-boot-starter-jta-narayana |
Spring Boot Narayana JTA Starter | Pom |
spring-boot-starter-mail |
Starter for using Java Mail and Spring Framework’s email sending support | Pom |
spring-boot-starter-mustache |
Starter for building web applications using Mustache views | Pom |
spring-boot-starter-quartz |
Spring Boot Quartz Starter | Pom |
spring-boot-starter-security |
Starter for using Spring Security | Pom |
spring-boot-starter-test |
Starter for testing Spring Boot applications with libraries including JUnit, Hamcrest and Mockito | Pom |
spring-boot-starter-thymeleaf |
Starter for building MVC web applications using Thymeleaf views | Pom |
spring-boot-starter-validation |
Starter for using Java Bean Validation with Hibernate Validator | Pom |
spring-boot-starter-web |
Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container | Pom |
spring-boot-starter-web-services |
Starter for using Spring Web Services | Pom |
spring-boot-starter-webflux |
Starter for building WebFlux applications using Spring Framework’s Reactive Web support | Pom |
spring-boot-starter-websocket |
Starter for building WebSocket applications using Spring Framework’s WebSocket support | Pom |
# ===================================================================
# COMMON SPRING BOOT PROPERTIES
#
# This sample file is provided as a guideline. Do NOT copy it in its
# entirety to your own application. ^^^
# ===================================================================
# ----------------------------------------
# CORE PROPERTIES
# ----------------------------------------
# BANNER
banner.charset=UTF-8 # Banner file encoding.
banner.location=classpath:banner.txt # Banner file location.
banner.image.location=classpath:banner.gif # Banner image file location (jpg/png can also be used).
banner.image.width= # Width of the banner image in chars (default 76)
banner.image.height= # Height of the banner image in chars (default based on image height)
banner.image.margin= # Left hand image margin in chars (default 2)
banner.image.invert= # If images should be inverted for dark terminal themes (default false)
# LOGGING
logging.config= # Location of the logging configuration file. For instance `classpath:logback.xml` for Logback
logging.exception-conversion-word=%wEx # Conversion word used when logging exceptions.
logging.file= # Log file name. For instance `myapp.log`
logging.level.*= # Log levels severity mapping. For instance `logging.level.org.springframework=DEBUG`
logging.path= # Location of the log file. For instance `/var/log`
logging.pattern.console= # Appender pattern for output to the console. Only supported with the default logback setup.
logging.pattern.file= # Appender pattern for output to the file. Only supported with the default logback setup.
logging.pattern.level= # Appender pattern for log level (default %5p). Only supported with the default logback setup.
logging.register-shutdown-hook=false #