当前位置 博文首页 > 文章内容

    Spring之使用FactoryBean为容器注入Bean

    作者: 栏目:未分类 时间:2020-08-25 11:01:19

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



    1、自定义Bean

    public class Fruit {
        private Integer fruitId;
        private String fruitName;
        private Double fruitPrice;
    
        public Fruit() {
            System.out.println("Fruit的无参数的构造方法");
        }
    
        public Fruit(Integer fruitId, String fruitName, Double fruitPrice) {
            this.fruitId = fruitId;
            this.fruitName = fruitName;
            this.fruitPrice = fruitPrice;
            System.out.println("Fruit的有参数的构造方法");
        }
    }
    

    2、编写配置类

    // 标记这是一个Spring配置类
    @Configuration
    public class SpringConfiguration {
        // 如果没有@Bean注解,则注入到容器中的id就是方法名(也就是myFactoryBean),但是如果显示的给了值,那么注入到容器中的就是factoryBean
        @Bean("factoryBean")
        public MyFactoryBean myFactoryBean(){
            return new MyFactoryBean();
        }
    }
    

    3、测试类 

    public class SpringDemo {
    
        @Test
        public void springTest01() throws Exception {
            AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfiguration.class);
            // 容器中获取的Bean,实际上就是工厂Bean(MyFactoryBean通过getObject()方法返回的对象)
            Object factoryBean01 = context.getBean("factoryBean");
            System.out.println("实际上注入到容器中的类型是:" + factoryBean01.getClass());
    
            Object factoryBean02 = context.getBean("factoryBean");
            System.out.println("注入到容器内的对象是否是单例:" + (factoryBean01 == factoryBean02));
    
            Object factoryBean03 = context.getBean("&factoryBean");
            System.out.println("如果想获取到MyFactoryBean的对象,使用&前缀:" + factoryBean03);
            
            // 输出打印Spring中的所有Bean名称
            String[] beanDefinitionNames = context.getBeanDefinitionNames();
            for (String beanDefinitionName : beanDefinitionNames) {
                System.out.println(beanDefinitionName);
            }
        }
    }
    

    4、测试结果 

    // 调用Fruit的无参数构造方法创建对象
    Fruit的无参数的构造方法
    // 虽然我们使用@Bean注解指定注入的是MyFactoryBean类型,但是从容器中取出来的类型依旧是我们在工厂Bean中通过getObject()方法创建的Bean的类型
    实际上注入到容器中的类型是:class com.spring01.bean.Fruit
    // Spring5中默认是创建单例,但是也可以重写FactoryBean中的isSinglton方法来指定单例还是多里
    注入到容器内的对象是否是单例:true
    // 如果你只想获取到工厂Bean的对象,需要加上一个&前缀来获取,不加前缀获取的就是工厂Bean实际创建的对象
    如果想获取到MyFactoryBean的对象,使用&前缀:com.spring01.controller.MyFactoryBean@78b729e6
    // Spring自身的组件
    org.springframework.context.annotation.internalConfigurationAnnotationProcessor
    org.springframework.context.annotation.internalAutowiredAnnotationProcessor
    org.springframework.context.annotation.internalCommonAnnotationProcessor
    org.springframework.context.event.internalEventListenerProcessor
    org.springframework.context.event.internalEventListenerFactory
    // Spring的配置类对象
    springConfiguration
    // @Bean注解注入到容器中MyFactoryBean的id,虽然id看起来是属于MyFactoryBean类型的,
    // 但是实际的类型却是MyFactoryBean调用getObject()方法返回的对象 factoryBean