first commit
This commit is contained in:
parent
2bad67ff38
commit
eac28eb90f
|
@ -0,0 +1,12 @@
|
|||
package com.baeldung.conditionalonproperty;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class NotificationApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(NotificationApplication.class, args);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.conditionalonproperty.config;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.baeldung.conditionalonproperty.service.EmailNotification;
|
||||
import com.baeldung.conditionalonproperty.service.NotificationSender;
|
||||
import com.baeldung.conditionalonproperty.service.SmsNotification;
|
||||
|
||||
@Configuration
|
||||
public class NotificationConfig {
|
||||
|
||||
@Bean(name = "emailNotification")
|
||||
@ConditionalOnProperty(prefix = "notification", name = "service", havingValue = "email")
|
||||
public NotificationSender notificationSender() {
|
||||
return new EmailNotification();
|
||||
}
|
||||
|
||||
@Bean(name = "smsNotification")
|
||||
@ConditionalOnProperty(prefix = "notification", name = "service", havingValue = "sms")
|
||||
public NotificationSender notificationSender2() {
|
||||
return new SmsNotification();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.conditionalonproperty.service;
|
||||
|
||||
public class EmailNotification implements NotificationSender {
|
||||
|
||||
@Override
|
||||
public String send(String message) {
|
||||
return "Email Notification: " + message;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.conditionalonproperty.service;
|
||||
|
||||
public interface NotificationSender {
|
||||
String send(String message);
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.conditionalonproperty.service;
|
||||
|
||||
public class SmsNotification implements NotificationSender {
|
||||
|
||||
@Override
|
||||
public String send(String message) {
|
||||
return "SMS notification: " + message;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,2 +1,4 @@
|
|||
spring.jpa.show-sql=true
|
||||
spring.jpa.hibernate.ddl-auto = update
|
||||
|
||||
notification.service=email
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.conditionalonproperty;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
|
||||
import com.baeldung.conditionalonproperty.config.NotificationConfig;
|
||||
import com.baeldung.conditionalonproperty.service.EmailNotification;
|
||||
import com.baeldung.conditionalonproperty.service.NotificationSender;
|
||||
|
||||
public class NotificationUnitTest {
|
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
|
||||
|
||||
@Test
|
||||
public void whenValueSetToEmail_thenCreateEmailNotification() {
|
||||
this.contextRunner.withPropertyValues("notification.service=email")
|
||||
.withUserConfiguration(NotificationConfig.class)
|
||||
.run(context -> {
|
||||
assertThat(context).hasBean("emailNotification");
|
||||
NotificationSender notificationSender = context.getBean(EmailNotification.class);
|
||||
assertThat(notificationSender.send("Hello From Baeldung!")).isEqualTo("Email Notification: Hello From Baeldung!");
|
||||
assertThat(context).doesNotHaveBean("smsNotification");
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue