Spring IOC-Profile
What is IOC Profile?
Spring profile is manage bean group.
Can see current active profile using getEnvironment method in ApplicationContext.
- How define profile.
- Define in class
- @Configuration @Profile(“test”)
- @Component @Profile(“test”)
- Define in method
- @Bean @Profile(“test”)
- Define in class
First of all, make interface.
1 | public interface ProRepository { |
Also make TestProRepository.
1 |
|
And can use with Configuration class like below.
1 |
|
Next, should write configuration in VM setting.
Let’s Execute code.
Once get Environment from ApplicationContext, use getActiveProfiles which has current active profile and getDefaultProfiles which just default.
1 |
|
Result like below.
1 | [test] |
But don’t have to create configuration class, there is more convenient way.
Just write ‘@Profile(“test”)’ in class.
1 |
|
And create another class for how to use profile.
1 |
|
As like above code, can write ‘@Profile(“!test”)’, it means that can write some operator in Profile annotation.
- Profile Operator
- ! (not)
- & (and)
- | (or)
Current configuration is like ‘-Dspring.profiles.active=”test” ‘, so the expected result like below.
1 | [test] |
If change VM configure ‘’-Dspring.profiles.active=”test222” ‘ ‘,so the expected result like below.
1 | [test22] |