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中智能提示属性,暂时还不知道,欢迎留言告知方案。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值