日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

springboot 注解动态赋值_java springboot动态给注解属性参数赋值

發布時間:2024/9/19 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot 注解动态赋值_java springboot动态给注解属性参数赋值 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

場景:

最近做了一個系統,使用Elasticsearch做了全文搜索,系統分為開發環境和正式環境,正式環境供公司內部使用,因為服務器資源不太充裕,決定開發環境和正式環境共用一個Elasticsearch,不同環境僅以索引名稱(indexName)進行區分。即:一個Elasticsearch供兩個系統使用。

實現:

1.在配置文件里定義索引名稱

開發環境的配置:application-dev.yml

spring:

data:

elasticsearch:

repositories:

enabled: true

cluster-nodes: 172.16.11.109:9300

cluster-name: elasticsearch

article-index-name: article

正式環境的配置:application-prod.yml

spring:

data:

elasticsearch:

repositories:

enabled: true

cluster-nodes: 172.16.11.109:9300

cluster-name: elasticsearch

article-index-name: article-prod

通過以上配置文件可以看出,兩個環境只有索引名稱是不一樣的(索引名稱article-index-name,屬于自己定義的配置項,這里為了便于統一管理,將其與Elasticsearch的配置放在一起)。

2.編寫一個配置類,用于讀取配置文件中索引名稱的值

import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

@Configuration

public class ArticleIndexConfig{

@Value("${spring.data.elasticsearch.article-index-name}")

private String articleIndexName;

@Bean

public String articleIndexName(){

return articleIndexName;

}

}

3.在Elasticsearch的實體類的注解上使用

import org.springframework.data.annotation.Id;

import org.springframework.data.elasticsearch.annotations.Document;

import org.springframework.data.elasticsearch.annotations.Field;

import org.springframework.data.elasticsearch.annotations.FieldType;

import java.io.Serializable;

@Document(indexName = "#{@articleIndexName}", type = "java")

public class ArticleModel implements Serializable{

private static final long seriaVersionUID = 6320548148250372656L;

@Id

private String rowId;

@Field(type = FieldType.text)

private String title;

@Field(type = FieldType.text)

private String content;

//getter和setter省略

//...

}

4.經過以上的動作,兩個環境(開發環境和正式環境)就可以共用一個Elasticsearch,而且相互之間的數據是隔離開的,互不影響。

總結

以上是生活随笔為你收集整理的springboot 注解动态赋值_java springboot动态给注解属性参数赋值的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。