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();
        }
    
    }
    
  • 相关阅读:
    C#设计模式系列 2 ChainOfResponsibility 职责链模式之真假美猴王
    c#查询计算机WMI信息
    C#设计模式系列 6 State 状态模式 之电视36计,我要自己掌握遥控器
    ASP.NET 一般处理文件,复制以前文件,无法调试,无法访问的问题
    DAY 215 Flask中before_request与after_request使用
    DAY 216 python爬虫requests库
    202020212 网络对抗技术 20181321 Exp2 后门原理与实践
    需求分析弯弓与烈马
    缓冲区溢出实验 20181321
    基于gmssl的CA系统构建及应用课程设计报告
  • 原文地址:https://www.cnblogs.com/fly-book/p/12692048.html
Copyright © 2011-2022 走看看