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,而且相互之间的数据是隔离开的,互不影响。

 

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Spring Boot,我们经常使用注解来配置应用程序的各种属性。如果一个注解有一个boolean类型的属性,我们可以使用以下两种方法动态地指定该属性的值: 1. 使用SpEL表达式:在注解使用SpEL表达式,这样属性的值将在应用程序启动时动态计算。例如: ```java @Autowired @Value("#{systemProperties['myBooleanProperty'] ?: true}") private boolean myBooleanProperty; ``` 上述代码,@Value注解使用了SpEL表达式来设置myBooleanProperty属性的值。表达式使用了systemProperties对象,该对象代表了系统属性。如果系统属性myBooleanProperty存在,则使用该属性值。否则,使用默认值true。 2. 使用@ConfigurationProperties注解:如果一个注解有多个boolean类型的属性,或者我们想将多个属性组合在一起,可以使用@ConfigurationProperties注解。该注解注解的所有属性映射到一个JavaBean。然后,我们可以在应用程序启动时从属性文件或环境变量加载该JavaBean,并使用其属性。例如: ```java @Component @ConfigurationProperties(prefix = "myProperties") public class MyProperties { private boolean myBooleanProperty; public boolean isMyBooleanProperty() { return myBooleanProperty; } public void setMyBooleanProperty(boolean myBooleanProperty) { this.myBooleanProperty = myBooleanProperty; } } ``` 上述代码,@ConfigurationProperties注解将myProperties前缀的属性映射到MyProperties类。我们可以将该类注入到其他类,并使用其属性。例如: ```java @Autowired private MyProperties myProperties; public void doSomething() { boolean myBooleanProperty = myProperties.isMyBooleanProperty(); // ... } ``` 在上述代码,我们使用@Autowired注解将MyProperties类注入到当前类,并使用其属性
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值