1. 程式人生 > >Spring學習記錄--factory-method 靜態工廠方法注入bean

Spring學習記錄--factory-method 靜態工廠方法注入bean

例項專案結構

我們編寫好兩個類,要求是其中一個類的屬性是另一個類。例項是阿中包含使用者。

A.java

package com.dybeq.pojo;

/**
 * @author DybeQ
 * @since 2018-8-3
 */
public class A {
    private User user;
    private String name;

    private A(User user, String name) {
        this.user = user;
        this.name = name;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public static A createInstance(User user, String name){
        A a = new A(user, name);
        return a;
    }

    @Override
    public String toString() {
        return "A{" +
                "user=" + user +
                ", name='" + name + '\'' +
                '}';
    }
}

User.java

package com.dybeq.pojo;

/**
 * @author DybeQ
 * @since 2018-8-3
 */
public class User {
    private String name;
    private String gender;
    private int age;

    public User(String name, String gender, int age) {
        this.name = name;
        this.gender = gender;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", age=" + age +
                '}';
    }
}

applicationContext.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 http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="dybeq" class="com.dybeq.pojo.User">
        <constructor-arg name="age" value="20"/>
        <constructor-arg name="gender" value="male"/>
        <constructor-arg name="name" value="DybeQ"/>
    </bean>

    <bean id="a" class="com.dybeq.pojo.A" factory-method="createInstance">
        <constructor-arg ref="dybeq"/>
        <constructor-arg value="CSA"/>
    </bean>
</beans>

測試

Test.java

package com.dybeq.test;

import com.dybeq.pojo.A;
import com.dybeq.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author DybeQ
 * @since 2018-8-3
 */
public class Test {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        User user = (User) applicationContext.getBean("dybeq");
        A a = (A) applicationContext.getBean("a");
        System.out.println(user.getName());
        System.out.println(a.getUser() + " loves " + a.getName());
    }
}

成功輸出。

總結

  1. 工廠方法方法必須是靜態
  2. 在的applicationContext.xml中,製造工廠法方法的引數是由構造精氨酸提供的
  3. 彈簧官方文件中所述“工廠方法返回的類的型別不必與包含靜態工廠方法的類相同”,我們可知工廠方法返回的型別可以與包含這個方法的類的型別不一致