Spring - @Import和ImportSelector以及ImportBeanDefinitionRegistrar

上几篇文章导入Bean用到的是@Bean注解,这里还有一个@Import注解也能向Spring的IOC容器中添加Bean实例,默认实例名就是类的全类名

事例代码

public class Color {
    
}


public class Rad extends Color {

}


public class Blue extends Color {
    
}

配置文件

//告诉Spring这是一个配置文件
@Configuration
@Import({Rad.class, Blue.class})//用Import向Spring的IOC容器中导入Rad类的实例和Blue类的实例
public class SpringConfig6 {

    @Conditional({WindowsCondition.class})
    @Bean(name = "bill")//给容器中注册一个bean
    public Person getPerson01() {
        return new Person("bill", 62);
    }

    @Conditional({LinuxCondition.class})
    @Bean(name = "linus")//给容器中注册一个bean
    public Person getPerson02() {
        return new Person("linus", 89);
    }
}

获取两个Color的实例

   @Test
    public void testAnnotation6() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig6.class);
        Map<String, Color> map = context.getBeansOfType(Color.class);
        map.keySet().forEach(key -> {
            Color color = map.get(key);
            System.out.println(key + "-->" + color.toString());
        });
    }

打印信息中我们就能看到我们通过@Import注解向Spring的IOC容器中导入的两个颜色的实例了

com.booyue.tlh.annotation.bean.Rad-->com.booyue.tlh.annotation.bean.Rad@55634720
com.booyue.tlh.annotation.bean.Blue-->com.booyue.tlh.annotation.bean.Blue@4b0d79fc

ImportSelector

我们也可以通过实现ImportSelector接口向Spring的IOC容器中注册Bean的实例

我们再次定义两个颜色的子类,一个Black和一个Yellow

public class Black extends Color {

}


public class Yellow extends Color {

}

实现ImportSelector接口,返回Black和Yellow的全类名

/**
 * 自定义ImportSelector
 */
public class MyImportSelector implements ImportSelector {

    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        return new String[]{"com.booyue.tlh.annotation.bean.Yellow","com.booyue.tlh.annotation.bean.Black"};
    }
}

在@Improt中引入我们自定的ImportSelector

//告诉Spring这是一个配置文件
@Configuration
@Import({Rad.class, Blue.class, MyImportSelector.class})//用Import向Spring的IOC容器中导入Rad类的实例和Blue类的实例,以及我们在ImportSelector中返回的类
public class SpringConfig6 {

    @Conditional({WindowsCondition.class})
    @Bean(name = "bill")//给容器中注册一个bean
    public Person getPerson01() {
        return new Person("bill", 62);
    }

    @Conditional({LinuxCondition.class})
    @Bean(name = "linus")//给容器中注册一个bean
    public Person getPerson02() {
        return new Person("linus", 89);
    }
}

从Spring的IOC容器中获取Color实例的时候,我们就能获取到Balck和Yellow的实例了


//从Spring的IOC容器中获取Color实例
    @Test
    public void testAnnotation6() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig6.class);
        Map<String, Color> map = context.getBeansOfType(Color.class);
        map.keySet().forEach(key -> {
            Color color = map.get(key);
            System.out.println(key + "-->" + color.toString());
        });
    }


//打印信息
com.booyue.tlh.annotation.bean.Rad-->com.booyue.tlh.annotation.bean.Rad@3012646b
com.booyue.tlh.annotation.bean.Blue-->com.booyue.tlh.annotation.bean.Blue@4a883b15
com.booyue.tlh.annotation.bean.Yellow-->com.booyue.tlh.annotation.bean.Yellow@25641d39
com.booyue.tlh.annotation.bean.Black-->com.booyue.tlh.annotation.bean.Black@7b36aa0c

ImportBeanDefinitionRegistrar

我们还可以通过ImportBeanDefinitionRegistrar来向Spring的IOC容器中导入Bean实例

我们在定义个Color的类:RainBow

public class RainBow extends Color {

}

实现ImportBeanDefinitionRegistrar接口,在ImportBeanDefinitionRegistrar的方法registerBeanDefinitions中如果有“red”和“blue”的话我们就将RainBow注册进去,名称为“rainBow”

/**
 * 自定义BeanDefinition注册器
 */
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

    /**
     * @param importingClassMetadata 当前类的注册信息
     * @param registry               BeanDefinition注册类,可以自定义注册Bean
     */
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        boolean hasRed = registry.containsBeanDefinition("com.booyue.tlh.annotation.bean.Rad");
        boolean hasBlue = registry.containsBeanDefinition("com.booyue.tlh.annotation.bean.Blue");
        if (hasRed && hasBlue) {
            BeanDefinition rainBowDefinition = new RootBeanDefinition(RainBow.class);
            registry.registerBeanDefinition("rainBow", rainBowDefinition);
        }
    }
}

在配置文件中将我们自定义的MyImportBeanDefinitionRegistrar用@Import注解导入

//告诉Spring这是一个配置文件
@Configuration
@Import({Rad.class, Blue.class, MyImportSelector.class,MyImportBeanDefinitionRegistrar.class})//用Import向Spring的IOC容器中导入Rad类的实例和Blue类的实例,以及我们在ImportSelector中返回的类
public class SpringConfig6 {
        ......
}

查看Spring的IOC容器中的情况:有red和blue,然后我们的rainBow也注册到了Spring的IOC中

//从Spring的IOC容器中获取Color实例
    @Test
    public void testAnnotation6() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig6.class);
        Map<String, Color> map = context.getBeansOfType(Color.class);
        map.keySet().forEach(key -> {
            Color color = map.get(key);
            System.out.println(key + "-->" + color.toString());
        });
    }


//打印信息
com.booyue.tlh.annotation.bean.Rad-->com.booyue.tlh.annotation.bean.Rad@25641d39
com.booyue.tlh.annotation.bean.Blue-->com.booyue.tlh.annotation.bean.Blue@7b36aa0c
com.booyue.tlh.annotation.bean.Yellow-->com.booyue.tlh.annotation.bean.Yellow@5824a83d
com.booyue.tlh.annotation.bean.Black-->com.booyue.tlh.annotation.bean.Black@537f60bf
rainBow-->com.booyue.tlh.annotation.bean.RainBow@5677323c

猜你喜欢

转载自blog.csdn.net/qq_27062249/article/details/118067295
今日推荐