com.zhaofujun.nest.spring.SpringBeanContainerProvider Maven / Gradle / Ivy
package com.zhaofujun.nest.spring;
import com.zhaofujun.nest.NestApplication;
import com.zhaofujun.nest.container.ContainerProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.*;
import org.springframework.stereotype.Component;
import java.util.Set;
import java.util.stream.Collectors;
@Component
public class SpringBeanContainerProvider implements ContainerProvider {
private Logger logger = LoggerFactory.getLogger(SpringBeanContainerProvider.class);
@Autowired
private ConfigurableApplicationContext applicationContext;
@Override
public void initialize() {
// applicationContext.refresh();
// applicationContext.start();
}
@Override
public T getInstance(Class clazz) {
try {
return applicationContext.getBean(clazz);
} catch (BeansException ex) {
logger.warn("从容器中没有找到类型为" + clazz.getName() + "的bean");
return null;
}
}
@Override
public T getInstance(Class clazz, String name) {
try {
return applicationContext.getBean(name, clazz);
} catch (BeansException ex) {
logger.warn("从容器中没有找到name为" + name + ",类型为" + clazz.getName() + "的bean");
return null;
}
}
@Override
public Set getInstances(Class clazz) {
return applicationContext.getBeansOfType(clazz).entrySet()
.stream()
.map(p -> p.getValue())
.collect(Collectors.toSet());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy