以动态和惰性方式创建多个相同类的bean

时间:2018-07-24 16:45:06

标签: spring spring-boot

@ConfigurationProperties("person")
public class PersonConfiguration {

    private List<Details> details = new ArrayList<>();

    //getters and setters and tostring

    public static class Details {
        private String key;
        private String company;
        private String family;

        //getters and setters and tostring

    }
}

public class Person {

    public String key;
    public SomeClass obj;

    //constructor

    //getter, setter and tostring
}

@Component
public class PersonFactory {

    @Autowired
    PersonConfiguration personConfig;

    public Person getPerson(String key) {
        //if key exists in PersonConfiguration
            //create SomeClass based on family and compnay details for corresponding key
            //create Person bean using key and SomeClass.
    }
}

仅当基于请求中的不同键被请求时,如何才能以惰性方式创建上述类的Person的多个bean。

基本上有一个配置和工厂类。根据传入的请求,需要检查配置中是否存在该请求,是否存在,需要使用工厂类创建某种类型的对象。

0 个答案:

没有答案