当前位置: 首页 > 知识库问答 >
问题:

如何解决此bean定义重写?

商俊智
2023-03-14

我已经从Spring Boot 1.5升级到Spring Boot 2.1.8。我做了一些测试,但现在失败了。我还在2.9版本中使用maven surefire插件,它也能正常工作,但如果有必要的话,我也将其升级到了2.22.0。

java prettyprint-override">@ExtendWith(SpringExtension.class)
@WebMvcTest(value = ElementController.class, secure = false)
@ContextConfiguration(classes = TestSite1Config.class)
public class ElementControllerSite1IT {
  @Autowired
  protected MockMvc mvc;
  @MockBean
  ElementService elementService;
  @BeforeEach
  public void setup() {
    when(elementService.getElementTable( ... )) //skipping args for brevity
       .thenReturn(new ElementTable());
  }

  @Configuration
  public static class TestSite1Config {
    @Bean
    @Autowired
    public ElementController elementController(final ElementService elementService) {
      return new ElementController(elementService, new ElementControllerProperties(DeploymentLocation.SITE1));
  }

  @Test
  public void failSite1ValidationWithoutId() throws Exception {
    ElementParameters params = getParams(false);
    mvc.perform(post("/element")
      .contentType(JSON)
      .andExpect(status().isBadRequest());
  }

  //more tests, but doesn't matter.
}

还有另一个类似于上述的类,但将Site1替换为Site2。

有一个ElementController

我得到这个例外:

Caused by BeanDefinitionOverrideException: Invalid bean definition with name 'elementController' defined in class path resource [ui/v2/web/ElementControllerSite1IT$TestSite1Config.class]: Cannot register bean definition [Root bean: class [null]; ... defined in class path resource [ui/v2/web/ElementControllerSite1ITConfig.class] for bean 'elementController': There is already [Generic bean: class [ui.v2.web.ElementController]; .. defined in file [...ui/v2/web/ElementController.class]] bound.

我没有编写测试,这是我继承的代码,在一个代码库中,我只是在后台处理。

共有3个答案

袁波
2023-03-14

让它与此一起工作:(对于任何偶然发现这个问题的人)

@ExtendWith(SpringExtension.class)
@AutoConfigureMockMvc
@WebMvcTest
@ContextConfiguration(classes = {ElementController.class,TestSite1Config.class})
public class ElementControllerSite1IT {
  @Autowired
  private MockMvc mvc;
  ...
  @Configruation
  public static class TestSite1Config {
    @Bean
    @Primary
    public ElementControllerProperties elementControllerProperties() { return ... }
  }
  ...
}
冉丰茂
2023-03-14

将spring.main.allow定义覆盖到application.properties

甘明朗
2023-03-14

您可以尝试@TestProperty tySource(属性="..."

@ExtendWith(SpringExtension.class)
@WebMvcTest(value = ElementController.class, secure = false)
@ContextConfiguration(classes = TestSite1Config.class)
@TestPropertySource(properties = {"spring.main.allow-bean-definition-overriding=true", "local.server.port=7777"})
public class ElementControllerSite1IT {
 ...
}
 类似资料:
  • 实现一个以整数为参数的静态方法,使用Math类计算给定整数的整数平方根。如果整数为负数,则取其正数的平方根,然后返回结果的负数,表示它是一个虚数 我一直在尝试,一直到这里

  • 问题内容: 我在下一行出现错误。我正在做添加到jsonarray的过程。请帮我。 问题答案: 这是我在重新编程时经常遇到的错误。此异常的原因或细节非常清楚。不允许在迭代时修改集合(正在添加新元素)。至少不支持语法。 为了解决您的问题,我认为有两种方法很简单。 1)。而不是使用语句来循环,更好的方法是使用迭代器来避免ConcurrentModificationException。 2)。在循环播放时

  • 问题内容: DeprecationWarning:需要一个整数(got类型为float)。不建议使用隐式转换为整数,并且在将来的Python版本中可能会删除隐式转换。 DeprecationWarning:需要一个整数(got类型为float)。不建议使用隐式转换为整数,并且在将来的Python版本中可能会删除隐式转换。 问题答案: 警告与的坐标参数有关。浮点坐标将表示的原点位于窗口像素之间。那没

  • 我的配置如下所示: 在应用程序上下文启动期间,我会在日志中收到如下内容: [INFO][]重写bean“ReconficationJob”的bean定义:替换[Generic bean:class[org.springframework.batch.core.configuration.xml.SimpleFlowFactoryBean];scope=;abstract=false;lazyIni

  • 我是Spring新手,我刚开始一个Spring MVC CRUD程序,但在尝试了很多方法之后,我一次又一次地面临同样的错误。 这是打印HTTP状态500–内部服务器错误的第一个异常。 ServletException 不满意的依赖 菜豆 聚甲醛 我还更改了依赖项版本,但它仍然不起作用。这是我的代码 配置类 配置初始化器 I数据库配置 我使用Hibernate数据库 模型类 服务类服务类 抽象类 我