-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adds support for getting instances and instance operators (#16)
- Loading branch information
1 parent
6d5f2fb
commit bb41b01
Showing
15 changed files
with
602 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
name: Verify build | ||
|
||
on: [push] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Cache local Maven repository | ||
uses: actions/cache@v3 | ||
with: | ||
path: ~/.m2/repository | ||
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} | ||
restore-keys: | | ||
${{ runner.os }}-maven- | ||
- name: Set up JDK 19 | ||
uses: actions/setup-java@v3 | ||
with: | ||
java-version: '19' | ||
distribution: 'adopt' | ||
|
||
- name: Package | ||
run: mvn package | ||
|
||
- name: Test | ||
run: mvn test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright 2023 Kapeta Inc. | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
package com.kapeta.spring.config; | ||
|
||
import org.springframework.beans.factory.config.BeanDefinition; | ||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; | ||
import org.springframework.beans.factory.support.BeanDefinitionRegistry; | ||
import org.springframework.beans.factory.support.GenericBeanDefinition; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class BeanHelper { | ||
private final BeanDefinitionRegistry beanRegistry; | ||
private final ConfigurableListableBeanFactory beanFactory; | ||
private final Map<Class,List<Object>> instances = new HashMap<>(); | ||
|
||
public BeanHelper(ConfigurableListableBeanFactory beanFactory) { | ||
this.beanRegistry = (BeanDefinitionRegistry) beanFactory; | ||
this.beanFactory = beanFactory; | ||
} | ||
|
||
private boolean isBeanRegistered(Class clz, Object value) { | ||
if (!instances.containsKey(clz)) { | ||
instances.put(clz, new ArrayList<>()); | ||
} | ||
if (instances.get(clz).contains(value)) { | ||
return true; | ||
} | ||
instances.get(clz).add(value); | ||
return false; | ||
} | ||
|
||
public <T,U extends T> void registerBean(String beanName, Class<T> clz, U value) { | ||
if (isBeanRegistered(clz, value)) { | ||
return; | ||
} | ||
GenericBeanDefinition beanDefinition = new GenericBeanDefinition(); | ||
beanDefinition.setBeanClass(clz); | ||
beanDefinition.setScope(BeanDefinition.SCOPE_SINGLETON); | ||
beanDefinition.setAutowireCandidate(true); | ||
beanDefinition.setInstanceSupplier(() -> value); | ||
beanRegistry.registerBeanDefinition(beanName, beanDefinition); | ||
} | ||
|
||
public <T,U extends T> void registerBean( Class<T> clz, U value) { | ||
if (isBeanRegistered(clz, value)) { | ||
return; | ||
} | ||
beanFactory.registerResolvableDependency(clz, value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.