給Spring 註冊Bean的幾種方式總結。其中使用
@Import
註解是Spring Boot 完成自動配置的一個核心註解。java
@Controller
、@Service
等)配合上組件掃描器@ComponentScan
@Bean
註解@Import
註解FactoryBean
@Bean
註解Foo
public class Foo { }
@Configuration public class ExampleConfiguration { @Bean public Foo foo(){ return new Foo(); } }
public class SpringApplication { public static void main(String[] args) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ExampleConfiguration.class); String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames(); for (String delegate : beanDefinitionNames){ System.out.println("Bean Name : "+delegate); } applicationContext.close(); } }
Bean Name : org.springframework.context.annotation.internalConfigurationAnnotationProcessor 信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@37bba400: startup date [Tue Jul 17 17:36:15 CST 2018]; root of context hierarchy Bean Name : org.springframework.context.annotation.internalAutowiredAnnotationProcessor Bean Name : org.springframework.context.annotation.internalRequiredAnnotationProcessor Bean Name : org.springframework.context.annotation.internalCommonAnnotationProcessor Bean Name : org.springframework.context.event.internalEventListenerProcessor Bean Name : org.springframework.context.event.internalEventListenerFactory Bean Name : exampleConfiguration Bean Name : foo
@Bean
註解導入的Bean,默認Bean name爲方法名程序員
@Import
註解@Import(Foo.class)
@Configuration @Import(Foo.class) public class ExampleConfiguration { }
public class SpringApplication { public static void main(String[] args) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ExampleConfiguration.class); String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames(); for (String delegate : beanDefinitionNames){ System.out.println("Bean Name : "+delegate); } applicationContext.close(); } }
Bean Name : org.springframework.context.annotation.internalConfigurationAnnotationProcessor Bean Name : org.springframework.context.annotation.internalAutowiredAnnotationProcessor Bean Name : org.springframework.context.annotation.internalRequiredAnnotationProcessor Bean Name : org.springframework.context.annotation.internalCommonAnnotationProcessor Bean Name : org.springframework.context.event.internalEventListenerProcessor Bean Name : org.springframework.context.event.internalEventListenerFactory Bean Name : exampleConfiguration Bean Name : com.hanson.bean.Foo
@Import
導入的Bean 默認的Bean Name 爲Bean 的全限定名稱spring
@Import(FooImportSelectorImpl.class)
ImportSelector
接口,並覆寫其方法selectImports
public class FooImportSelectorImpl implements ImportSelector { @Override public String[] selectImports(AnnotationMetadata importingClassMetadata) { return new String[]{"com.hanson.bean.Foo"}; } }
@Configuration @Import(FooImportSelectorImpl.class) public class ExampleConfiguration { }
Bean Name : org.springframework.context.annotation.internalConfigurationAnnotationProcessor Bean Name : org.springframework.context.annotation.internalAutowiredAnnotationProcessor Bean Name : org.springframework.context.annotation.internalRequiredAnnotationProcessor Bean Name : org.springframework.context.annotation.internalCommonAnnotationProcessor Bean Name : org.springframework.context.event.internalEventListenerProcessor Bean Name : org.springframework.context.event.internalEventListenerFactory Bean Name : exampleConfiguration Bean Name : com.hanson.bean.Foo
這個方式有點控制反轉的意思,就是隻須要提供一個類的全限定名稱,就能幫助咱們注入到容器中。shell
@Import(FooImportBeanDefinitionRegistrarImpl.class)
,ImportBeanDefinitionRegistrar
並覆寫其方法registerBeanDefinitions
public class FooImportBeanDefinitionRegistrarImpl implements ImportBeanDefinitionRegistrar { @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { RootBeanDefinition beanDefinition = new RootBeanDefinition(); beanDefinition.setBeanClass(Foo.class); registry.registerBeanDefinition("fooExample", beanDefinition); } }
@Configuration @Import(FooImportBeanDefinitionRegistrarImpl.class) public class ExampleConfiguration { }
Bean Name : org.springframework.context.annotation.internalConfigurationAnnotationProcessor Bean Name : org.springframework.context.annotation.internalAutowiredAnnotationProcessor Bean Name : org.springframework.context.annotation.internalRequiredAnnotationProcessor Bean Name : org.springframework.context.annotation.internalCommonAnnotationProcessor Bean Name : org.springframework.context.event.internalEventListenerProcessor Bean Name : org.springframework.context.event.internalEventListenerFactory Bean Name : exampleConfiguration Bean Name : fooExample
這個方式須要程序員手動構建一個BeanDefinition
對象。而且爲他設置一個Bean Nameapp