Spring中bean的parent、depends-on、factory-bean、factory-method属性

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属性。




    
        
        
    
    
        
    
    
        
    

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




    
    

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




    
        
    
    
    

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、factory-bean、factory-method属性)