• SpringBoot中 使用@Autowired 将bean注入到List或Map等集合中


    举例说明如下:

    步骤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("
    print 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;
    }
  • 相关阅读:
    取石子(斐波那契博弈)
    Kindergarten(网络流解法)
    最大团的一些定理
    Escape(多记一个方向状态的BFS)迷宫逃脱
    网络流的一些定理
    线段树维护动态连续子段HDU1540
    最大流Dinic(模板)
    MCMF最大流最小割(模板)Dijkstra负权优化
    Exchanging Gifts--2019CCPC哈尔滨 E题
    A<=B的前提下全排列A使答案尽量大
  • 原文地址:https://www.cnblogs.com/fengwenzhee/p/14979907.html
Copyright © 2020-2023  润新知