【问题标题】:System.getproperty("spring.profiles.active") always getting Null in JPA Entity ListenersSystem.getproperty("spring.profiles.active") 总是在 JPA 实体侦听器中获取 Null
【发布时间】:2019-06-09 12:58:32
【问题描述】:

我正在尝试使用 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");  

    }

请指教!

【问题讨论】:

  • 它不是系统属性,它是 Spring 环境的一部分。请改用@Value 或自动连接Environment:stackoverflow.com/questions/9267799/…
  • @BackSlash,你是对的。但在 JPA EntityListener @Value 或 Environment 中不起作用。正如@Karol Dowbecki 提到的,我必须明确需要在我的 Listener 类中注入依赖项。

标签: java spring-boot jpa entitylisteners


【解决方案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
  • 谢谢@KarolDowbecki,这个解决方案对我有用。我已将所需的属性放入相应的 application.properties 文件中,并且我有 @Autowired Environment 中的建议 This answer 非常感谢您的指导,非常感谢!
【解决方案2】:

将 Environment 注入您的代码,然后在 Environment 上调用 getActiveProfiles() 方法。这将返回所有活动配置文件的字符串数组。

见

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

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

【讨论】:

    【解决方案3】:

    改为使用 boolean Environment.acceptsProfiles(String ...profiles)。

    Javadoc:

    返回一个或多个给定配置文件是否处于活动状态,或者在没有明确的活动配置文件的情况下,返回一个或多个给定配置文件是否包含在一组默认配置文件中。如果配置文件以“!”开头逻辑是相反的,即如果给定的配置文件不活动,该方法将返回 true。例如, env.acceptsProfiles("p1", "!p2")

    如果配置文件“p1”处于活动状态或“p2”未处于活动状态,将返回 true。

    它合理化了 spring.profiles.active 和 spring.profiles.default 列表。这意味着如果 my-profile 在默认列表中但 !my-profile 在活动列表中,当您询问它是否接受 my-profile 它将返回 false。

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-11-25
        • 1970-01-01
        • 2018-08-23
        • 1970-01-01
        • 1970-01-01
        • 2015-12-15
        • 2010-10-07
        相关资源
        最近更新 更多