为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

qq_遁去的一_1 2021-10-20 11:33:58
我有两个春豆。它们名称相同,但类别不同。这是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);    }
查看完整描述

2 回答

?
慕的地6264312

TA贡献1817条经验 获得超6个赞

Himly 引用的答案,不会让 Spring 创建同名的 bean。它实际上阻止它启动,因为构建应用程序将失败。

如果多个 bean 被定义为同名,那么稍后定义的 bean 将覆盖之前定义的 bean。因此,在您的情况下,只有一个名为的 beanapprove_sign_up_project_request|Task_1tm7e53将存在,除非您禁用 bean 定义覆盖。


查看完整回答
反对 回复 2021-10-20
?
慕村9548890

TA贡献1884条经验 获得超4个赞

我已经明白原因了。


当定义两个同名不同类型的bean时。春天会选择最后一个来压倒其他的。


就我而言,只有一个名为“approve_sign_up_project_request|Task_1tm7e53”的 bean,类型为 StudentTaskToResponseDataConverter。


当我使用名称和 UserTaskCompleter 类型来获取 bean 表单 beanFactory 时

,spring 找不到它,然后 spring 将抛出异常。


如何让spring创建同名bean?


我从这里找到答案


这是答案的导入部分


您可以在构建 Spring Boot 应用程序时使用初始化程序:


@SpringBootApplication

public class SpringBootApp {


    public static void main(String... args) {

        new SpringApplicationBuilder(SpringBootApp.class)

            .initializers(new ApplicationContextInitializer<GenericApplicationContext>() {

                @Override

                public void initialize(GenericApplicationContext applicationContext) {

                    applicationContext.setAllowBeanDefinitionOverriding(false);

                }

            })

        .run(args);


    }

}

或者使用 java 8:


new SpringApplicationBuilder(SpringBootApp.class)

    .initializers((GenericApplicationContext c) -> c.setAllowBeanDefinitionOverriding(false) )

    .run(args);


查看完整回答
反对 回复 2021-10-20
  • 2 回答
  • 0 关注
  • 403 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信