zoukankan      html  css  js  c++  java
  • 注解方式配置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();
        }
    
    }
    
  • 相关阅读:
    linux 分区格式查看
    MDL原理理解
    linux oracle配置开机启动
    oracle em手动配置
    java字符编码详解
    linux oracle 配置监听器
    mysql 生成时间序列数据
    R实用小技巧
    python将文件夹下的所有csv文件存入mysql和oracle数据库
    遗传算法求解最优化问题
  • 原文地址:https://www.cnblogs.com/fly-book/p/12692048.html
Copyright © 2011-2022 走看看