forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fallback beans in Spring * Fallback beans in Spring * Fallback beans in Spring
- Loading branch information
Showing
9 changed files
with
154 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...-boot-modules/spring-boot-3/src/main/java/com/baeldung/fallback/MessagingApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.baeldung.fallback; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
|
||
@SpringBootApplication | ||
public class MessagingApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(MessagingApplication.class, args); | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
...ing-boot-3/src/main/java/com/baeldung/fallback/messaging/DevelopmentMessagingService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.baeldung.fallback.messaging; | ||
|
||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@Profile("!test") | ||
public class DevelopmentMessagingService implements MessagingService { | ||
|
||
@Override | ||
public void sendMessage(String text) { | ||
// implementation in development environment | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...spring-boot-3/src/main/java/com/baeldung/fallback/messaging/FallbackMessagingService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.baeldung.fallback.messaging; | ||
|
||
import org.springframework.context.annotation.Fallback; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@Fallback | ||
public class FallbackMessagingService implements MessagingService { | ||
|
||
@Override | ||
public void sendMessage(String text) { | ||
// fallback implementation | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...modules/spring-boot-3/src/main/java/com/baeldung/fallback/messaging/MessagingService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.baeldung.fallback.messaging; | ||
|
||
public interface MessagingService { | ||
void sendMessage(String text); | ||
} |
16 changes: 16 additions & 0 deletions
16
...ring-boot-3/src/main/java/com/baeldung/fallback/messaging/ProductionMessagingService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.baeldung.fallback.messaging; | ||
|
||
import org.springframework.context.annotation.Primary; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@Profile("production") | ||
@Primary | ||
public class ProductionMessagingService implements MessagingService { | ||
|
||
@Override | ||
public void sendMessage(String text) { | ||
// implementation in production environment | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...pring-boot-3/src/test/java/com/baeldung/fallback/DevelopmentMessagingServiceUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.baeldung.fallback; | ||
|
||
import com.baeldung.fallback.messaging.DevelopmentMessagingService; | ||
import com.baeldung.fallback.messaging.FallbackMessagingService; | ||
import com.baeldung.fallback.messaging.MessagingService; | ||
import com.baeldung.fallback.messaging.ProductionMessagingService; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(classes = {FallbackMessagingService.class, DevelopmentMessagingService.class, ProductionMessagingService.class}) | ||
public class DevelopmentMessagingServiceUnitTest { | ||
|
||
@Autowired | ||
private MessagingService messagingService; | ||
|
||
@Test | ||
public void givenNoProfile_whenSendMessage_thenDevelopmentMessagingService() { | ||
assertEquals(messagingService.getClass(), DevelopmentMessagingService.class); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...s/spring-boot-3/src/test/java/com/baeldung/fallback/FallbackMessagingServiceUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.baeldung.fallback; | ||
|
||
import com.baeldung.fallback.messaging.DevelopmentMessagingService; | ||
import com.baeldung.fallback.messaging.FallbackMessagingService; | ||
import com.baeldung.fallback.messaging.MessagingService; | ||
import com.baeldung.fallback.messaging.ProductionMessagingService; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(classes = {FallbackMessagingService.class, DevelopmentMessagingService.class, ProductionMessagingService.class}) | ||
@ActiveProfiles("test") | ||
public class FallbackMessagingServiceUnitTest { | ||
|
||
@Autowired | ||
private MessagingService messagingService; | ||
|
||
@Test | ||
public void givenTestProfile_whenSendMessage_thenFallbackMessagingService() { | ||
assertEquals(messagingService.getClass(), FallbackMessagingService.class); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...spring-boot-3/src/test/java/com/baeldung/fallback/ProductionMessagingServiceUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.baeldung.fallback; | ||
|
||
import com.baeldung.fallback.messaging.DevelopmentMessagingService; | ||
import com.baeldung.fallback.messaging.FallbackMessagingService; | ||
import com.baeldung.fallback.messaging.MessagingService; | ||
import com.baeldung.fallback.messaging.ProductionMessagingService; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(classes = {FallbackMessagingService.class, DevelopmentMessagingService.class, ProductionMessagingService.class}) | ||
@ActiveProfiles("production") | ||
public class ProductionMessagingServiceUnitTest { | ||
|
||
@Autowired | ||
private MessagingService messagingService; | ||
|
||
@Test | ||
public void givenProductionProfile_whenSendMessage_thenProductionMessagingService() { | ||
assertEquals(messagingService.getClass(), ProductionMessagingService.class); | ||
} | ||
} |