Skip to content

Commit 22d3389

Browse files
authored
[impr-spring-profile] profile combinations (#18244)
* [impr-spring-profile] profile combinations * [impr-spring-profile] add code examples used in article * [impr-spring-profile] add tests
1 parent 782f78a commit 22d3389

8 files changed

+142
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.profiles;
2+
3+
import org.springframework.context.annotation.Profile;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component
7+
@Profile("!a & !b & !c")
8+
public class NoneOfThreeProfilesComponent {
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.profiles;
2+
3+
import org.springframework.context.annotation.Profile;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component
7+
@Profile("!dev & !production & !mysql & !tomcat")
8+
public class SpecialDatasourceConfig implements DatasourceConfig {
9+
10+
@Override
11+
public void setup() {
12+
System.out.println("Setting up a very special datasource. ");
13+
}
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.profiles;
2+
3+
import org.springframework.context.annotation.Profile;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component
7+
@Profile("a & b & c")
8+
public class ThreeProfilesComponent {
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.profiles;
2+
3+
import org.springframework.context.annotation.Profile;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component
7+
@Profile("tomcat & mysql")
8+
public class TomcatAndMySqlDatasourceConfig implements DatasourceConfig {
9+
10+
@Override
11+
public void setup() {
12+
System.out.println("Setting up MySql datasource for Tomcat environment. ");
13+
}
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.profiles;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.test.context.ActiveProfiles;
8+
import org.springframework.test.context.ContextConfiguration;
9+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10+
import org.springframework.test.context.support.AnnotationConfigContextLoader;
11+
12+
@RunWith(SpringJUnit4ClassRunner.class)
13+
@ActiveProfiles({"a","b","c"})
14+
@ContextConfiguration(classes = { SpringProfilesConfig.class }, loader = AnnotationConfigContextLoader.class)
15+
public class ABCProfilesWithAnnotationIntegrationTest {
16+
@Autowired
17+
ThreeProfilesComponent threeProfilesComponent;
18+
19+
@Test
20+
public void testSpringProfiles() {
21+
Assert.assertNotNull(threeProfilesComponent);
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.profiles;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.test.context.ActiveProfiles;
8+
import org.springframework.test.context.ContextConfiguration;
9+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10+
import org.springframework.test.context.support.AnnotationConfigContextLoader;
11+
12+
@RunWith(SpringJUnit4ClassRunner.class)
13+
@ActiveProfiles("dev")
14+
@ContextConfiguration(classes = { SpringProfilesConfig.class }, loader = AnnotationConfigContextLoader.class)
15+
public class NoneOfABCProfileWithAnnotationIntegrationTest {
16+
@Autowired
17+
NoneOfThreeProfilesComponent noneOfThreeProfilesComponent;
18+
19+
@Test
20+
public void testSpringProfiles() {
21+
Assert.assertNotNull(noneOfThreeProfilesComponent);
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.profiles;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.test.context.ActiveProfiles;
8+
import org.springframework.test.context.ContextConfiguration;
9+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10+
import org.springframework.test.context.support.AnnotationConfigContextLoader;
11+
12+
@RunWith(SpringJUnit4ClassRunner.class)
13+
@ActiveProfiles("something-very-special")
14+
@ContextConfiguration(classes = { SpringProfilesConfig.class }, loader = AnnotationConfigContextLoader.class)
15+
public class SpecialProfileWithAnnotationIntegrationTest {
16+
@Autowired
17+
DatasourceConfig datasourceConfig;
18+
19+
@Test
20+
public void testSpringProfiles() {
21+
Assert.assertTrue(datasourceConfig instanceof SpecialDatasourceConfig);
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.profiles;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.test.context.ActiveProfiles;
8+
import org.springframework.test.context.ContextConfiguration;
9+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10+
import org.springframework.test.context.support.AnnotationConfigContextLoader;
11+
12+
@RunWith(SpringJUnit4ClassRunner.class)
13+
@ActiveProfiles({"tomcat", "mysql"})
14+
@ContextConfiguration(classes = { SpringProfilesConfig.class }, loader = AnnotationConfigContextLoader.class)
15+
public class TomcatAndMySqlProfileWithAnnotationIntegrationTest {
16+
@Autowired
17+
DatasourceConfig datasourceConfig;
18+
19+
@Test
20+
public void testSpringProfiles() {
21+
Assert.assertTrue(datasourceConfig instanceof TomcatAndMySqlDatasourceConfig);
22+
}
23+
}

0 commit comments

Comments
 (0)