springboot starter (3-1)

自定义starter

maven 依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<!--  能够给开发者引入该jar包后 有一定提示 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
</dependency>

核心代码

@ConfigurationProperties(prefix = "mayikt")
public class TokenProperties {
    private String tokenRedisHost;
    private String tokenRedisPwd;
}

@Configuration
@EnableConfigurationProperties(TokenProperties.class)
public class TokenAutoConfiguration {
    @Bean
    public TokenService tokenService() {
        return new TokenService();
    }
}

public class TokenService {
    @Autowired
    private TokenProperties tokenProperties;

    public String getToken() {
        System.out.println("模拟生成token");
        return tokenProperties.getTokenRedisHost() + "," + tokenProperties.getTokenRedisPwd();
    }
}

SPI
META-INF/spring.factories 内容:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.mayikt.utils.TokenAutoConfiguration
SPI 是 Java 提供的一种服务加载方式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于 Spring Boot Starter Data 整合 Elasticsearch,可以按照以下步骤进行: 1. 在 pom.xml 文件中添加 Elasticsearch 的依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> ``` 2. 在 application.properties 文件中配置 Elasticsearch 的连接信息: ```properties spring.data.elasticsearch.cluster-nodes=localhost:9300 spring.data.elasticsearch.cluster-name=my-application ``` 其中,cluster-nodes 是 Elasticsearch 集群节点的地址,cluster-name 是集群名称。 3. 创建一个 ElasticsearchRepository 接口来定义 Elasticsearch 的操作: ```java public interface BookRepository extends ElasticsearchRepository<Book, String> { } ``` 其中,Book 是实体类,String 是实体类主键的数据类型。 4. 在需要使用 Elasticsearch 的地方注入 ElasticsearchRepository 接口,并使用它进行数据操作: ```java @Autowired private BookRepository bookRepository; public void saveBook(Book book) { bookRepository.save(book); } public List<Book> findBooksByAuthor(String author) { return bookRepository.findByAuthor(author); } ``` 这里的 save 方法是保存数据,findByAuthor 方法是按照作者查询数据。 以上就是 Spring Boot Starter Data 整合 Elasticsearch 的基本步骤。需要注意的是,这里使用的是 ElasticsearchRepository 接口,它提供了一些常用的操作方法,如果需要更多的操作,可以使用 ElasticsearchTemplate 类来进行操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值