错误信息

java.lang.IllegalStateException: Failed to load ApplicationContext

在进行测试数据库连接的时候,获取数据库连接失败。

其中applicationContext.xml中导入了spring-persist-mybatis.xml文件

spring-persist-mybatis.xml文件中创建了dataSource <bean>标签。

// 在类上标记必要的注解,Spring整合Junit
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"}) // 需要在pom中引入spring依赖
public class CrowdTest {
    // 采用注解(Spring整合)的好处就是,IOC容器中有啥,在test中就可以直接用
    @Autowired
    @Qualifier("dataSource")
    private DataSource dataSource;

    @Test
    public void testConnection() throws SQLException {
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
    }
}

错误原因

spring-persist-mybatis.xml<bean>容器 <property>配置错误

解决方案

name改正确即可

另外可能的解决方案

在"classpath:applicationContext.xml" 的 classpath加个 既classpath*就可以了*(参考:failed to load applicationcontext解决

Logo

一站式 AI 云服务平台

更多推荐