Skip to content

Commit

Permalink
added get bean definition by type
Browse files Browse the repository at this point in the history
  • Loading branch information
mykolaFilimonov committed Dec 1, 2023
1 parent 93ac410 commit 863af0e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,11 @@ private Object[] findOrCreateBeanDependencies(BeanDefinition beanDefinition) {
public List<Object> getAllBeans() {
return storageByName.values().stream().toList();
}

@Override
public <T> T getBean(Class<T> clazz) {
BeanDefinition beanDefinitionByType = dependencyUtils.getDependencyForType(clazz, definitionRegistry);
Object bean = getBean(beanDefinitionByType.getName());
return clazz.cast(bean);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.bobocodebreskul.context.registry;

import java.util.List;

/**
* A factory interface for creating and obtaining instances of beans.
*/
Expand All @@ -12,5 +14,21 @@ public interface ObjectFactory {
* @return the instance of the requested bean
*/
Object getBean(String name);

/**
* Retrieves a bean of the specified type from the container.
*
* @param clazz the class type of the bean to retrieve
* @param <T> the type of the bean
* @return the bean instance of the specified type, or {@code null} if not found
*/
<T> T getBean(Class<T> clazz);

/**
* Retrieves a list of all beans in the container.
*
* @return a list containing all beans in the container
*/
List<Object> getAllBeans();
}

Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private BeanDefinition getDependencyFromQualifier(String qualifier, BeanDependen
dependency, dependency.type(), beanDefinition.getBeanClass()));
}

private BeanDefinition getDependencyForType(Class<?> type, BeanDefinitionRegistry registry) {
public BeanDefinition getDependencyForType(Class<?> type, BeanDefinitionRegistry registry) {
List<BeanDefinition> beanDefinitionByType = registry.getBeanDefinitionByType(type);

if (beanDefinitionByType.isEmpty()) {
Expand Down

0 comments on commit 863af0e

Please sign in to comment.