本文目录

  • 1. 通过 @Bean的方式来导入组件(适用于导入自己的类/第三方组件的类)
  • 2.通过@CompentScan +@Controller @Service @Respository @compent 组合使用导入
  • 3. 通过 @Import 来导入组件 (这种方式导入组件的id为全类名路径)
  • 4. 通过@Import + ImportSeletor类实现组件的方式,来导入组件(可以指定bean的名称)
  • 5. 通过@Import + BeanDefinitionRegister导入组件(可以指定bean的名称)
  • 6. 通过实现FacotryBean接口来实现注册 组件



1. 通过 @Bean的方式来导入组件(适用于导入自己的类/第三方组件的类)

@Configuration 
public class MainConfig {
 	@Bean
    public Person person() {
        return new Person();
    }
}

2.通过@CompentScan +@Controller @Service @Respository @compent 组合使用导入

// 此处以SpringBoot为例
// 1.在启动类,添加ComponentScan,指定扫描哪个包下的文件
@SpringBootApplication
@ComponentScan(value="com.example")   // value也可以接多个包,它是一个String[]类型
public class DemoApplication {
	public static void main(String[] args){
		SpringApplication.run(DemoApplication.class, args);
	}
}
// 2.在指定配置扫描的包下,在类中使用@Controller/@Service 这些注解即可
@Controller
public class HomeController{
	// 这样,HomeController类也就会被扫描到 Spring IOC 容器中
}

3. 通过 @Import 来导入组件 (这种方式导入组件的id为全类名路径)

@Configuration
@Import(value={Person.class, Car.class})
public class MainConfig {
	
}

4. 通过@Import + ImportSeletor类实现组件的方式,来导入组件(可以指定bean的名称)

// 1.定义类,实现ImportSelector类,重写selectImports()方法
public class MyImportSelector implements ImportSelector {
	// 可以获取导入类的注解信息
	@Override
	public String[] selectImports(AnnotationMetadata importingClassMetadata) {
		return new String[]{"com.example.entity.Person"};    // 要导入IOC容器类的全类名路径
	}
}

// 2.使用@Import方式导入
@Configuration
@Import(value={Person.class, Car.class, MyImportSelector.class})
public class MainConfig {

}

5. 通过@Import + BeanDefinitionRegister导入组件(可以指定bean的名称)

// 1.定义类,实现ImportBeanDefinitionRegistrar类,重写registerBeanDefinitions()方法
public class MyBeanDefinitionRegister implements ImportBeanDefinitionRegistrar{
	// 可以获取导入类的注解信息
	@Override
	public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
		//创建一个bean定义对象 
		RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(Person.class); 
		//把bean定义对象导入到容器中 
		registry.registerBeanDefinition("person",rootBeanDefinition);
	}
}

// 2.使用@Import方式导入
@Configuration
@Import(value={Person.class, Car.class, MyBeanDefinitionRegister .class})
public class MainConfig {

}

6. 通过实现FacotryBean接口来实现注册 组件

// 1.定义MyFactoryBean类,实现FactoryBean类,重写getObject()/getObjectType()/isSingleton()方法
public class MyFactoryBean implements FactoryBean<Person> {

	// 返回bean的对象
	@Override
	public Person getObject() throws Exception {
		return new Person();
	}

	// 返回bean的类型
	@Override
	public Class<?> getObjectType() {
		return Person.class;
	}

	@Override
	public boolean isSingleton() {
		return true;
	}
}

// 2.使用getBean()方法获取bean实例
@SpringBootApplication
public class DemoApplication {
	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
		AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(MyFactoryBean.class);
		// bean名称为类名首字母小写
		Object bean = ac.getBean("myFactoryBean");   // 获取的是指定的bean对象
		System.out.println(bean);
		Object bean1 = ac.getBean("&myFactoryBean");  // 名称加个&,获取工厂bean本身对象(MyFactoryBean类)    
		System.out.println(bean1);
	}
}

容器追加挂载目录 容器中如何添加组件_类名

FacotryBean方式添加组件至IOC容器,运用场景:

  对于创建过程比较复杂的对象的创建,目前Spring其实有很多实现方式了,而FactoryBean只是其中一种,也许我们不会采用此种方式来实现实例对象的创建,但我们需要能够看懂此种方式,知道有这种实现方式;很多第三方都沿用了此种方式,我们去追源码的时候,很容易就能碰到;