猿问

当有两个相同名称和不同类的 bean 时,如何按名称获取正确的 bean

我有两个春豆。它们名称相同,但类别不同。


这是bean定义。


这是第一个。


@Bean(name = "approve_sign_up_project_request|Task_1tm7e53")

    public StudentTaskToResponseDataConverter perfectUserProfileVO() {

        return studentTaskVO -> {

            ResponseVo vo = toResponseVO(studentTaskVO);

            vo.setData(new PerfectUserProfileVO());


            return vo;

        };

    }

这是第二个


@Bean(name = "approve_sign_up_project_request|Task_1tm7e53")

    public UserTaskCompleter perfectUserProfile() {

        return new UserTaskCompleter() {

            @Override

            public void validate(Task task, CompleteUserTaskDTO dto) throws RuntimeException {

                Long studentId = getStudentId(task);

                UserProfileIntegrityValidatedResultDTO results = userService.

                        validateTheIntegrityOfUserProfile(studentId);

                if (results.getComplete()) {

                    //nothing to do for now

                }else {

                    LOGGER.error("Validated failed cause the student -- {} not yet perfected the profile",

                            studentId);

                    throw new UserProfileImperfectException("Missing fields are " + results.getMissingFields());

                }

            }


            @Override

            public void executeBusinessLogic(Task task, CompleteUserTaskDTO dto) {


            }


            @Override

            public Map<String, Object> getTheVariablesForCompleterUserTask(Task task, CompleteUserTaskDTO dto) {

                return null;

            }

        };

    }

当我使用下面的代码来获取 bean 时,spring 会抛出异常。但我不明白其中的原因。我想当我使用bean名称和bean类来获取它时,spring会给我正确的bean。但实际上我错了,春天没有给它。


这是获取bean的代码


private UserTaskCompleter getBean(CompleteUserTaskDTO dto) {

        String beanName = dto.getProcessDefinitionKey() + "|" + dto.getActivityId();

        return applicationContext.getBean(beanName, UserTaskCompleter.class);

    }



qq_遁去的一_1
浏览 404回答 2
2回答

慕的地6264312

Himly 引用的答案,不会让 Spring 创建同名的 bean。它实际上阻止它启动,因为构建应用程序将失败。如果多个 bean 被定义为同名,那么稍后定义的 bean 将覆盖之前定义的 bean。因此,在您的情况下,只有一个名为的 beanapprove_sign_up_project_request|Task_1tm7e53将存在,除非您禁用 bean 定义覆盖。

慕村9548890

我已经明白原因了。当定义两个同名不同类型的bean时。春天会选择最后一个来压倒其他的。就我而言,只有一个名为“approve_sign_up_project_request|Task_1tm7e53”的 bean,类型为 StudentTaskToResponseDataConverter。当我使用名称和 UserTaskCompleter 类型来获取 bean 表单 beanFactory 时,spring 找不到它,然后 spring 将抛出异常。如何让spring创建同名bean?我从这里找到答案这是答案的导入部分您可以在构建 Spring Boot 应用程序时使用初始化程序:@SpringBootApplicationpublic class SpringBootApp {&nbsp; &nbsp; public static void main(String... args) {&nbsp; &nbsp; &nbsp; &nbsp; new SpringApplicationBuilder(SpringBootApp.class)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .initializers(new ApplicationContextInitializer<GenericApplicationContext>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void initialize(GenericApplicationContext applicationContext) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; applicationContext.setAllowBeanDefinitionOverriding(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; .run(args);&nbsp; &nbsp; }}或者使用 java 8:new SpringApplicationBuilder(SpringBootApp.class)&nbsp; &nbsp; .initializers((GenericApplicationContext c) -> c.setAllowBeanDefinitionOverriding(false) )&nbsp; &nbsp; .run(args);
随时随地看视频慕课网APP

相关分类

Java
我要回答