zoukankan      html  css  js  c++  java
  • 自定义springboot-starter

    参考:
    https://juejin.im/entry/5b447cbbe51d45199566f752
    https://www.baeldung.com/spring-boot-custom-starter

    项目结构

    子模块 mystarter (自定义starter)

    • pom
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>lesson1</artifactId>
            <groupId>com.xh.sb.learn.lesson1</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
    
        <packaging>jar</packaging>
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.xh.sb.learn.lesson1.mystarter</groupId>
        <artifactId>mystarter</artifactId>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
                <version>2.1.4.RELEASE</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <version>2.1.4.RELEASE</version>
                <optional>true</optional>
            </dependency>
    
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>2.9.0</version>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

    注意:

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <version>2.1.4.RELEASE</version>
                <optional>true</optional>
            </dependency>
    

    不加会在使用@ConfigurationProperties 报错

    spring boot Configuration Annotation Proessor not found in classpath

    • RedisProperties.java
    @ConfigurationProperties(prefix = "redis.starter")
    public class RedisProperties {
        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;
        }
    }
    
    
    • RedisAutoConfiguration.java
    @Configuration
    @ConditionalOnClass(Jedis.class)    // 存在Jedis这个类才装配当前类
    @EnableConfigurationProperties(RedisProperties.class)
    public class RedisAutoConfiguration {
    
    
        @Bean("jedis")
        @ConditionalOnMissingBean   // 没有Jedis这个类才进行装配
        public Jedis jedis(RedisProperties redisProperties) {
            return new Jedis(redisProperties.getHost(), redisProperties.getPort());
        }
    }
    
    
    • spring.factories
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=
    com.xh.sb.learn.lesson1.mystarter.config.RedisAutoConfiguration
    

    注意:

    必须加

    子模块 test4starter

    • pom
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>lesson1</artifactId>
            <groupId>com.xh.sb.learn.lesson1</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.xh.sb.learn.lesson1.test4starter</groupId>
        <artifactId>test4starter</artifactId>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>2.1.4.RELEASE</version>
            </dependency>
    
            <dependency>
                <groupId>com.xh.sb.learn.lesson1.mystarter</groupId>
                <artifactId>mystarter</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>
    
    • Test4starterApplication.java
    @SpringBootApplication
    @RestController
    @RequestMapping("/app")
    public class Test4starterApplication {
        @Autowired
        private Jedis jedis;
    
        public static void main(String[] args) {
            SpringApplication.run(Test4starterApplication.class, args);
        }
    
        @GetMapping("/name")
        public String name() {
            String result = jedis.get("name");
            return result;
        }
    
        @PostMapping("/name/{name}")
        public void name(@PathVariable("name") String name) {
            jedis.setex("name", 1000, name);
        }
    }
    
    
    • application.properties
    redis.starter.host=127.0.0.1
    redis.starter.port=6379
    

    启动redis

    ./redis-server

    插入和读取数据


     

  • 相关阅读:
    【百度搜索研发部】以求医为例谈搜索引擎排序算法的基础原理(转)
    TF-IDF与余弦相似性的应用(三):自动摘要
    TF-IDF与余弦相似性的应用(一):自动提取关键词
    TF-IDF与余弦相似性的应用(二):找出相似文章
    技术向:一文读懂卷积神经网络CNN(转)
    [透析] 卷积神经网络CNN究竟是怎样一步一步工作的?(转)
    像素间的基本关系-距离(转)
    Swift学习笔记-字符串和字符(Strings and Characters)-比较字符串 (Comparing Strings)
    Swift学习笔记-基本运算符(Basic Operators)-空合运算符(Nil Coalescing Operator)
    Swift学习笔记-基本运算符(Basic Operators)-求余运算符
  • 原文地址:https://www.cnblogs.com/lanqie/p/11172293.html
Copyright © 2011-2022 走看看