在JPA实体监听器中,System.getproperty("spring.profiles.active")始终获取为Null

4

我正在尝试在JPA实体监听器中获取Spring活动配置文件,使用System.getproperty("spring.profiles.active")。但它总是返回Null配置文件。但是,我已经在服务器上检查并正确配置了配置文件。

我尝试使用Environment获取Spring活动配置文件,但在监听器中,我无法@Autowired Environment。

    @PostUpdate

    public void methodInvoked afterUpdate(Example example){

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

    }

请给予指导!

这不是系统属性,而是Spring环境的一部分。请使用@Value或自动装配Environment:https://dev59.com/l2ox5IYBdhLWcg3wVS8J - BackSlash
@BackSlash,你是对的。但在JPA EntityListener中,“@Value”或“Environment”不起作用。就像@Karol Dowbecki提到的那样,我必须明确在我的Listener类中注入依赖项。 - Ninja
5个回答

1

这就是我得到它的方式:

  @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

当前活动配置文件 - log

当前活动配置文件 - dev-standalone

true


1
在注入时,应使用Environment bean,如this answer所述。如果您正在构建Web应用程序,则SpringBeanAutowiringSupport将起作用。
@Autowired
private Environment env;

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

}

这并不一定会显示活动配置文件,它只是一个用于在启动时设置活动配置文件的属性,可能没有设置,或者活动配置文件可能会在启动时更改,例如在ApplicationContextInitializer中。 - PaulNUK
谢谢@KarolDowbecki,这个解决方案对我很有效。我已经将所需的属性放在了相应的application.properties文件中,并像此答案中建议的那样使用了@Autowired Environment。非常感谢您的指导,真的很感激! - Ninja

0
将环境注入到您的代码中,然后在环境上调用getActiveProfiles()方法。这将返回一个字符串数组,其中包含所有活动配置文件。
请参见。

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

不要依赖于"spring.profiles.active",因为它可能没有被设置 - 此属性用于通过属性设置值,其值不反映哪些配置文件是活动的,因为这些可能已经以编程方式设置。

0

请使用布尔值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)方法完成的,如果您制作自定义环境,则可以使用该方法。


0

我找到了一个解决方案,因为我需要知道我的实体中当前活动的配置文件,并且在那里注入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()。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接