springboot自定义starter

 

  1. 新创建一个gradle项目my-starter-redis (参考官网或者按需参考: https://blog.csdn.net/qq_25337221/article/details/111173810
  2. 编写主类
    实际上这个类可以不要,如果不要,请在build.gradle加入如下内容,否则build时报错:
    bootJar {
        enabled = false
    }
    
    jar {
        enabled = true
    }
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    /**
     * docker安装redis: 
     * 参考链接: https://www.runoob.com/docker/docker-install-redis.html
     * 拉取镜像: docker pull redis
     * 运行镜像: docker run -itd --name redis-test -p 6379:6379 redis
     * 依赖:
     *  compileJava.dependsOn(processResources)
        dependencies {
    	  compile 'org.springframework.boot:spring-boot-configuration-processor'
    	  compile 'org.redisson:redisson:3.11.1'
    	  compile 'org.springframework.boot:spring-boot-starter'
    	  annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"	
        }
     */
    @SpringBootApplication
    public class StarterApplication {
    	public static void main(String[] args) {
    		SpringApplication.run(StarterApplication.class, args);
    	}
    }
    

     

  3. 定义属性类:
import org.springframework.boot.context.properties.ConfigurationProperties;

import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
@ConfigurationProperties(prefix="gp.redisson")
public class RedissonProperties {
	private String host="localhost";
	private String password;
	private int port=6379;
	private int timeout;
	private boolean ssl;
	
}

4. 定义配置类:

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

@Configuration
@ConditionalOnClass(Redisson.class)
@EnableConfigurationProperties(RedissonProperties.class)
public class RedissonAutoConfiguation {
	@Bean
	RedissonClient redissonClient(RedissonProperties redissonProperties) {
		Config config=new Config();
		String prefix="redis://";
		if (redissonProperties.isSsl()) {
			prefix="rediss://";
		}
		SingleServerConfig singleServerConfig=config.useSingleServer()
				.setAddress(prefix+redissonProperties.getHost()+":"+redissonProperties.getPort())
				.setConnectTimeout(redissonProperties.getTimeout());
		if (!StringUtils.isEmpty(redissonProperties.getPassword())) {
			singleServerConfig.setPassword(redissonProperties.getPassword());
		}
		return Redisson.create(config);
	}
}

5.  在src/main/resource下创建META-INF/spring.factories文件,写入

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.config.RedissonAutoConfiguation(该类地址是实际情况地址)

6. 新建一个新项目use-my-starter-redis,编写主类如下(注意,依赖也可以将上面build成jar包引入):

 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
 	依赖:
	compile project("my-starter-redis")
	compile 'org.springframework.boot:spring-boot-starter-web'
 */
@SpringBootApplication
public class UseMyStarterRedisApplication {
	public static void main(String[] args) {
		SpringApplication.run(UseMyStarterRedisApplication.class, args);
	}
}

7. 编写一个简单的controller类:

import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Controller {
	@Autowired
	private RedissonClient client;
	@GetMapping("/hello")
	public Object insert(String hello) {
		 RBucket<Object> bucket = client.getBucket("demo");
		 bucket.set("hello");
		return bucket.get();
	}
}

8. yaml文件配置:

server:
  port: 8888
gp:
  redisson:
    host: localhost
  port: 6379

9. 此时浏览器输入localhost:8888/hello,"hello"成功输出则表明整合成功。但是如何让yaml文件在eclipse中智能提示属性,暂时还不知道,欢迎留言告知方案。

 

上一篇:Redisson分布式锁使用


下一篇:SpringBoot整合Redisson(单机)