簡體   English   中英

System.getproperty("spring.profiles.active") 始終在 JPA 實體監聽器中獲取 Null

[英]System.getproperty("spring.profiles.active") always getting Null in JPA Entity Listeners

我正在嘗試使用System.getproperty("spring.profiles.active")JPA實體偵聽器中獲取 Spring 活動配置文件。 但它總是返回 Null 配置文件。 但是我已經檢查了服務器並且配置文件配置正確。

我嘗試使用 Environment 獲取 Spring 活動配置文件,但在偵聽器中,我也無法@Autowired Environment。

    @PostUpdate

    public void methodInvoked afterUpdate(Example example){

    String activeProfile = System.getproperty("spring.profiles.active");  

    }

請任何指導!

您應該在注入時使用Environment bean,如本答案所述 如果您正在構建 Web 應用程序, SpringBeanAutowiringSupport將起作用:

@Autowired
private Environment env;

@PostUpdate
public void methodInvoked afterUpdate(Example example) {
  SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
  String[] activeProfile = env.getActiveProfiles();

}

將 Environment 注入您的代碼,然后在 Environment 上調用 getActiveProfiles() 方法。 這將返回所有活動配置文件的字符串數組。

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/env/Environment.html#getActiveProfiles--

不要依賴“spring.profiles.active”,因為它可能沒有設置——這個屬性用於通過屬性設置值,它的值不反映哪些配置文件是活動的,因為這些可能已經以編程方式設置。

使用 boolean Environment.acceptsProfiles(String ...profiles) 代替。

Javadoc:

返回一個或多個給定的配置文件是否處於活動狀態,或者在沒有明確的活動配置文件的情況下,一個或多個給定的配置文件是否包含在一組默認配置文件中。 如果配置文件以 '!' 開頭邏輯被反轉,即如果給定的配置文件未激活,該方法將返回 true。 例如, env.acceptsProfiles("p1", "!p2")

如果配置文件 'p1' 處於活動狀態或 'p2' 未處於活動狀態,則將返回 true。

它使spring.profiles.activespring.profiles.default列表合理化。 這意味着如果my-profile在默認列表中但!my-profile在活動列表中,當您詢問它是否接受my-profile 時,它將返回false

這主要是通過受保護的 AbstractEnvironment.isProfileActive(profile) 方法完成的,如果您創建自定義環境,您可以使用該方法。

我找到了一個解決方案,因為我需要知道我的實體中當前活動的配置文件,並且在那里注入 bean Environment 是不好的做法,所以我注冊了事件偵聽器:

public class PropertySettingListener {

    private final Environment environment;

    @Autowired
    public PropertySettingListener(Environment environment) {
        this.environment = environment;
    }

    @EventListener
    public void onApplicationEvent(ContextRefreshedEvent event) {

        String[] profiles = environment.getActiveProfiles();
        for (String profile : profiles) {
            if ("springldaptest".equals(profile)) {
                System.setProperty("ldap.profiles.active", profile);
            }
        }
    }
}

它將設置有關當前配置文件的屬性,您可以在任何需要的地方使用 System.getProperty()。

這就是我得到它的方式:

  @Autowired
  private Environment environment;
  
  public void getActiveProfiles() {
    getActiveProfiles();
    System.out.println((isProfileActive("test-dev-std")));
  }

  private void getActiveProfiles() {

    for (String profileName : environment.getActiveProfiles()) {
      System.out.println("Currently active profile - " + profileName);
    }
  }

  private boolean isProfileActive(String profile) {

    final String[] profiles = environment.getActiveProfiles();

    return Arrays.stream(profiles).anyMatch(str -> str.equals(profile));

  }

日志是:

當前活動的配置文件 - test-dev-std

當前活動的配置文件 - 日志

當前活動的配置文件 - dev-standalone

真的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM