spring实战4 Failed to load ApplicationContext

今天看到spring的自动装配组件,照着书上的敲了下来报错了

代码:

接口

public interface CompactDisc {

    void play();}
实现类
import org.springframework.stereotype.Component;

@Componentpublic class SgtPeppers implements CompactDisc {

    private String title = "命运交响曲";    private String artist = "Ludwig van Beethoven";    @Override    public void play() {        System.out.println("player:"+title+ " by " +artist);    }}

配置类
import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;

@Configuration@ComponentScanpublic class CDPlayerConfig {}

测试类
import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.junit.Assert.assertNotNull;

@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes = CDPlayerConfig.class)public class CDPlayerTest {

    @Autowired    private CompactDisc cd;

    @Test    public void cdShouldNotBeNull(){        assertNotNull(cd);    }

}
jdk1.7,spring4.3

然后执行测试类,错误

Error creating bean with name ‘org.springframework.context.annotation.internalAsyncAnnotationProcessor‘ defined in class path resource

[org/springframework/scheduling/annotation/ProxyAsyncConfiguration.class]: Bean instantiation via factory method failed; nested exception

is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.scheduling.annotation.AsyncAnnotatio

nBeanPostProcessor]: Factory method ‘asyncAdvisor‘ threw exception; nested exception is java.lang.IllegalArgumentException:

@EnableAsync annotation metadata was not injected

类中的方法 cdShouldNotNull,错误

java.lang.IllegalStateException: Failed to load ApplicationContext

错误是加载不到配置文件,明明用的javaconfig代替了xml文件了

@ContextConfiguration(classes = CDPlayerConfig.class)这是怎么回事,于是,我创建了一个ApplicationContext就试着写成了
@ContextConfiguration(value = "classpath:/ApplicationContext.xml")
方法报错,这次和上次不一样了,ApplicationContext加载到了,但是又无法创建bean了Error creating bean with name ‘CDPlayerTest‘: Unsatisfied dependency expressed through field ‘cd‘; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘CompactDisc‘ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}于是写成....
@ContextConfiguration(locations = {"classpath:/ApplicationContext.xml","classpath:/CDPlayerConfig.java"})还是报错...最后发现,是因为我把这几个类直接放到了src文件夹下的缘故,解决方法:把这些类都放到了新建的一个三级包下。书上的示例代码确实是放在了包下,这一点我没有注意,但也没想到这会有影响,细节还是很重要。

新人起步!欢迎指正!

原文地址:https://www.cnblogs.com/ffyg/p/10906041.html

时间: 2024-10-03 02:01:01

spring实战4 Failed to load ApplicationContext的相关文章

spring junit 做单元测试,报 Failed to load ApplicationContext 错误

spring junit 做单元测试,报 Failed to load ApplicationContext 错误. 查找了好一会,最后发现.@ContextConfiguration(locations = { "classpath:/spring/applicationContext.xml","classpath:/spring/app-config.xml", …… 改成 @ContextConfiguration(locations = { "c

spring中junit 提示Failed to load ApplicationContext

错误提示: 1:java.lang.IllegalStateException: Failed to load ApplicationContext 2:Error creating bean with name 'userService' defined in class path resource [UserService.xml]: Initialization of bean failed; nested exception is org.springframework.aop.fram

java.lang.IllegalStateException: Failed to load ApplicationContext(2)

错误提示:java.lang.IllegalStateException: Failed to load ApplicationContext Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [applicationContext.xml]: Error sett

SpringBoot报错:Failed to load ApplicationContext(javax.websocket.server.ServerContainer not available)

引起条件: WebSocket+单元测试,单元测试报错! 解决方法: SpringBootTest增加webEnvironment参数. https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications @SpringBootTest(webEnvironment = SpringBootT

Failed to load ApplicationContext

主要错误信息:java.lang.IllegalStateException: Failed to load ApplicationContext Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [h

java.lang.IllegalStateException: Failed to load ApplicationContext

1.错误描述 INFO:2015-02-05 22:14:21[main] - Loading XML bean definitions from class path resource [applicationContext.xml] INFO:2015-02-05 22:14:22[main] - JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning INFO:2015-02-05

junit测试时,出现java.lang.IllegalStateException: Failed to load ApplicationContext

课程设计要求进行junit测试,我是在已经做好的ssh项目上做的测试,测试类代码如下 package com.zhang.web.services; import static org.junit.Assert.*; import org.junit.After; import org.junit.Before; import org.junit.Test; import javax.annotation.Resource; import org.junit.runner.RunWith; im

spring quartz 定时任务“Failed to load class "org.slf4j.impl.StaticLoggerBinder”“Checking for available updated version of Quartz”

Failed to load class "org.slf4j.impl.StaticLoggerBinder 需要slf4j-api.jar.slf4j-log4j12.jar Checking for available updated version of Quartz 添加系统属性 System.setProperty("org.terracotta.quartz.skipUpdateCheck", "true"); spring quartz 定

常见Failed to load ApplicationContext异常解决方案!!

java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125) at org.springframework.test.context.suppo