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();
        }
    
    }
    
  • 相关阅读:
    httpRuntime 元素(ASP.NET 设置架构)
    Change GridView RowColor OnMouseClick
    创建自定义配置结
    设置自定义服务器控件的TagPrefix
    使用DataTable.ReadXml时抛出异常
    正则表达式的字符,定位字符和重复字符
    URL 编码
    tschs.xml 全文索引文件配置说明
    SQL2005 全文索引 contains
    SQL 语句执行时间
  • 原文地址:https://www.cnblogs.com/fly-book/p/12692048.html
Copyright © 2011-2022 走看看