Skip to content

Commit

Permalink
@ComponentScan of BdkExtensionService beans
Browse files Browse the repository at this point in the history
  • Loading branch information
thibauult committed Jan 13, 2022
1 parent eea68f4 commit 839c08d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
7 changes: 2 additions & 5 deletions docs/extension.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@
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;
import java.util.Optional;

@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
Expand Down Expand Up @@ -59,9 +65,13 @@ public List<BdkExtensionService> bdkExtensionServices(final List<BdkExtension> 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());
}
}
}

Expand Down

0 comments on commit 839c08d

Please sign in to comment.