+12
@@ -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);
|
||||
}
|
||||
}
|
||||
+26
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.conditionalonproperty.service;
|
||||
|
||||
public class EmailNotification implements NotificationSender {
|
||||
|
||||
@Override
|
||||
public String send(String message) {
|
||||
return "Email Notification: " + message;
|
||||
}
|
||||
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.conditionalonproperty.service;
|
||||
|
||||
public interface NotificationSender {
|
||||
String send(String message);
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.conditionalonproperty.service;
|
||||
|
||||
public class SmsNotification implements NotificationSender {
|
||||
|
||||
@Override
|
||||
public String send(String message) {
|
||||
return "SMS notification: " + message;
|
||||
}
|
||||
|
||||
}
|
||||
+2
@@ -1,2 +1,4 @@
|
||||
spring.jpa.show-sql=true
|
||||
spring.jpa.hibernate.ddl-auto = update
|
||||
|
||||
notification.service=email
|
||||
|
||||
+27
@@ -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");
|
||||
});
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.asyncvsflux;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class AsyncController {
|
||||
|
||||
@GetMapping("/async_result")
|
||||
@Async
|
||||
public CompletableFuture<String> getResultAsyc(HttpServletRequest request) {
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return CompletableFuture.completedFuture("Result is ready!");
|
||||
}
|
||||
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.asyncvsflux;
|
||||
|
||||
import java.io.IOException;
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.FilterConfig;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class AsyncFilter implements Filter {
|
||||
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) throws ServletException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
|
||||
try {
|
||||
Thread.sleep(200);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
filterChain.doFilter(servletRequest, servletResponse);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.asyncvsflux;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.scheduling.annotation.EnableAsync;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableAsync
|
||||
public class AsyncVsWebFluxApp {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(AsyncVsWebFluxApp.class, args);
|
||||
}
|
||||
}
|
||||
@@ -49,6 +49,10 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-rest</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
@@ -99,24 +103,7 @@
|
||||
<!-- Spring Fox 2 -->
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger2</artifactId>
|
||||
<version>${spring.fox.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger-ui</artifactId>
|
||||
<version>${spring.fox.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-data-rest</artifactId>
|
||||
<version>${spring.fox.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-bean-validators</artifactId>
|
||||
<artifactId>springfox-boot-starter</artifactId>
|
||||
<version>${spring.fox.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
+9
-10
@@ -1,13 +1,9 @@
|
||||
package com.baeldung.swagger2boot.configuration;
|
||||
|
||||
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||
import com.baeldung.swagger2boot.plugin.EmailAnnotationPlugin;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import com.baeldung.swagger2boot.plugin.EmailAnnotationPlugin;
|
||||
|
||||
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
|
||||
import springfox.documentation.builders.PathSelectors;
|
||||
import springfox.documentation.builders.RequestHandlerSelectors;
|
||||
@@ -16,13 +12,16 @@ import springfox.documentation.service.Contact;
|
||||
import springfox.documentation.spi.DocumentationType;
|
||||
import springfox.documentation.spring.data.rest.configuration.SpringDataRestConfiguration;
|
||||
import springfox.documentation.spring.web.plugins.Docket;
|
||||
import springfox.documentation.swagger.web.*;
|
||||
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
|
||||
import springfox.documentation.swagger.web.DocExpansion;
|
||||
import springfox.documentation.swagger.web.ModelRendering;
|
||||
import springfox.documentation.swagger.web.OperationsSorter;
|
||||
import springfox.documentation.swagger.web.TagsSorter;
|
||||
import springfox.documentation.swagger.web.UiConfiguration;
|
||||
import springfox.documentation.swagger.web.UiConfigurationBuilder;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
@Configuration
|
||||
@EnableSwagger2WebMvc
|
||||
@Import({SpringDataRestConfiguration.class, BeanValidatorPluginsConfiguration.class})
|
||||
public class SpringFoxConfig {
|
||||
|
||||
@@ -43,8 +42,8 @@ public class SpringFoxConfig {
|
||||
return new Docket(DocumentationType.SWAGGER_2)
|
||||
.apiInfo(apiInfo())
|
||||
.select()
|
||||
.apis(RequestHandlerSelectors.any())
|
||||
.paths(PathSelectors.any())
|
||||
.apis(RequestHandlerSelectors.any())
|
||||
.paths(PathSelectors.any())
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user