您好,登錄后才能下訂單哦!
本篇內容介紹了“怎么自定義Java Spring-boot-starter”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
<groupId>com.bozhon</groupId> <artifactId>redis-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> <name>redis-spring-boot-starter</name> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- 想要讓自定義配置項有提示,導入這個包,在項目編譯后會生成元數據,同樣是在META-INF目錄下--> <!-- 會自動生成spring-configuration-metadata.json文件。--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <version>2.1.1.RELEASE</version> <optional>true</optional> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> </dependencies>
@ConfigurationProperties(prefix = "redis") public class JedisProperties { private String host; private int port; public String getHost() { return host; } public void setHost(String host) { this.host = host; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } }
@Configuration @ConditionalOnClass(Jedis.class) @EnableConfigurationProperties(JedisProperties.class) public class JedisAutoConfiguration { @Bean @ConditionalOnMissingBean public Jedis jedis(JedisProperties p){ return new Jedis(p.getHost(),p.getPort()); } }
在src/main/resources下增加以下內容
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.bozhon.config.JedisAutoConfiguration
<dependency> <groupId>com.bozhon</groupId> <artifactId>redis-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
application.properties
redis.host=127.0.0.1 redis.port=6379
@RestController public class RedisController { @Autowired private Jedis jedis; @GetMapping("/info") public String info() { jedis.set("test","11111111"); return "success"; } }
“怎么自定義Java Spring-boot-starter”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。