Skip to content

Commit

Permalink
Fallback beans (eugenp#17357)
Browse files Browse the repository at this point in the history
* Fallback beans in Spring

* Fallback beans in Spring

* Fallback beans in Spring
  • Loading branch information
abh1navv authored Aug 20, 2024
1 parent dd43432 commit 3dc5634
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 1 deletion.
10 changes: 9 additions & 1 deletion spring-boot-modules/spring-boot-3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,17 @@
<maven-surefire-plugin.version>3.0.0-M7</maven-surefire-plugin.version>
<start-class>com.baeldung.sample.TodoApplication</start-class>
<mockserver.version>5.14.0</mockserver.version>
<spring-boot.version>3.2.4</spring-boot.version>
<spring-boot.version>3.4.0-M1</spring-boot.version>
<lombok-mapstruct-binding.version>0.2.0</lombok-mapstruct-binding.version>
<springdoc-openapi-webmvc-ui.version>2.5.0</springdoc-openapi-webmvc-ui.version>
</properties>

<repositories>
<repository>
<id>spring-milestone</id>
<name>Spring Milestone</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>

</project>
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);
}

}
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
}
}
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
}
}
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);
}
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
}
}
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);
}
}
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);
}
}
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);
}
}

0 comments on commit 3dc5634

Please sign in to comment.