145

Is there a way programmatically to get the current active profile within my bean?

pixel
  • 23,075
  • 32
  • 129
  • 220
rayman
  • 19,638
  • 42
  • 142
  • 242

1 Answers1

287

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
  • 23,075
  • 32
  • 129
  • 220
Artem Bilan
  • 102,070
  • 11
  • 81
  • 107
  • Thanks. how would you create different property files for different profiles? – rayman Feb 08 '15 at 14:15
  • 8
    It'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
  • Getting environment as null – Girish Dec 24 '21 at 08:14