Spring注解开发——11、组件注册-使用FactoryBean注册组件

11、组件注册-使用FactoryBean注册组件

package org.springframework.beans.factory;

import org.springframework.lang.Nullable;

public interface FactoryBean {

    @Nullable
    T getObject() throws Exception;

    @Nullable
    Class getObjectType();

    default boolean isSingleton() {
        return true;
    }

}

11.1 实现FactoryBean接口

package com.suirui.springanno.entity;

import org.springframework.beans.factory.FactoryBean;

public class ColorFactoryBean implements FactoryBean {

    /**
     * 返回一个color对象 ,这个对象会被添加到容器中
     *
     * @return
     * @throws Exception
     */
    public Color getObject() throws Exception {
        System.out.println("ColorFactoryBean……");
        return new Color();
    }

    /**
     * 返回Color对象的类型
     *
     * @return
     */
    public Class getObjectType() {
        return Color.class;
    }

    /**
     * 控制是否单例
     *
     * @return true 单实例 false 多实例
     */
    public boolean isSingleton() {
        return true;
    }
}

11.2 MainConfig注入ColorFactoryBean

package com.suirui.springanno.config;

import com.suirui.springanno.condition.LinuxConditional;
import com.suirui.springanno.condition.MyImportBeanDefinitionRegistrar;
import com.suirui.springanno.condition.MyImportSelector;
import com.suirui.springanno.condition.WindowsConditional;
import com.suirui.springanno.entity.Color;
import com.suirui.springanno.entity.ColorFactoryBean;
import com.suirui.springanno.entity.Person;
import com.suirui.springanno.entity.Red;
import org.springframework.context.annotation.*;

@Configuration  //告诉Spring这是一个配置类
@Import({Color.class,  Red.class, MyImportSelector.class, MyImportBeanDefinitionRegistrar.class})  // 快速导入组建,ID 默认是全路径包名
public class MainConfig2 {

    @Bean("bill")
    @Conditional({WindowsConditional.class})//满足class条件才能生成bean
    public Person person1() {
        return  new Person("bill",38);
    }

    @Bean("linus")
    @Conditional({LinuxConditional.class})//满足class条件才能生成bean
    public Person person2() {
        return  new Person("linus",48);
    }

    @Bean
    public ColorFactoryBean colorFactoryBean() {//看似注册的是ColorFactoryBean,其实同时还注册了ColorFactoryBean getObject的对象
        return new ColorFactoryBean();
    }
}
package com.suirui.springanno;


import com.suirui.springanno.config.MainConfig;
import com.suirui.springanno.config.MainConfig2;
import com.suirui.springanno.entity.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.env.Environment;

import java.util.Map;

public class MainTest {
    public static void main(String[] args) {

        AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(MainConfig2.class);
        //获取所有的BeanDefinitionNames
        String[] beanNames = ac.getBeanDefinitionNames();
        for (String s : beanNames) {
            System.out.println(s);
        }

        Object bean = ac.getBean("colorFactoryBean"); //通过方法名获取的是factoryBean中getObject的对象
        System.out.println("colorFactoryBean bean的类型" + bean.getClass());

        Object bean2 = ac.getBean("&colorFactoryBean");//加上&才是返回真正的colorFactoryBean对象
        System.out.println("colorFactoryBean bean的类型" + bean2.getClass());
    }
}

Spring注解开发——11、组件注册-使用FactoryBean注册组件_第1张图片

你可能感兴趣的:(Spring注解开发)