Spring管理对象一般是通过注解@Component、xml(),如果我们不想通过spring,想自己操作让spring无差别管理,那应该怎么做到呢?
我们知道Spring的Bean启动顺序大致是如下图
分析:如果我们想不通过Spring的手段(@Component第),让Spring管理我们的对象,我们可以通过在扫描的时候,将bean放到bd(BeanDefinition)中
幸运的是,Spring已经帮我们考虑到了这一点
Spring提供了一个后置处理器BeanDefinitionRegistryPostProcessor
public interface BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor {
void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry var1) throws BeansException;
}
复制代码
它的作用是在Spring开始根据BeanDefinition
实例化之前,让用户通过接口参数BeanDefinitionRegistry做一些,最常见的处理就是通过BeanDefinitionRegistry.registerBeanDefinition
注册bd
以dubbo为例,我们知道,dubbo在使用注解@Service暴露接口的时候,会让接口给到Spring管理,跟我们上面讲的一致,
dubbo通过ServiceAnnotationBeanPostProcessor来实现这一功能
public class ServiceAnnotationBeanPostProcessor implements BeanDefinitionRegistryPostProcessor
复制代码
spring启动时,会进入它的post方法,一路debug进去到registerServiceBean
private void registerServiceBean(BeanDefinitionHolder beanDefinitionHolder, BeanDefinitionRegistry registry,DubboClassPathBeanDefinitionScanner scanner) {
Class<?> beanClass = resolveClass(beanDefinitionHolder);
Service service = findAnnotation(beanClass, Service.class);
Class<?> interfaceClass = resolveServiceInterfaceClass(beanClass, service);
String annotatedServiceBeanName = beanDefinitionHolder.getBeanName();
AbstractBeanDefinition serviceBeanDefinition =
buildServiceBeanDefinition(service, interfaceClass, annotatedServiceBeanName);
// ServiceBean Bean name
String beanName = generateServiceBeanName(service, interfaceClass, annotatedServiceBeanName);
//省略...........
}
复制代码
重点方法 -> buildServiceBeanDefinition
private AbstractBeanDefinition buildServiceBeanDefinition(Service service, Class<?> interfaceClass,
String annotatedServiceBeanName) {
BeanDefinitionBuilder builder = rootBeanDefinition(ServiceBean.class);
AbstractBeanDefinition beanDefinition = builder.getBeanDefinition();
MutablePropertyValues propertyValues = beanDefinition.getPropertyValues();
String[] ignoreAttributeNames = of("provider", "monitor", "application", "module", "registry", "protocol",
"interface", "interfaceName");
propertyValues.addPropertyValues(new AnnotationPropertyValuesAdapter(service, environment, ignoreAttributeNames));
// References "ref" property to annotated-@Service Bean
addPropertyReference(builder, "ref", annotatedServiceBeanName);
// Set interface
builder.addPropertyValue("interface", interfaceClass.getName());
//从service获取 对bd赋值
//省略...
return builder.getBeanDefinition();
}
复制代码
利用ServiceBean作为类名生成BeanDefinition,并对其赋值,方便以后对象实例化生成相应的对象