System.getproperty("spring.profiles.active") always getting Null in JPA Entity Listeners
Asked Answered
G

5

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 !

Gardiner answered 15/1, 2019 at 13:45 Comment(2)
It's not a system property, it's part of Spring's environment. Use @Value or autowire Environment instead: #9268299Calendar
@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.Gardiner
S
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();

}
Salerno answered 15/1, 2019 at 13:52 Comment(2)
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 ApplicationContextInitializerNabors
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 Thanks a lot for your guidance, Really appreciated!Gardiner
E
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

Eugeniaeugenics answered 2/4, 2022 at 17:39 Comment(0)
N
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.

Nabors answered 15/1, 2019 at 14:47 Comment(0)
M
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.

Menander answered 15/1, 2021 at 22:10 Comment(0)
C
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.

Copaiba answered 3/1, 2022 at 14:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.