SpringBoot四大神器之Starter

来源:互联网 发布:社交网络数据挖掘 pdf 编辑:程序博客网 时间:2024/05/21 09:42

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-slf4jspring-boot-starter-logging■ ch.qos.logback:logback-classic
■ org.slf4j:jcl-over-slf4j
■ org.slf4j:jul-to-slf4j
■ org.slf4j:log4j-over-slf4jspring-boot-starter-mail■ org.springframework.boot:spring-boot-starter
■ org.springframework:spring-context
■ org.springframework:spring-context-support
■ com.sun.mail:javax.mailspring-boot-starter-mobile■ org.springframework.boot:spring-boot-starter
■ org.springframework.boot:spring-boot-starter-web
■ org.springframework.mobile:spring-mobile-devicespring-boot-starter-mustache■ org.springframework.boot:spring-boot-starter
■ org.springframework.boot:spring-boot-starter-web
■ com.samskivert:jmustachespring-boot-starter-redis■ org.springframework.boot:spring-boot-starter
■ org.springframework.data:spring-data-redis
■ redis.clients:jedisspring-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:groovyspring-boot-starter-security■ org.springframework.boot:spring-boot-starter
■ org.springframework:spring-aop
■org.springframework.security:spring-security-config
■org.springframework.security:spring-security-webspring-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-facebookspring-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-linkedinspring-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-twitterspring-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-testspring-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-dialectspring-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-websocketspring-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.elspring-boot-starter-validation■ org.springframework.boot:spring-boot-starter
■ org.apache.tomcat.embed:tomcat-embed-el
■org.hibernate:hibernate-validatorspring-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-supportspring-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-webmvcspring-boot-starter-websocket■ org.springframework.boot:spring-boot-starter
■ org.springframework.boot:spring-boot-starter-web
■ org.springframework:spring-messaging
■org.springframework:spring-websocketspring-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

添加依赖管理

<dependencyManagement>        <dependencies>            <dependency>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-dependencies</artifactId>                <version>${spring.boot.version}</version>                <type>pom</type>                <scope>import</scope>            </dependency>        </dependencies></dependencyManagement>

添加starter自己的依赖

<dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency></dependencies>

新建configuration

@Configuration@ComponentScan( basePackages = {"com.patterncat.actuator"} )public class WebAutoConfiguration {    /**     * addViewController方法不支持placeholder的解析     * 故在这里用变量解析出来     */    @Value("${actuator.web.base:}")    String actuatorBase;//    @Bean//    public ActuatorNavController actuatorNavController(){//        return new ActuatorNavController();//    }    @Bean    public WebMvcConfigurerAdapter configStaticMapping() {        return new WebMvcConfigurerAdapter() {            @Override            public void addViewControllers(ViewControllerRegistry registry) {                //配置跳转                registry.addViewController(actuatorBase+"/nav").setViewName(                        "forward:/static/nav.html");            }            @Override            public void addResourceHandlers(ResourceHandlerRegistry registry) {                registry.addResourceHandler("/static/**").                        addResourceLocations("classpath:/static/");            }        };    }}

修改/META-INF/spring.factories

# AutoConfigurationsorg.springframework.boot.autoconfigure.EnableAutoConfiguration=com.patterncat.actuator.configuration.WebAutoConfiguration

发布

mvn clean install

引用

        <dependency>            <groupId>com.patterncat</groupId>            <artifactId>spring-boot-starter-actuator-web</artifactId>            <version>1.0-SNAPSHOT</version>        </dependency>

启动访问

mvn spring-boot:run

访问

http://localhost:8080/nav

note

  • github工程源码

  • Spring Boot:定制自己的starter

  • Boot your own infrastructure – Extending Spring Boot in five steps

0 0
原创粉丝点击