What is the Spring equivalent to FactoryModuleBuilder, @AssistedInject, and @Assisted in Guice?

前端 未结 2 2226
没有蜡笔的小新
没有蜡笔的小新 2021-02-20 04:12

What is the Spring Framework equivalent to FactoryModuleBuilder, @AssistedInject, and @Assisted in Google Guice? In other words, what is the recommended approach using Spring t

2条回答
  •  佛祖请我去吃肉
    2021-02-20 04:32

    I'm not entirely certain that this question is a dupe, (only 90% sure), but this answer:

    https://stackoverflow.com/a/13243066/1768232

    Seems to have the information you need. Specifically, you should do this:

    I got it working by fetching an instance of the bean used in the constructor-arg out of the context and then populating it with the values that you are working with at run-time. This bean will then be used as the parameter when you get your factory-generated bean.

    public class X {
       public void callFactoryAndGetNewInstance() {
          User user = context.getBean("user");
          user.setSomethingUsefull(...);
          FileValidator validator = (FileValidator)context.getBean("fileValidator");
          ...
       }
    }
    

    I recommend reading the entire answer.

提交回复
热议问题