'System.getproperty("spring.profiles.active") always getting Null in JPA Entity Listeners
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 !
Solution 1:[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();
}
Solution 2:[2]
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
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.
Solution 3:[3]
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.
Solution 4:[4]
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.
Solution 5:[5]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | PaulNUK |
| Solution 3 | Michael O'Cleirigh |
| Solution 4 | Peter S. |
| Solution 5 | GtdDev |
