Is there a way programmatically to get the current active profile within my bean?
            Asked
            
        
        
            Active
            
        
            Viewed 1.7e+01k times
        
    1 Answers
328
            It doesn't matter is your app Boot or just raw Spring. There is just enough to inject org.springframework.core.env.Environment to your bean.
@Autowired
private Environment environment;
....
this.environment.getActiveProfiles();
        pixel
        
- 24,905
 - 36
 - 149
 - 251
 
        Artem Bilan
        
- 113,505
 - 11
 - 91
 - 118
 
- 
                    Thanks. how would you create different property files for different profiles? – rayman Feb 08 '15 at 14:15
 - 
                    10It's definitely the separate SO question. However you can take a look to Boot docs: http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-profile-specific-properties – Artem Bilan Feb 08 '15 at 14:36
 - 
                    1Getting environment as null – Girish Dec 24 '21 at 08:14
 - 
                    same - getting env as null – mangusta Jun 25 '22 at 21:42
 - 
                    3Perhaps the place you’d like to autowire is not a spring bean. Better to ask a new SO question with more info. – Artem Bilan Jun 26 '22 at 02:10