1.创建redis-spring-boot-autoconfigure模块,依赖jedis模块

        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>

 

1.创建redis-spring-boot-starter模块,依赖redis-spring-boot-autoconfigure模块

        <dependency>
            <groupId>com.lin</groupId>
            <artifactId>redis-spring-boot-autoconfigure</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

3.在redis-spring-boot-autoconfigure模块中初始化Jedis的Bean,并定义META-INF/spring.factories文件(

@SpringBootApplication>
@EnableAutoConfiguration>
AutoConfigurationImportSelector>
getAutoConfigurationEntry>

META-INF/spring.factories:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.lin.redis.configure.RedisProperties

EnableAutoConfiguration=RedisProperties(自己写的配置类)

4.在测试类模块了引入自定义的redis-starter模块,测试获得jedis的bean,操作bean

<!--自定义的redis的starter-->
        <dependency>
            <groupId>com.lin</groupId>
            <artifactId>redis-spring-boot-starter</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

实现类:

package com.lin.demo01;

import com.lin.redis.configure.RedisAutoConfigure;
import com.lin.redis.configure.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import redis.clients.jedis.Jedis;

//@Import(User.class)
@Import(RedisAutoConfigure.class)
@SpringBootApplication
public class Demo01Application {

    public static void main(String[] args) {

        ConfigurableApplicationContext context = SpringApplication.run(Demo01Application.class, args);
        Jedis jedis = context.getBean(Jedis.class);
        System.out.println(jedis);
        jedis.set("name","lin");
        String name = jedis.get("name");
        System.out.println(name);
//        User bean = context.getBean(User.class);
//        System.out.println(bean);
//        RedisAutoConfigure bean = context.getBean(RedisAutoConfigure.class);
//        System.out.println(bean);

    }
//    @Bean
// public Jedis jedis(){
// return new Jedis("localhost",6379);
// }
}

redis-spring-boot-auticonfigure里的RedisAutoConfigure:

package com.lin.redis.configure;


import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.Jedis;

@Configuration
//@EnableConfigurationProperties(RedisProperties.class)
@ConditionalOnClass(Jedis.class)
public class RedisAutoConfigure {
//@Primary
    @Bean
    @ConditionalOnMissingBean(name="jedis")
    public Jedis jedis(
//            @Qualifier("")
            RedisProperties redisProperties){
        return new Jedis(redisProperties.getHost(), redisProperties.getPort());

    }
}

redis-spring-boot-auticonfigure里的Redis-properties:

package com.lin.redis.configure;

import org.springframework.boot.context.properties.ConfigurationProperties;


@ConfigurationProperties(prefix = "redis")
public class RedisProperties {
    private String host="localhost";
    private int port =6379;


    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;
    }
}

 

posted on 2022-06-01 23:51  阿霖找BUG  阅读(123)  评论(0编辑  收藏  举报