SpringBoot学习 - 自定义starter
Posted Ivyvivid
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot学习 - 自定义starter相关的知识,希望对你有一定的参考价值。
自己开发一个spring boot starter的步骤
1.新建一个项目(全部都基于maven),比如新建一个spring-boot-starter-redis的maven项目
pom.xml:
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 5 <groupId>com.study.spring-boot</groupId> 6 <artifactId>spring-boot-starter-redis</artifactId> 7 <version>1.0.0</version> 8 <packaging>jar</packaging> 9 10 <name>spring-boot-starter-redis</name> 11 <url>http://maven.apache.org</url> 12 13 <properties> 14 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 15 </properties> 16 17 <dependencies> 18 <dependency> 19 <groupId>org.springframework.boot</groupId> 20 <artifactId>spring-boot-starter</artifactId> 21 <version>1.5.3.RELEASE</version> 22 </dependency> 23 <dependency> 24 <groupId>redis.clients</groupId> 25 <artifactId>jedis</artifactId> 26 <version>2.9.0</version> 27 </dependency> 28 <dependency> 29 <groupId>junit</groupId> 30 <artifactId>junit</artifactId> 31 <version>3.8.1</version> 32 <scope>test</scope> 33 </dependency> 34 </dependencies> 35 </project>
2.需要一个配置类,配置类里面需要装配好需要提供出去的类
配置类:
1 package com.study.spring_boot_redis; 2 3 import org.springframework.boot.context.properties.ConfigurationProperties; 4 5 @ConfigurationProperties(prefix="redis") 6 public class RedisProperties { 7 private String host; 8 private Integer port; 9 public String getHost() { 10 return host; 11 } 12 public void setHost(String host) { 13 this.host = host; 14 } 15 public Integer getPort() { 16 return port; 17 } 18 public void setPort(Integer port) { 19 this.port = port; 20 } 21 }
1 package com.study.spring_boot_redis; 2 3 4 import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 5 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 6 import org.springframework.boot.context.properties.EnableConfigurationProperties; 7 import org.springframework.context.annotation.Bean; 8 import org.springframework.context.annotation.Configuration; 9 10 import redis.clients.jedis.Jedis; 11 12 @Configuration 13 @ConditionalOnClass(Jedis.class) 14 @EnableConfigurationProperties(RedisProperties.class) 15 public class RedisAutoConfiguration { 16 @Bean 17 @ConditionalOnMissingBean 18 public Jedis Jedis(RedisProperties redisProperties) { 19 return new Jedis(redisProperties.getHost(),redisProperties.getPort()); 20 21 } 22 }
3.
(1)使用@Enable,使用@Import导入需要装配的类
Enable注解:
1 package com.study.spring_boot_redis; 2 3 import java.lang.annotation.Documented; 4 import java.lang.annotation.ElementType; 5 import java.lang.annotation.Retention; 6 import java.lang.annotation.RetentionPolicy; 7 import java.lang.annotation.Target; 8 9 import org.springframework.context.annotation.Import; 10 11 @Target(ElementType.TYPE) 12 @Retention(RetentionPolicy.RUNTIME) 13 @Documented 14 @Import(RedisAutoConfiguration.class) 15 public @interface EnableRedis { 16 17 }
(2)/META-INF/spring.factories,在org.springframework.boot.autoconfigure.EnableAutoConfiguration配置需要装配的类
/spring-boot-starter-redis/src/main/resources/META-INF/spring.factories:
1 org.springframework.boot.autoconfigure.EnableAutoConfiguration = com.study.spring_boot_redis.RedisAutoConfiguration
真正的项目:springbootstarter(maven项目)
pom.xml:
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 5 <groupId>com.study.springboot</groupId> 6 <artifactId>springboot</artifactId> 7 <version>1.0.0</version> 8 <packaging>jar</packaging> 9 10 <name>springboot</name> 11 <url>http://maven.apache.org</url> 12 13 <properties> 14 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 15 </properties> 16 17 <dependencyManagement> 18 <dependencies> 19 <dependency> 20 <groupId>org.springframework.boot</groupId> 21 <artifactId>spring-boot-dependencies</artifactId> 22 <version>1.5.3.RELEASE</version> 23 <scope>import</scope> 24 <type>pom</type> 25 </dependency> 26 </dependencies> 27 </dependencyManagement> 28 29 <dependencies> 30 <dependency> 31 <groupId>com.study.spring-boot</groupId> 32 <artifactId>spring-boot-starter-redis</artifactId> 33 <version>1.0.0</version> 34 </dependency> 35 <dependency> 36 <groupId>junit</groupId> 37 <artifactId>junit</artifactId> 38 <scope>test</scope> 39 </dependency> 40 </dependencies> 41 </project>
/springbootstarter/src/main/resources/application.properties:
1 redis.host=127.0.0.1 2 redis.port=6379
1 //@EnableRedis 2 @SpringBootApplication 3 public class App { 4 5 public static void main(String[] args) { 6 ConfigurableApplicationContext context = SpringApplication.run(App.class,args); 7 Jedis jedis = context.getBean(Jedis.class); 8 jedis.set("id", "root123"); 9 System.out.println(jedis.get("id")); 10 context.close(); 11 } 12 13 }
以上是关于SpringBoot学习 - 自定义starter的主要内容,如果未能解决你的问题,请参考以下文章
SpringBoot 学习笔记心得自定义Starter启动器