舉例說明如下:
步驟1:定義一個接口
public interface IPerson { void doWork(); }
步驟2:對該接口做第一個實現類
import org.springframework.stereotype.Component; @Component("student") public class StudentImpl implements IPerson { @Override public void doWork() { System.out.println("I am studying"); } }
步驟3:對該接口做第二個實現類
import org.springframework.stereotype.Component; @Component("teacher") public class TeacherImpl implements IPerson { @Override public void doWork() { System.out.println("I am teaching"); } }
步驟4:使用@Autowired對List和Map進行注入使用
import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class PersonService { @Autowired List<IPerson> persons; @Autowired Map<String, IPerson> personMaps; public void echo() { System.out.println("print list:"); for (IPerson p : persons) { p.doWork(); } System.out.println("\nprint map:"); for (Map.Entry<String, IPerson> entry : personMaps.entrySet()) { System.out.println("Person:" + entry.getKey() + ", " + entry.getValue()); } } }
步驟5:編寫啟動類調用PersonService的echo()函數進行測試
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication springApplication = new SpringApplication(Application.class); ApplicationContext context=springApplication.run(args); PersonService service=context.getBean(PersonService.class); service.echo(); } }
程序運行結果為:
print list:
I am studying
I am teaching
print map:
Person:student, com.tang.aaa.StudentImpl@723e88f9
Person:teacher, com.tang.aaa.TeacherImpl@5f0fd5a0
二、策略模式:根據配置使用對應的實現類
對應Map的注入,key必須為String類型,即bean的名稱,而value為IPerson類型的對象實例。
通過對上述Map類型的注入,可以改寫為根據bean名稱,來獲取並使用對應的實現類。
舉例如下:
步驟1:修改上述步驟4中的PersonService類如下:
import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class PersonService { @Autowired Map<String, IPerson> personMaps; public void work(String name) { IPerson person=personMaps.get(name); if(null!=person) { person.doWork(); } } }
步驟2:通過對PersonServer的work()傳遞不同的參數,實現對不同實現類的調用
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication springApplication = new SpringApplication(Application.class); ApplicationContext context=springApplication.run(args); PersonService service=context.getBean(PersonService.class); service.work("teacher"); } }
我們可以使用service.work("teacher")或者service.work("student")來調用不同的實現類,即達到設計模式中策略模式的類似效果。
原文鏈接:https://blog.csdn.net/inrgihc/article/details/104742206
補充:若使用構造方法注入,則不需要加@Autowired注解,並允許定義為final:
private final Map<String, IPerson> personMaps;
public PersonService(Map<String,IPerson> personMaps){
this.personMaps = personMaps;
}