zoukankan      html  css  js  c++  java
  • 阿里面试官:小伙子,你给我说一下Spring Bean初始化的几种常规方式吧

    前言

    通过构造方法实例化
    通过静态工厂实例化
    通过实例工厂实例化
    通过FactoryBean实例化

    RumenzA实体类

    package com.rumenz;
    public class RumenzA {
    
        private String id;
        private String name;
        
        public static RumenzA createRumenzA(){
            RumenzA rumenzA=new RumenzA();
            rumenzA.setId("123");
            rumenzA.setName("入门小站");
            return rumenzA;
        }
        
        public  RumenzA() {
            System.out.println("RumenzA 无参构造方法");
        }
        public RumenzA(String id) {
            this.id = id;
            System.out.println("ID构造方法");
        }
        // set get省略
    }
    

      

    构造方法

    beans.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="rumenz" class="com.rumenz.RumenzA" />
    <bean id="rumenz1" class="com.rumenz.RumenzA">
         <constructor-arg name="id" value="1"/>
    </bean>
    </beans>
    

      

    DemoApplication.java

    package com.rumenz;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class DemoApplication {
        public static  void main(String[] args) {
            ClassPathXmlApplicationContext  ca=new ClassPathXmlApplicationContext("beans.xml");
            RumenzA rumenzA=(RumenzA)ca.getBean("rumenz");
        }
    
    }
    

      

    输出

    xxx.DefaultListableBeanFactory - Creating shared instance of singleton bean 'rumenz'
    RumenzA 无参构造方法
    xxx.DefaultListableBeanFactory - Creating shared instance of singleton bean 'rumenz1'
    ID构造方法
    

      

    静态工厂

    beans.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <bean id="rFactory" class="com.rumenz.RumenzFactory" factory-method="rumenzFactory"/>
    <bean id="rFactory1" class="com.rumenz.RumenzFactory" factory-method="rumenzFactory">
        <constructor-arg name="id" value="111"/>
    </bean>
    </beans>
    

      

    RumenzFactory工厂类

    package com.rumenz;
    public class RumenzFactory {
        
        //静态方法
        public static RumenzA rumenzFactory(){
            return new RumenzA();
        }
        public static RumenzA rumenzFactory(String id){
            return new RumenzA(id);
        }
    
    }
    

      

    DemoApplication.java

    package com.rumenz;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class DemoApplication {
        public static  void main(String[] args) {
            ClassPathXmlApplicationContext  ca=new ClassPathXmlApplicationContext("beans.xml");
        }
    
    }
    

      

    输出

    xxx.DefaultListableBeanFactory - Creating shared instance of singleton bean 'rFactory'
    RumenzA 无参构造方法
    xxx.DefaultListableBeanFactory - Creating shared instance of singleton bean 'rFactory1'
    ID构造方法
    

      

    实例工厂实例化

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd">
        <bean  id="rFactory" class="com.rumenz.RumenzFactory" />
        <bean id="rumenz" factory-bean="rFactory" factory-method="rumenzFactory"></bean>
        <bean id="rumenz1" factory-bean="rFactory" factory-method="rumenzFactory">
            <constructor-arg name="id" value="666"></constructor-arg>
        </bean>
    </beans>
    

      

    RumenzFactory.java

    package com.rumenz;
    public class RumenzFactory {
        //不能用静态方法
        public  RumenzA rumenzFactory(){
            return new RumenzA();
        }
        public  RumenzA rumenzFactory(String id){
            return new RumenzA(id);
        }
    }
    

      

    DemoApplication.java

    package com.rumenz;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class DemoApplication {
        public static  void main(String[] args) {
            ClassPathXmlApplicationContext  ca=new ClassPathXmlApplicationContext("beans.xml");
            //RumenzA rumenzA=(RumenzA)ca.getBean("rFactory1");
        }
    
    }
    

      

    输出

    xxx.DefaultListableBeanFactory - Creating shared instance of singleton bean 'rumenz'
    RumenzA 无参构造方法
    xxx.DefaultListableBeanFactory - Creating shared instance of singleton bean 'rumenz1'
    ID构造方法
    

      

    通过FactoryBean实例化

    beans.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd">
        
        <bean id="rumenz-by-factoryBean"  class="com.rumenz.RumenzAFactoryBean"/>
    </beans>
    

      

    RumenzAFactoryBean.java

    package com.rumenz;
    
    import org.springframework.beans.factory.FactoryBean;
    
    public class RumenzAFactoryBean implements FactoryBean {
        @Override
        public Object getObject() throws Exception {
            return RumenzA.createRumenzA();
        }
        @Override
        public Class<?> getObjectType() {
            return RumenzA.class;
        }
    }
    

      

    DemoApplication.java

    package com.rumenz;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class DemoApplication {
        public static  void main(String[] args) {
            ClassPathXmlApplicationContext  ca=new ClassPathXmlApplicationContext("beans.xml");
            RumenzA rumenzA=(RumenzA)ca.getBean("rumenz-by-factoryBean");
        }
    
    }
    

      

    输出/异步加载bean

    xxx.DefaultListableBeanFactory - Creating shared instance of singleton bean 'rumenz-by-factoryBean'
    RumenzA 无参构造方法
    

      

    最后

    大家看完有什么不懂的可以在下方留言讨论,也可以关注我私信问我,我看到后都会回答的。也欢迎大家关注我的公众号:前程有光,金三银四跳槽面试季,整理了1000多道将近500多页pdf文档的Java面试题资料,文章都会在里面更新,整理的资料也会放在里面。谢谢你的观看,觉得文章对你有帮助的话记得关注我点个赞支持一下!

  • 相关阅读:
    Python3.7 练习题(-) 如何使用Python生成200个优惠卷(激活码)
    Could not find a version that satisfies the requirement PIL
    python中如何对待易过期的cookies
    python代码在linux服务器一般的开头
    mysql innodb引擎 一次线上死锁分析排查步骤
    centos 6.5 gogs迁移外部仓库报错
    mysql 存儲emjoy表情是報錯Incorrect string value:
    python开发技巧---列表、字典、集合值的过滤
    zabbix学习-如何部署一个agent客户端
    zabbix学习-zabbix安装
  • 原文地址:https://www.cnblogs.com/lwh1019/p/13347639.html
Copyright © 2011-2022 走看看