Spring dynamic injection, factory-like pattern

前端 未结 3 1053
情话喂你
情话喂你 2021-01-31 20:58

A continuation from Dependency injection, delayed injection praxis. I have the Main class:

package test;

import org.springframework.beans.factory.annotation.Aut         


        
3条回答
  •  臣服心动
    2021-01-31 21:52

    If you want some member of your class to be dynamically initialized\populated on every call to the corresponding getter, you can try the Lookup Method Injection. Read pp. 3.3.4.1 here.

    So even if the class that contains the dynamic member was created in scope=singletone (the default for spring bean container) every time you will accessthe field that has a lookup method assigned, you will get an appropriate object according to the business logic implemented inside the lookup method. In your case the list is an interface so you can easily implement the validation inside your lookup method and return a validated list.

    Edit:

    I found better example in Spring documentation - I think it is very clear. Take a look at "3.4.6.1 Lookup method injection"

    When you configure the Main class assign a lookup method to its List member - it will be called whenever you need a new instance of the List bean.

    Good luck!

提交回复
热议问题