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