想要让自己写的一个普通类创建的对象扔到Spring 容器中管理,有以下方法:
@Configuration
@Import(YourBusinessService.class)
public class YourBusinessImportConfig {
}
当然 可以将类 YourBusinessImportConfig 换成注解也行,例如:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({YourBusinessService.class})
public @interface EnableYourBusinessService {
}
然后你就可以在自己的Spring项目中采用 @EnableYourBusinessService 来注入 YourBusinessService对象。
public class YourBusinessServiceSelector implements ImportSelector {
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[]{"com.baisheng.service.YourBusinessService"};
}
}
然后通过@Import注解注入到Spring容器里面
@Configuration
@Import(YourBusinessServiceSelector .class)
public class YourBusinessImportConfig {
}
当然同理,可以将YourBusinessImportConfig 类换成注解也行
public class EnableConfigurationPropertiesRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
registerInfrastructureBeans(registry);
ConfigurationPropertiesBeanRegistrar beanRegistrar = new ConfigurationPropertiesBeanRegistrar(registry);
this.getTypes(metadata).forEach(beanRegistrar::register);
}
}
自己写一个
public class YourBeanDefinitionRegistrat implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
BeanDefinition beanDefinition = new RootBeanDefinition(YourBusinessService.class);
registry.registerBeanDefinition("yourBusinessService", beanDefinition);
}
}
这种一般在自定义框架中比较常见,比如dubbo等
如果你采用SpringBoot为基础框架时,就比较好做。
@Resource
private ApplicationContext applicationContext;
DefaultListableBeanFactory defaultListableBeanFactory =
(DefaultListableBeanFactory) applicationContext.getAutowireCapableBeanFactory();
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(YourBusinessService.class);
// 如果有构造函数参数
beanDefinitionBuilder.addConstructorArgValue(object1);
beanDefinitionBuilder.addConstructorArgValue(object2);
beanDefinitionBuilder.addConstructorArgValue(object3);
// 等等 有几个构造函数的参数就设置几个
// 设置 init方法
beanDefinitionBuilder.setInitMethodName("initMethodName");
// 设置 destory方法
beanDefinitionBuilder.setDestroyMethodName("destoryMethodName");
AbstractBeanDefinition beanDefinition = beanDefinitionBuilder.getBeanDefinition();
defaultListableBeanFactory.registerBeanDefinition("beanName", beanDefinitionBuilder.getBeanDefinition());
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.middol.starter.mq.config.AliyunRocketMqAutoConfigure,\
com.middol.starter.mq.config.ApacheRocketMqAutoConfigure
这里有两个配置类 AliyunRocketMqAutoConfigure 和 ApacheRocketMqAutoConfigure
配置类大致情况如下:
@Configuration
@ConditionalOnClass({ONSFactory.class})
@ConditionalOnProperty(prefix = "middol.mq.aliyun.rocketmq", value = "enable", havingValue = "true")
@EnableConfigurationProperties({AliyunRocketMqProperties.class})
public class AliyunRocketMqAutoConfigure implements InitializingBean {
}
@Configuration
@ConditionalOnClass({SendMessageContext.class})
@ConditionalOnProperty(prefix = "middol.mq.apache.rocketmq", value = "enable", havingValue = "true")
@EnableConfigurationProperties({ApacheRocketMqProperties.class})
public class ApacheRocketMqAutoConfigure implements InitializingBean {
}
在配置类里面的 @Bean方法new出的对象都会进入spring容器中。