SpringBoot四大神器之Starter

SpringBoot的starter主要用来简化依赖用的。本文主要分两部分,一部分是列出一些starter的依赖,另一部分是教你自己写一个starter。

部分starters的依赖

Starter(Group ID: org.springframework.boot) 传递依赖于
spring-boot-starter-log4j2 ■ org.apache.logging.log4j:log4j-slf4j-impl
■ org.apache.logging.log4j:log4j-api
■ org.apache.logging.log4j:log4j-core
■ org.slf4j:jcl-over-slf4j
■ org.slf4j:jul-to-slf4j
spring-boot-starter-logging ■ ch.qos.logback:logback-classic
■ org.slf4j:jcl-over-slf4j
■ org.slf4j:jul-to-slf4j
■ org.slf4j:log4j-over-slf4j
spring-boot-starter-mail ■ org.springframework.boot:spring-boot-starter
■ org.springframework:spring-context
■ org.springframework:spring-context-support
■ com.sun.mail:javax.mail
spring-boot-starter-mobile ■ org.springframework.boot:spring-boot-starter
■ org.springframework.boot:spring-boot-starter-web
■ org.springframework.mobile:spring-mobile-device
spring-boot-starter-mustache ■ org.springframework.boot:spring-boot-starter
■ org.springframework.boot:spring-boot-starter-web
■ com.samskivert:jmustache
spring-boot-starter-redis ■ org.springframework.boot:spring-boot-starter
■ org.springframework.data:spring-data-redis
■ redis.clients:jedis
spring-boot-starter-remote-shell ■ org.springframework.boot:spring-boot-starter
■ org.springframework.boot:spring-boot-starter-actuator
■ org.crashub:crash.cli
■ org.crashub:crash.connectors.ssh (excludes org.codehaus.groovy:groovy-all)
■ org.crashub:crash.connectors.telnet (excludes javax.servlet:servlet-api, log4j :log4j, commons-logging:commons-logging)
■ org.crashub:crash.embed.spring(excludes org.springframework:spring-web, org.codehaus.groovy:groovy-all)
■ org.crashub:crash.plugins.cron (excludes org.codehaus.groovy:groovy-all)
■ org.crashub:crash.plugins.mail (excludes org.codehaus.groovy:groovy-all)
■ org.crashub:crash.shell (excludes org.codehaus.groovy:groovy-all)
■ org.codehaus.groovy:groovy
spring-boot-starter-security ■ org.springframework.boot:spring-boot-starter
■ org.springframework:spring-aop
■org.springframework.security:spring-security-config
■org.springframework.security:spring-security-web
spring-boot-starter-social-facebook ■org.springframework.boot:spring-boot-starter
■ org.springframework.boot:spring-boot-starter-web
■org.springframework.social:spring-social-config
■ org.springframework.social:spring-social-core
■ org.springframework.social:spring-social-web
■org.springframework.social:spring-social-facebook
spring-boot-starter-social-linkedin ■org.springframework.boot:spring-boot-starter
■ org.springframework.boot:spring-boot-starter-web
■org.springframework.social:spring-social-config
■ org.springframework.social:spring-social-core
■ org.springframework.social:spring-social-web
■org.springframework.social:spring-social-linkedin
spring-boot-starter-social-twitter ■org.springframework.boot:spring-boot-starter
■ org.springframework.boot:spring-boot-starter-web
■ org.springframework.social:spring-social-config
■ org.springframework.social:spring-social-core
■org.springframework.social:spring-social-web
■ org.springframework.social:spring-social-twitter
spring-boot-starter-test ■ junit:junit
■ org.mockito:mockito-core
■ org.hamcrest:hamcrest-core
■ org.hamcrest:hamcrest-library
■ org.springframework:spring-core(excludes commons-logging:commons-logging)
■ org.springframework:spring-test
spring-boot-starter-thymeleaf ■ org.springframework.boot:spring-boot-starter
■ org.springframework.boot:spring-boot-starter-web
■ org.thymeleaf:thymeleaf-spring4
■nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect
spring-boot-starter-tomcat ■ org.apache.tomcat.embed:tomcat-embed-core
■ org.apache.tomcat.embed:tomcat-embed-el
■org.apache.tomcat.embed:tomcat-embed-logging-juli
■org.apache.tomcat.embed:tomcat-embed-websocket
spring-boot-starter-undertow ■ io.undertow:undertow-core
■ io.undertow:undertow-servlet(excludes org.jboss.spec.javax.servlet:jboss-servlet-api_3.1_spec)
■ io.undertow:undertow-websockets-jsr
■ javax.servlet:javax.servlet-api
■ org.glassfish:javax.el
spring-boot-starter-validation ■ org.springframework.boot:spring-boot-starter
■ org.apache.tomcat.embed:tomcat-embed-el
■org.hibernate:hibernate-validator
spring-boot-starter-velocity ■ org.springframework.boot:spring-boot-starter
■ org.springframework.boot:spring-boot-starter-web
■ commons-beanutils:commons-beanutils
■ commons-collections:commons-collections
■ commons-digester:commons-digester
■ org.apache.velocity:velocity
■ org.apache.velocity:velocity-tools
■ org.springframework:spring-context-support
spring-boot-starter-web ■ org.springframework.boot:spring-boot-starter
■ org.springframework.boot:spring-boot-starter-tomcat
■ org.springframework.boot:spring-boot-starter-validation
■com.fasterxml.jackson.core:jackson-databind
■ org.springframework:spring-web
■ org.springframework:spring-webmvc
spring-boot-starter-websocket ■ org.springframework.boot:spring-boot-starter
■ org.springframework.boot:spring-boot-starter-web
■ org.springframework:spring-messaging
■org.springframework:spring-websocket
spring-boot-starter-ws ■ org.springframework.boot:spring-boot-starter
■ org.springframework.boot:spring-boot-starter-web ■org.springframework:spring-jms
■ org.springframework:spring-oxm
■ org.springframework.ws:spring-ws-core
■ org.springframework.ws:spring-ws-support

Reference

来自<<Spring Boot in Action>>的附录B SpringBoot Starters的内容

如何自己写starter

主要步骤

  • 1、选择已有的starters,在此基础上进行扩展.

  • 2、创建自动配置文件并设定META-INF/spring.factories里的内容.

  • 3、发布你的starter

添加依赖管理


  
  
  1. <dependencyManagement>
  2. <dependencies>
  3. <dependency>
  4. <groupId>org.springframework.boot </groupId>
  5. <artifactId>spring-boot-dependencies </artifactId>
  6. <version>${spring.boot.version} </version>
  7. <type>pom </type>
  8. <scope>import </scope>
  9. </dependency>
  10. </dependencies>
  11. </dependencyManagement>

添加starter自己的依赖


  
  
  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot </groupId>
  4. <artifactId>spring-boot-starter </artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot </groupId>
  8. <artifactId>spring-boot-starter-web </artifactId>
  9. </dependency>
  10. </dependencies>

新建configuration


  
  
  1. @Configuration
  2. @ComponentScan( basePackages = { "com.patterncat.actuator"} )
  3. public class WebAutoConfiguration {
  4. /**
  5. * addViewController方法不支持placeholder的解析
  6. * 故在这里用变量解析出来
  7. */
  8. @Value( "${actuator.web.base:}")
  9. String actuatorBase;
  10. // @Bean
  11. // public ActuatorNavController actuatorNavController(){
  12. // return new ActuatorNavController();
  13. // }
  14. @Bean
  15. public WebMvcConfigurerAdapter configStaticMapping() {
  16. return new WebMvcConfigurerAdapter() {
  17. @Override
  18. public void addViewControllers(ViewControllerRegistry registry) {
  19. //配置跳转
  20. registry.addViewController(actuatorBase+ "/nav").setViewName(
  21. "forward:/static/nav.html");
  22. }
  23. @Override
  24. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  25. registry.addResourceHandler( "/static/**").
  26. addResourceLocations( "classpath:/static/");
  27. }
  28. };
  29. }
  30. }

修改/META-INF/spring.factories


  
  
  1. # AutoConfigurations
  2. org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.patterncat.actuator.configuration.WebAutoConfiguration

发布

mvn clean install
  
  

引用


  
  
  1. <dependency>
  2. <groupId>com.patterncat </groupId>
  3. <artifactId>spring-boot-starter-actuator-web </artifactId>
  4. <version>1.0-SNAPSHOT </version>
  5. </dependency>

启动访问

mvn spring-boot:run
  
  

访问

http://localhost:8080/nav
  
  

note

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值