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");  

    }

请多指教!

如果您正在构建 Web 应用程序,您应该使用 Environment bean while injecting it as described in this answer. SpringBeanAutowiringSupport

@Autowired
private Environment env;

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

}

将环境注入您的代码,然后对环境调用 getActiveProfiles() 方法。 returns 所有活动配置文件的字符串数组。

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

不要依赖 "spring.profiles.active",因为它可能未设置 - 此 属性 用于通过 属性 设置值,它的值不反映哪些配置文件处于活动状态,因为这些可能已通过编程设置。

改用布尔值 Environment.acceptsProfiles(String ...profiles)。

Javadoc:

Return whether one or more of the given profiles is active or, in the case of no explicit active profiles, whether one or more of the given profiles is included in the set of default profiles. If a profile begins with '!' the logic is inverted, i.e. the method will return true if the given profile is not active. For example, env.acceptsProfiles("p1", "!p2")

will return true if profile 'p1' is active or 'p2' is not active.

它合理化了 spring.profiles.activespring.profiles.default 列表。这意味着如果 my-profile 在默认列表中但 !my-profile 在您询问它是否接受 my-profile 它将 return false.

这主要是通过受保护的 AbstractEnvironment.isProfileActive(profile) 方法完成的,您可以在创建自定义环境时使用该方法。

我找到了一个解决方案,因为我需要知道我的实体中当前活动的配置文件,并且在那里注入 bean 环境是不好的做法,所以我注册了事件侦听器:

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));

  }

日志是:

Currently active profile - test-dev-std

Currently active profile - log

Currently active profile - dev-standalone

true