通过Import 注解动态创建bean实例

阅读更多

通过 @Import 注解动态创建bean实例主要有四种方式:

        导入普通java类,将其作为Spring bean注入到Spring容器中。

        导入 @Configuration 注解标注的配置类。

        导入实现ImportBeanDefinitionRegistrar接口的实现类。

        导入实现ImportSelector接口的实现类。

 

1、导入普通Java类

    配置类:

@Configuration
@Import({Role.class}) //导入普通Java类
class ImportAnnotationConfig {
	
}

    测试代码:

//AnnotationConfigApplicationContext默认会扫描以下注解类:Component、Configuration等
AnnotationConfigApplicationContext applicationContext 
	= new AnnotationConfigApplicationContext(ImportAnnotationConfig.class);
		
Role role3 = applicationContext.getBean(Role.class);
System.out.println(role3.getName());

 

2、导入配置类

public interface CountryMapper {
	void show();
}

class CountryMapperImpl2 implements CountryMapper {
	@Override
	public void show(){
		System.out.println("CountryMapperImpl2.show()");
	}
}

@Configuration
class ImportAnnotationConfig2 {
	@Bean
	public CountryMapper getCountryMapper(){
		CountryMapper countryMapper = new CountryMapperImpl2();
		return countryMapper;
	}
}


@Configuration
@Import({ImportAnnotationConfig2.class}) //导入配置类
class ImportAnnotationConfig {
	@Bean
	public User getUser(){
		User user = new User();
		user.setUsername("uid");
		user.setPassword("pwd");
		return user;
	}
}


//测试代码
AnnotationConfigApplicationContext applicationContext 
	= new AnnotationConfigApplicationContext(ImportAnnotationConfig.class);

User user = applicationContext.getBean(User.class);
System.out.println(user.getUsername());
System.out.println(user.getPassword());

CountryMapper countryMapper = applicationContext.getBean(CountryMapper.class);
countryMapper.show();
System.out.println(countryMapper.getUser());

 

3、导入ImportBeanDefinitionRegistrar的实现类

class ImportAnnotationRegistrar implements ImportBeanDefinitionRegistrar{
	@Override
	public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
		String beanName = "testRole";
		if(!registry.containsBeanDefinition(beanName)){
	    	BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(Role.class);
	    	
	    	GenericBeanDefinition beanDefinition = (GenericBeanDefinition)builder.getRawBeanDefinition();
	    	beanDefinition.setBeanClass(Role.class);
	    	beanDefinition.setAutowireMode(GenericBeanDefinition.AUTOWIRE_BY_NAME);
	    	
	    	//设置bean对象的属性值
	    	beanDefinition.getPropertyValues().add("name", "Test");
	    	
	    	registry.registerBeanDefinition(beanName, beanDefinition);
    	}
	}
}

@Configuration
@Import({ImportAnnotationRegistrar.class}) //导入ImportBeanDefinitionRegistrar实现类
class ImportAnnotationConfig {
 
}

//测试代码
AnnotationConfigApplicationContext applicationContext 
	= new AnnotationConfigApplicationContext(ImportAnnotationConfig.class);

Role role = applicationContext.getBean("testRole", Role.class);
System.out.println(role.getName());

 

4、导入ImportSelector的实现类

class ImportAnnotationSelector implements ImportSelector{
	@Override
	public String[] selectImports(AnnotationMetadata annotationMetadata) {
		//可以是Register实现类、配置类、普通Java类
		String[] array = {
			ImportAnnotationRegistrar.class.getName(),
			ImportAnnotationConfig2.class.getName(),
			Role.class.getName()
		};
		return array;
	}
}

@Configuration
@Import({ImportAnnotationSelector.class})
class ImportAnnotationConfig {

}

//测试代码
AnnotationConfigApplicationContext applicationContext 
	= new AnnotationConfigApplicationContext(ImportAnnotationConfig.class);

Role role = applicationContext.getBean("testRole", Role.class);
System.out.println(role.getName());

Role role2 = applicationContext.getBean(Role.class.getName(), Role.class);
System.out.println(role2.getName());

CountryMapper countryMapper = applicationContext.getBean(CountryMapper.class);
countryMapper.show();

 

你可能感兴趣的:(通过Import 注解动态创建bean实例)