声明式注入 BeanDefinition
@Configuration + @Bean
@Configuration
public class QuickstartConfiguration {
@Bean
public Person person() {
return new Person();
}
}
@Component + @ComponentScan
@Configuration
@ComponentScan("com.linkedbear.spring.annotation.c_scan")
public class ComponentScanConfiguration
@Import
@Import({Boss.class, BartenderConfiguration.class})
public @interface EnableTavern
配置式注入 BeanDefinition
<bean> 标签声明
<bean id="person" class="com.linkedbear.spring.basic_dl.a_quickstart_byname.bean.Person"/>
编程式注入 BeanDefinition
ImportBeanDefinitionRegistrar
public class WaiterRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
registry.registerBeanDefinition("waiter", new RootBeanDefinition(Waiter.class));
}
}
借助 BeanDefinitionRegistryPostProcessor 注入
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
registry.registerBeanDefinition("animalNameSetterPostProcessor",
new RootBeanDefinition(AnimalNameSetterPostProcessor.class));
}
手动构造 BeanDefinition 注入
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
BeanDefinition personDefinition = BeanDefinitionBuilder.rootBeanDefinition(Person.class)
.addPropertyValue("name", "zhangsan").getBeanDefinition();
ctx.registerBeanDefinition("person", personDefinition);
ctx.refresh();