4

I am trying to get Spring active profile in JPA Entity Listener using System.getproperty("spring.profiles.active"). But it is always returning Null profile. However I have checked on server and profile is correctly configured.

I have tried to get Spring active profile using Environment but in the listener, I am unable to @Autowired Environment also.

    @PostUpdate

    public void methodInvoked afterUpdate(Example example){

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

    }

Any guidance please !

Ninja
  • 63
  • 1
  • 1
  • 7
  • It's not a system property, it's part of Spring's environment. Use `@Value` or autowire `Environment` instead: https://stackoverflow.com/questions/9267799/how-do-you-get-current-active-default-environment-profile-programmatically-in-sp – BackSlash Jan 15 '19 at 13:48
  • @BackSlash, You are right. But in JPA EntityListener `@Value` or `Environment` does not work. As @Karol Dowbecki mentioned I have to explicitly need to inject the Dependency in my Listener class. – Ninja Jan 16 '19 at 14:35

5 Answers5

1

You should use Environment bean while injecting it as described in this answer. SpringBeanAutowiringSupport will work if you are building a web application:

@Autowired
private Environment env;

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

}
Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
  • This won't necessarily show the active profiles, it's just a property used to set active profiles at boot time and may not be set, or active profiles might be changed at boot time, for example in an ApplicationContextInitializer – PaulNUK Jan 15 '19 at 14:49
  • Thanks @KarolDowbecki, This solution is working for me. I have put required properties in respective application.properties files and I have `@Autowired Environment` as suggested in [This answer](https://stackoverflow.com/questions/12155632/injecting-a-spring-dependency-into-a-jpa-entitylistener) Thanks a lot for your guidance, Really appreciated! – Ninja Jan 16 '19 at 14:41
1

That's how I got it:

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

  }

The log is:

Currently active profile - test-dev-std

Currently active profile - log

Currently active profile - dev-standalone

true

GtdDev
  • 748
  • 6
  • 14
0

Inject the Environment into your code and then call the getActiveProfiles() method on Environment. This returns an array of Strings of all the active profiles.

See

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

Don't rely on the "spring.profiles.active" as this may not be set - this property is used to set values via a property, and it's value does not reflect which profiles are active, as these may have been set programmatically.

PaulNUK
  • 4,774
  • 2
  • 30
  • 58
0

Use boolean Environment.acceptsProfiles(String ...profiles) instead.

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.

It rationalizes the spring.profiles.active and spring.profiles.default lists. This means that if my-profile is in the default list but !my-profile is in the active list when you ask if it accepts my-profile it will return false.

This is done mostly through the protected AbstractEnvironment.isProfileActive(profile) method which you can use if you make a custom Environment.

0

I found a solution because I needed to know the currently active profile in my entities and it is bad practice to inject bean Environment there, so i registered event listener:

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

It will set property regarding the current profile and you can just use System.getProperty() wherever you want.

Peter S.
  • 470
  • 1
  • 7
  • 17