From 839c08d7e024acbef38ccff5193781b465c6ac9f Mon Sep 17 00:00:00 2001 From: symphony-thibault Date: Thu, 13 Jan 2022 17:00:11 +0100 Subject: [PATCH] `@ComponentScan` of `BdkExtensionService` beans --- docs/extension.md | 7 ++----- .../bdk/examples/spring/api/GroupApi.java | 3 --- .../bdk/spring/SymphonyBdkAutoConfiguration.java | 3 +++ .../bdk/spring/config/BdkExtensionConfig.java | 16 +++++++++++++--- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/docs/extension.md b/docs/extension.md index c7113485e..9df2ac4b0 100644 --- a/docs/extension.md +++ b/docs/extension.md @@ -92,8 +92,8 @@ class ExtensionExample { ``` ### Access your Extension's service in Spring Boot -In Spring Boot, your extension's service is _lazily_ initialized. It means that you must annotate your injected extension's service -field with the `@Lazy` annotation in addition to the `@Autowired` one: +Since your extension service implements `BdkExtensionService` it will automatically be scanned and added to the application +context at startup. You can then simply inject and use it in your application beans: ```java @Configuration public class MyBdkExtensionConfig { @@ -108,13 +108,10 @@ public class MyBdkExtensionConfig { @RequestMapping("/api") public class ApiController { - @Lazy // required, otherwise Spring Boot application startup will fail @Autowired private MyBdkExtensionService groupService; } ``` -> :bulb: Note that your IDE might show an error like "_Could not autowire. No beans of 'MyBdkExtensionService' type found_". -> To disable this warning you can annotate your class with `@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")` ## BDK Aware Extensions The BDK Extension Model allows extensions to access to some core objects such as the configuration or the api clients. diff --git a/symphony-bdk-examples/bdk-spring-boot-example/src/main/java/com/symphony/bdk/examples/spring/api/GroupApi.java b/symphony-bdk-examples/bdk-spring-boot-example/src/main/java/com/symphony/bdk/examples/spring/api/GroupApi.java index 8162415a1..22c1f0854 100644 --- a/symphony-bdk-examples/bdk-spring-boot-example/src/main/java/com/symphony/bdk/examples/spring/api/GroupApi.java +++ b/symphony-bdk-examples/bdk-spring-boot-example/src/main/java/com/symphony/bdk/examples/spring/api/GroupApi.java @@ -7,7 +7,6 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Lazy; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @@ -16,12 +15,10 @@ @Slf4j @RestController @RequestMapping("/api/v1/groups") -@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") public class GroupApi { private final SymphonyGroupService groupService; - @Lazy @Autowired public GroupApi(SymphonyGroupService groupService) { this.groupService = groupService; diff --git a/symphony-bdk-spring/symphony-bdk-core-spring-boot-starter/src/main/java/com/symphony/bdk/spring/SymphonyBdkAutoConfiguration.java b/symphony-bdk-spring/symphony-bdk-core-spring-boot-starter/src/main/java/com/symphony/bdk/spring/SymphonyBdkAutoConfiguration.java index e627b1d36..0189c7b52 100644 --- a/symphony-bdk-spring/symphony-bdk-core-spring-boot-starter/src/main/java/com/symphony/bdk/spring/SymphonyBdkAutoConfiguration.java +++ b/symphony-bdk-spring/symphony-bdk-core-spring-boot-starter/src/main/java/com/symphony/bdk/spring/SymphonyBdkAutoConfiguration.java @@ -1,5 +1,6 @@ package com.symphony.bdk.spring; +import com.symphony.bdk.extension.BdkExtensionService; import com.symphony.bdk.spring.config.BdkActivityConfig; import com.symphony.bdk.spring.config.BdkApiClientsConfig; import com.symphony.bdk.spring.config.BdkCoreConfig; @@ -10,6 +11,8 @@ import com.symphony.bdk.spring.config.BdkServiceConfig; import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.FilterType; import org.springframework.context.annotation.Import; /** diff --git a/symphony-bdk-spring/symphony-bdk-core-spring-boot-starter/src/main/java/com/symphony/bdk/spring/config/BdkExtensionConfig.java b/symphony-bdk-spring/symphony-bdk-core-spring-boot-starter/src/main/java/com/symphony/bdk/spring/config/BdkExtensionConfig.java index aace98bbe..3e94cc342 100644 --- a/symphony-bdk-spring/symphony-bdk-core-spring-boot-starter/src/main/java/com/symphony/bdk/spring/config/BdkExtensionConfig.java +++ b/symphony-bdk-spring/symphony-bdk-core-spring-boot-starter/src/main/java/com/symphony/bdk/spring/config/BdkExtensionConfig.java @@ -14,7 +14,9 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.DependsOn; +import org.springframework.context.annotation.FilterType; import java.util.ArrayList; import java.util.List; @@ -22,6 +24,10 @@ @Slf4j @API(status = API.Status.EXPERIMENTAL) +@ComponentScan( + basePackages = "com.symphony.bdk", + includeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = BdkExtensionService.class) +) public class BdkExtensionConfig { @Bean @@ -59,9 +65,13 @@ public List bdkExtensionServices(final List e for (BdkExtension extension : extensions) { if (extension instanceof BdkExtensionServiceProvider) { final BdkExtensionService serviceBean = ((BdkExtensionServiceProvider) extension).getService(); - beanFactory.registerSingleton(serviceBean.getClass().getCanonicalName(), serviceBean); - services.add(serviceBean); - log.info("Extension service bean <{}> successfully registered in application context", serviceBean.getClass().getCanonicalName()); + if (beanFactory.getBean(serviceBean.getClass()) == null) { + beanFactory.registerSingleton(serviceBean.getClass().getCanonicalName(), serviceBean); + services.add(serviceBean); + log.info("Extension service bean <{}> successfully registered in application context", serviceBean.getClass().getCanonicalName()); + } else { + log.info("Extension service bean <{}> already exist in application context", serviceBean.getClass().getCanonicalName()); + } } }