美文网首页
Spring中bean的parent、depends-on、fa

Spring中bean的parent、depends-on、fa

作者: 慢游世界 | 来源:发表于2019-03-31 19:35 被阅读0次

    1.parent

    配置了parent属性的bean可以继承指定的bean的属性。
    例子:
    Singer.java

    public class Singer {
    
        private String gender;
        private String country;
        private String name;
    
        // 省略setter和getter...
        @Override
        public String toString() {
            return "[" + country + " " + gender + " " + name + "]";
        }
    }
    

    context.xml,定义的Jane和Bibi都指定ChineseFemaleSinger为parent,她们将继承ChineseFemaleSinger的country和gender属性。

    <?xml version="1.0" encoding="UTF-8" ?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns="http://www.springframework.org/schema/beans"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" default-autowire="byName">
    
        <bean id="ChineseFemaleSinger" class="source.Singer" abstract="true" >
            <property name="country" value="中国"/>
            <property name="gender" value="女"/>
        </bean>
        <bean id="Jane" parent="ChineseFemaleSinger">
            <property name="name" value="张靓颖"/>
        </bean>
        <bean id="Bibi" parent="ChineseFemaleSinger">
            <property name="name" value="周笔畅"/>
        </bean>
    </beans>
    

    App.java

    public class App {
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("classpath:context.xml");
            Singer jane = (Singer)context.getBean("Jane");
            Singer bibi = (Singer)context.getBean("Bibi");
            System.out.println(jane);
            System.out.println(bibi);
        }
    }
    

    执行App的main方法,输出:

    [中国 女 张靓颖]
    [中国 女 周笔畅]
    

    2.depends-on

    配置了depends-on的bean会在被指定的bean初始化之后初始化,销毁前销毁。
    例子:
    BeanA.java

    public class BeanA {
    
        public void init() {
            System.out.println("beanA is initialized");
        }
    
        public void destroy() {
            System.out.println("beanA is destroyed");
        }
    }
    

    BeanB.java

    public class BeanB {
    
        public void init() {
            System.out.println("beanB is initialized");
        }
    
        public void destroy() {
            System.out.println("beanB is destroyed");
        }
    }
    

    context.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns="http://www.springframework.org/schema/beans"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" default-autowire="byName">
    
        <bean id="beanA" class="source.BeanA" init-method="init" destroy-method="destroy"/>
        <bean id="beanB" class="source.BeanB" depends-on="beanA" init-method="init" destroy-method="destroy"/>
    </beans>
    

    beanA和beanB都配置了init-method和destroy-method,在初始化和销毁时输出一些信息,以便判断初始化和销毁的顺序。
    App.java

    public class App {
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("classpath:context.xml");
            ((ClassPathXmlApplicationContext) context).close();
        }
    }
    

    执行main方法,输出

    beanA is initialized
    beanB is initialized
    beanB is destroyed
    beanA is destroyed
    

    可以看到,beanB在beanA初始化后初始化,在beanA销毁前销毁。

    3.factory-bean、factory-method

    factory-bean属性指定一个bean,factory-method指定某个返回一个对象的方法。
    例子:
    Phone.java

    public class Phone {
    
        private String brand;
        private String model;
        private String ram;
        private String cpu;
        private String assembledBy;
    
        @Override
        public String toString() {
            return "Phone{" +
                    "brand='" + brand + '\'' +
                    ", model='" + model + '\'' +
                    ", ram='" + ram + '\'' +
                    ", cpu='" + cpu + '\'' +
                    ", assembledBy='" + assembledBy + '\'' +
                    '}';
        }
        // 省略setter和getter
    }
    

    IphoneFactory.java

    public class IPhoneFactory {
    
        private String factoryName;
    
        public String getFactoryName() {
            return factoryName;
        }
    
        public void setFactoryName(String factoryName) {
            this.factoryName = factoryName;
        }
        public Phone getPhone() throws Exception {
            Phone p = new Phone();
            p.setBrand("APPLE");
            p.setCpu("A12");
            p.setModel("X");
            p.setRam("4GB");
            p.setAssembledBy(factoryName);
            return p;
        }
    }
    

    context.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns="http://www.springframework.org/schema/beans"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" default-autowire="byName">
    
        <bean id="iphoneFactory" class="source.IPhoneFactory">
            <property name="factoryName" value="Foxconn" />
        </bean>
        <bean id="iphone" factory-bean="iphoneFactory" factory-method="getPhone">
        </bean>
    </beans>
    

    App.java

    public class App {
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("classpath:context.xml");
            System.out.println(context.getBean("iphone"));
        }
    }
    

    执行main方法,输出

    Phone{brand='APPLE', model='X', ram='4GB', cpu='A12', assembledBy='Foxconn'}
    

    相关文章

      网友评论

          本文标题:Spring中bean的parent、depends-on、fa

          本文链接:https://www.haomeiwen.com/subject/eoqubqtx.html