spring - 升级到 Spring Boot 1.3 导致 java.lang.NoSuchMethodError : org. springframework.beans.factory.config.ConfigurableBeanFactory.getSingletonMutex()

标签 spring spring-mvc spring-boot

运行测试,堆栈跟踪完全在 Spring 框架和 jUnit 中:

java.lang.NoSuchMethodError: org.springframework.beans.factory.config.ConfigurableBeanFactory.getSingletonMutex()Ljava/lang/Object;
    at org.springframework.context.event.AbstractApplicationEventMulticaster.setBeanFactory(AbstractApplicationEventMulticaster.java:86)
    at org.springframework.boot.context.event.EventPublishingRunListener.registerApplicationEventMulticaster(EventPublishingRunListener.java:81)
    at org.springframework.boot.context.event.EventPublishingRunListener.contextPrepared(EventPublishingRunListener.java:71)
    at org.springframework.boot.SpringApplicationRunListeners.contextPrepared(SpringApplicationRunListeners.java:60)
    at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:329)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:295)
    at org.springframework.boot.test.SpringApplicationContextLoader.loadContext(SpringApplicationContextLoader.java:98)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:228)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:230)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:249)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

测试类非常简单:

package com.uss.identity.dao;

import com.uss.identity.TestApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import static org.junit.Assert.assertNull;

@SuppressWarnings("SpringJavaAutowiredMembersInspection")
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {TestApplication.class})
@EnableTransactionManagement
@ActiveProfiles("test-flyway")
public class ClientRepoTest2 {

    @Autowired
    ClientRepository dao;

    @Test
    public void testFindNone() throws Exception {
        assertNull(dao.findOne("nonesuch"));
    }

}

引用的 TestApplication 类就是:

package com.uss.identity;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Profile;

@SpringBootApplication
@Profile({"test", "test-flyway"})
public class TestApplication {

}

使用 Spring Boot 1.2.8.RELEASE 时效果很好。但尝试升级到1.3.0.RELEASE会导致上述异常。

最佳答案

您应该使用 Spring Boot 的依赖管理来确保您获得所有 Spring Framework 模块的一致且正确的版本。您可以使用spring-boot-starter-parent来做到这一点作为你的 pom 的父级:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.0.RELEASE</version>
</parent>

或者将其导入到您的 pom 的 <dependency-management> 中部分:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>1.3.0.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

更多信息请参阅 dependency management section Spring Boot 文档。

关于spring - 升级到 Spring Boot 1.3 导致 java.lang.NoSuchMethodError : org. springframework.beans.factory.config.ConfigurableBeanFactory.getSingletonMutex(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34191085/

相关文章:

java - TestExecutionListeners 注释防止连接 spring bean

java - Jhipster:应该为用户文件设置什么路径

java - 通用抽象类的自定义 Jackson 反序列化

java - Spring MVC 与 Soap 服务多线程和多服务器

java - Spring MVC UTF-8 字符编码

java - 当我将 global-method-security 添加到 spring-servlet.xml 时出现 Spring MVC 异常

intellij-idea - 无法从 Spring Boot、Gradle 和 IntelliJ-Idea 生成可执行 jar

java - 创建名称为 'sessionFactory' 的 bean 时出错无法实例化默认 tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]

java - Spring Boot : java. sql.SQLException:用户访问被拒绝

java - 如何在Spring中使用注释动态 Autowiring bean?