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

    插入和读取数据


     

  • 相关阅读:
    linux系统sed命令输出匹配字符的行及其后若干行
    linux系统中统计每一行的字符数
    linux系统中sed复合命令
    [转载]J2EE程序员应该掌握的Linux系统的知识
    [转载]Linuz常用命令(1)
    [转载]J2EE程序员应该掌握的Linux系统的知识
    [转载]Linuz常用命令(1)
    [转载]Oracle常用函数大全
    [转载]Linuz常用命令(1)
    [转载]Linux常用命令(2)
  • 原文地址:https://www.cnblogs.com/lanqie/p/11172293.html
Copyright © 2011-2022 走看看