018-spring Boot Starter Development

Source: Internet
Author: User

Self-built Spring-boot-starter

Artifactid naming

Spring official starter is usually named Spring-boot-starter-{name} such as Spring-boot-starter-web,

Spring officially recommends that unofficial starter names should follow the format of {Name}-spring-boot-starter.

Example using Redis as an example

1. Create a new MAVEN project Redis-spring-boot-starter, add Pom
        <Dependency>            <groupId>Redis.clients</groupId>            <Artifactid>Jedis</Artifactid>            <version>2.9.0</version>        </Dependency>        <Dependency>            <groupId>Org.springframework.boot</groupId>            <Artifactid>Spring-boot-starter</Artifactid>            <version>1.5.9.RELEASE</version>        </Dependency>
2 "Add configuration Properties
 PackageCom.lhx.spring.redis;Importorg.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties (prefix= "Redis") Public classredisproperties {PrivateString host; PrivateInteger Port;  PublicString gethost () {returnhost; }     Public voidSethost (String host) { This. Host =host; }     PublicInteger Getport () {returnPort; }     Public voidSetport (Integer port) { This. Port =Port; }}
View Code3 "Add Autoconfiguration Class
@Configuration @conditionalonclass (Jedis. class ) @EnableConfigurationProperties (redisproperties. class )publicclass  redisautoconfiguration {    @Bean    @ConditionalOnMissingBean ( Jedis. class )    public  Jedis Jedis (redisproperties redisproperties) {        return  New  Jedis (Redisproperties.gethost (), Redisproperties.getport (). Intvalue ());}    }

At this point the program is used.

4 "Program use

Add a Dependency package

        <Dependency>            <groupId>Com.lhx.spring</groupId>            <Artifactid>Redis-spring-boot-starter</Artifactid>            <version>1.0.0</version>        </Dependency>
View Code

There are two specific types:

Mode one, add annotations in Redis-spring-boot-starter, import imports Configuration class
@Target (Elementtype.type) @Retention (retentionpolicy.runtime) @ Documented@inherited@autoconfigurationpackage@import (redisautoconfiguration. class ) public @Interface  Enableredis {}

Program use

@EnableRedis @springbootapplication  Public class App {    publicstaticvoidthrows  SQLException {        = Springapplication.run (App.class, args);         = Context.getbean (Jedis.  Class);        Jedis.set ("id", "3333");        System.out.println (Jedis.get ("id"));        Context.close ();    }}
Mode two, spring.factories, configuration configuration class

View the Spring.factories file under Meta-inf in the Spring-boot-autoconfigure-1.5.9.release.jar package

It is common starter resources/META-INF to include the fully qualified name of the Automation configuration class in the Spring.factories file under the project's folder.

Org.springframework.boot.autoconfigure.enableautoconfiguration=com.lhx.spring.redis.redisautoconfiguration

Spring The enableautoconfigurationimportselector in the boot project will automatically view the contents of the Spring.factories file under the corresponding file for each jar and load the classes in it to be configured in the auto-configuration process. The Enableautoconfigurationimportselector is import in the @enableautoconfiguration annotation.

Program use

@SpringBootApplication  Public class App {    publicstaticvoidthrows  SQLException {        = Springapplication.run (App.class, args);         = Context.getbean (Jedis.  Class);        Jedis.set ("id", "4444");        System.out.println (Jedis.get ("id"));        Context.close ();    }}

018-spring Boot Starter Development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.