自定义Spring Boot Starter
2023-09-11 14:16:58 时间
自定义Spring Boot Starter
- pom中添加依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
</dependencies>
- 编写配置类:
@Configuration
@EnableConfigurationProperties(JedisProperties.class)
@ConditionalOnClass(Jedis.class)
public class JedisAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public Jedis jedis(JedisProperties jedisProperties) {
Jedis jedis = new Jedis(jedisProperties.getHost(), jedisProperties.getPort());
jedis.auth(jedisProperties.getPassword());
System.out.println("jedis-----------init");
return jedis;
}
}
- 编写属性文件配置类:
@ConfigurationProperties(prefix = "redis")
public class JedisProperties {
private String host;
private Integer port;
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
}
- META-INF/spring.properties 中添加配置,或者使用@Impoert注解导入
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.aimilin.JedisAutoConfiguration
- 使用:pom导入依赖:
<dependency>
<groupId>com.aimilin</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.0.0</version>
</dependency>
@Component
public class RedisTest {
@Autowired
private Jedis jedis;
public void run() {
jedis.set("hello", "text");
System.out.println("redis ---- text");
}
}
相关文章
- Spring Boot启动时数据库初始化spring.datasource(转)
- Spring Boot 2.x :通过 spring-boot-starter-hbase 集成 HBase
- Spring Boot 开发集成 WebSocket,实现私有即时通信系统
- Spring Boot HTTP over JSON 的错误码异常处理
- maven 创建spring boot 需要的配置[一]
- spring boot:shardingsphere+druid多数据源整合seata分布式事务(spring boot 2.3.3)
- spring boot:使mybatis访问多个druid数据源(spring boot 2.3.2)
- Spring Boot下Bean定义方式及调用方式
- spring boot:spring security实现oauth2授权认证(spring boot 2.3.3)
- spring boot:actuator的安全配置:使用spring security做ip地址限制(spring boot 2.3.2)
- Spring Boot项目搭建易错注意事项
- Spring Boot自定义缓存注解
- 详解Spring Boot 2.X使用缓存@Cacheable代码示例
- Spring 中的如何自定义事件处理(Custom Event)
- Java Spring实现原理研究之Servlet initialization初始化过程
- 让你的Spring Boot工程支持HTTP和HTTPS
- spring boot报FileSizeLimitExceededException异常的解决方法
- 学习Spring Boot:(十四)spring-shiro的密码加密
- 学习Spring Boot:(十一) 自定义装配参数
- Spring boot自定义启动字符画(banner)
- spring boot配置dubbo注意事项
- spring boot + spring security +JWT令牌 +前后端分离--- 心得
- Spring Boot 项目打包问题集锦: jar依赖多出boot-inf 文件夹问题/多环境动态打包/缺少BOOT-INF目录问题等...
- Spring之AOP源码理解,Spring4.3.12.RELEASE版本
- 005-html+js+spring multipart文件上传
- 009-Spring Boot 事件监听、监听器配置与方式、spring、Spring boot内置事件
- SpringBoot实例③Spring Boot 日志配置
- Spring Boot Test介绍
- 【java】Spring Boot --spring boot项目整合xxl-job