SpringBoot基本配置


入口類和@SpringBootApplication

  @SpringBootApplication是Spring Boot的核心注解,是一個組合注解,部分源碼如下:

@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 

  • 它的核心功能由@EnableAutoConfiguration注解提供,@EnableAutoConfiguration讓Spring Boot根據類中的jar包依賴為當前項目進行自動配置
  • Spring Boot會自動掃描@SpringBootApplication注解所在類的同級包以及下級包中的Bean

關閉特定的自動配置

通過@SpringBootApplication注解下的exclude參數如:

@SpringBootApplication(exclude={DataSourceAutoConfigration.class})

 

 

定制Banner

Spring Boot啟動會有一個默認圖案,這個圖案 可以自定義, 自定義方式如下

在src/main/resource下新建一個banner.txt文件

通過http://patorjk.com/software/taag/這個網站生成字符

將生成的字符粘貼到banner.txt文件中即可

關閉banner可以修改入口類

SpringApplication app = new SpringApplication(DemoApplication.class);

    app.setBannerMode(Banner.Mode.OFF);

    app.run(args);

 

starter pom

  Spring Boot提供了很多starter pom方便進行maven配置,下面是一些常見的starter:

  

名稱

描述

spring-boot-starter

核心Spring Boot starter,包括自動配置支持,日志和YAML

spring-boot-starter-actuator

生產准備的特性,用於幫我們監控和管理應用

spring-boot-starter-amqp

對”高級消息隊列協議”的支持,通過spring-rabbit實現

spring-boot-starter-aop

對面向切面編程的支持,包括spring-aop和AspectJ

spring-boot-starter-batch

對Spring Batch的支持,包括HSQLDB數據庫

spring-boot-starter-cloud-connectors

對Spring Cloud Connectors的支持,簡化在雲平台下(例如,Cloud Foundry 和Heroku)服務的連接

spring-boot-starter-data-elasticsearch

對Elasticsearch搜索和分析引擎的支持,包括spring-data-elasticsearch

spring-boot-starter-data-gemfire

對GemFire分布式數據存儲的支持,包括spring-data-gemfire

spring-boot-starter-data-jpa

對”Java持久化API”的支持,包括spring-data-jpa,spring-orm和Hibernate

spring-boot-starter-data-mongodb

對MongoDB NOSQL數據庫的支持,包括spring-data-mongodb

spring-boot-starter-data-rest

對通過REST暴露Spring Data倉庫的支持,通過spring-data-rest-webmvc實現

spring-boot-starter-data-solr

對Apache Solr搜索平台的支持,包括spring-data-solr

spring-boot-starter-freemarker

對FreeMarker模板引擎的支持

spring-boot-starter-groovy-templates

對Groovy模板引擎的支持

spring-boot-starter-hateoas

對基於HATEOAS的RESTful服務的支持,通過spring-hateoas實現

spring-boot-starter-hornetq

對”Java消息服務API”的支持,通過HornetQ實現

spring-boot-starter-integration

對普通spring-integration模塊的支持

spring-boot-starter-jdbc

對JDBC數據庫的支持

spring-boot-starter-jersey

對Jersey RESTful Web服務框架的支持

spring-boot-starter-jta-atomikos

對JTA分布式事務的支持,通過Atomikos實現

spring-boot-starter-jta-bitronix

對JTA分布式事務的支持,通過Bitronix實現

spring-boot-starter-mail

對javax.mail的支持

spring-boot-starter-mobile

對spring-mobile的支持

spring-boot-starter-mustache

對Mustache模板引擎的支持

spring-boot-starter-redis

對REDIS鍵值數據存儲的支持,包括spring-redis

spring-boot-starter-security

對spring-security的支持

spring-boot-starter-social-facebook

對spring-social-facebook的支持

spring-boot-starter-social-linkedin

對spring-social-linkedin的支持

spring-boot-starter-social-twitter

對spring-social-twitter的支持

spring-boot-starter-test

對常用測試依賴的支持,包括JUnit, Hamcrest和Mockito,還有spring-test模塊

spring-boot-starter-thymeleaf

對Thymeleaf模板引擎的支持,包括和Spring的集成

spring-boot-starter-velocity

對Velocity模板引擎的支持

spring-boot-starter-web

對全棧web開發的支持, 包括Tomcat和spring-webmvc

spring-boot-starter-websocket

對WebSocket開發的支持

spring-boot-starter-ws

對Spring Web服務的支持

 

 

注:本文是個人學習筆記,以上內容均來自《JavaEE開發的顛覆者 Spring Boot實戰》一書

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM