注解方式配置bean

public abstract class Color {
    abstract String getName();
}
public class Red extends Color {
    @Override
    String getName() {
        return "red";
    }
}
@Component
public class ColorService {
    @Autowired
    @Qualifier("green")
    private Color color;

    public void getColorName(){
        System.out.println(color.getName());
    }
}

@Component+@Bean 方式

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * 使用 @Bean 方式
 */
@Component
public class BeanConfiguration {

    @Bean("red")
    public Color getColor(){
        return new Red();
    }
}

FactoryBean方式

import org.springframework.beans.factory.FactoryBean;
import org.springframework.stereotype.Component;

/**
 * 实现FactoryBean
 */
@Component
public class MyColor implements FactoryBean<Color> {
    @Override
    public Color getObject() throws Exception {
        return new Blue();
    }

    @Override
    public Class<?> getObjectType() {
        return Color.class;
    }
}

BeanDefinitionRegistryPostProcessor方式


import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.stereotype.Component;

/**
 * 实现 BeanDefinitionRegistryPostProcessor
 */
@Component
public class MyBeanRegister implements BeanDefinitionRegistryPostProcessor {
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        RootBeanDefinition beanDefinition = new RootBeanDefinition();
        beanDefinition.setBeanClass(Yellow.class);
        registry.registerBeanDefinition("yellow", beanDefinition);
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    }
}

ImportBeanDefinitionRegistrar方式


import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.stereotype.Component;

/**
 * 实现 ImportBeanDefinitionRegistrar
 */
@Component
public class MyBeanImport implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        RootBeanDefinition rootBeanDefinition = new RootBeanDefinition();
        rootBeanDefinition.setBeanClass(Green.class);
        registry.registerBeanDefinition("green",rootBeanDefinition);
    }
}

ImportBeanDefinitionRegistrar方式测试


import cn.fly.springbootdemo.ioc.ann.ColorService;
import cn.fly.springbootdemo.ioc.ann.MyBeanImport;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;

/**
 * 注解方式配置bean
 * 优点:
 *    使用简单
 *    开发效率高
 *    高内聚
 * 缺点:
 *    配置分散
 *    对象关系不清晰
 *    配置修改需要重新编译工程
 */
@SpringBootTest
@Import(MyBeanImport.class)
public class BeansTest2 {
    @Autowired
    private ColorService colorService;

    @Test
    public void test(){
        colorService.getColorName();
    }

}
原文地址:https://www.cnblogs.com/fly-book/p/12692048.html