1、@Configuration + @Bean
@Configuration
public class MyConfiguration {@Beanpublic Person person() {Person person = new Person();person.setName("spring");return person;}
}
2、@Componet + @ComponentScan
@Component
public class Person {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +'}';}
}@ComponentScan(basePackages = "com.springboot.initbean.*")
public class Demo1 {public static void main(String[] args) {AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Demo1.class);Person bean = applicationContext.getBean(Person.class);System.out.println(bean);}
}
3、@Import注解导入
@import注解源码
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Import {/** * 用于导入一个class文件 * {@link Configuration @Configuration}, {@link ImportSelector}, * {@link ImportBeanDefinitionRegistrar}, or regular component classes to import. */Class<?>[] value();}
3.1、直接使用@import注解导入类
然后自动的就被放置在IOC容器中了。
public class Person {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +'}';}
}
/*** 直接使用@Import导入person类,然后尝试从applicationContext中取,成功拿到**/
@Import(Person.class)
public class Demo1 {public static void main(String[] args) {AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Demo1.class);Person bean = applicationContext.getBean(Person.class);System.out.println(bean);}
}
3.2 @Import + ImportSelector
@Import(MyImportSelector.class)
public class Demo1 { public static void main(String[] args) {AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Demo1.class);Person bean = applicationContext.getBean(Person.class);System.out.println(bean);}
}class MyImportSelector implements ImportSelector {@Overridepublic String[] selectImports(AnnotationMetadata importingClassMetadata) {return new String[]{"com.springboot.pojo.Person"};}
}
3.3 @Import + ImportBeanDefinitionRegistrar
bean的定义(bean的元数据),也是需要放在IOC容器中进行管理的,先有bean的元数据,
applicationContext再根据bean的元数据去创建Bean。
@Import(MyImportBeanDefinitionRegistrar.class)
public class Demo1 {public static void main(String[] args) {AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Demo1.class);Person bean = applicationContext.getBean(Person.class);System.out.println(bean);}
}class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {@Overridepublic void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {// 构建一个beanDefinition, 关于beanDefinition我后续会介绍,可以简单理解为bean的定义.AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Person.class).getBeanDefinition();// 将beanDefinition注册到Ioc容器中.registry.registerBeanDefinition("person", beanDefinition);}
}
3.4 @Import + DeferredImportSelector
DeferredImportSelector 它是 ImportSelector 的子接口,所以实现的方法和第二种无异。
只是Spring的处理方式不同,它和Spring Boot中的自动导入配置文件 延迟导入有关
@Import(MyDeferredImportSelector.class)
public class Demo1 {public static void main(String[] args) {AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Demo1.class);Person bean = applicationContext.getBean(Person.class);System.out.println(bean);}
}
class MyDeferredImportSelector implements DeferredImportSelector {@Overridepublic String[] selectImports(AnnotationMetadata importingClassMetadata) {// 也是直接将Person的全限定名放进去return new String[]{Person.class.getName()};}
}
4、使用FactoryBean接口
FactoryBean, 后缀为bean,那么它其实就是一个bean,
BeanFactory,顾名思义 bean工厂,它是IOC容器的顶级接口
@Configuration
public class Demo1 {@Beanpublic PersonFactoryBean personFactoryBean() {return new PersonFactoryBean();}public static void main(String[] args) {AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Demo1.class);Person bean = applicationContext.getBean(Person.class);System.out.println(bean);}
}class PersonFactoryBean implements FactoryBean<Person> {/** * 直接new出来Person进行返回. */@Overridepublic Person getObject() throws Exception {return new Person();}/** * 指定返回bean的类型. */@Overridepublic Class<?> getObjectType() {return Person.class;}
}
5、使用 BeanDefinitionRegistryPostProcessor
等beanDefinition加载完毕之后,对beanDefinition进行后置处理,
可以在此进行调整IOC容器中的beanDefinition,从而干扰到后面进行初始化bean。
public class Demo1 {public static void main(String[] args) {AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();MyBeanDefinitionRegistryPostProcessor beanDefinitionRegistryPostProcessor = new MyBeanDefinitionRegistryPostProcessor();applicationContext.addBeanFactoryPostProcessor(beanDefinitionRegistryPostProcessor);applicationContext.refresh();Person bean = applicationContext.getBean(Person.class);System.out.println(bean);}
}class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {@Overridepublic void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Person.class).getBeanDefinition();registry.registerBeanDefinition("person", beanDefinition);}@Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {}
}