Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'App' is defined

工具:Eclipse mars

环境:jdk1.8

说明:这是在学习Spring Task时遇到的一个bug,代码如下:

定时任务类:

package com.task.test;

import java.util.Date;
import org.springframework.stereotype.Component;

@Component
public class App {

    public void execute1(){
        System.out.printf("Task: %s, Current time: %s\n", 1, new Date() +" current thread :" +Thread.currentThread().getName());
    }

    public void execute2(){
        System.out.printf("Task: %s, Current time: %s\n", 2, new Date() +" current thread :" +Thread.currentThread().getName());
    }

}

Spring配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd">

    <!-- 配置注解扫描 -->
    <context:component-scan base-package="com.task.test"/>

    <task:scheduler id="taskScheduler" pool-size="100" />

    <task:scheduled-tasks scheduler="taskScheduler">
        <!-- 即刻开始隔5秒钟触发任务一次 -->
        <task:scheduled ref="App" method="execute1" cron="0/5 * * * * ?"/>
        <!-- 即刻开始,隔10秒钟触发任务一次 -->
        <task:scheduled ref="App" method="execute2" cron="0/10 * * * * ?"/>
    </task:scheduled-tasks>

</beans>

测试代码:

package com.task.test;

import java.util.Date;
import org.springframework.stereotype.Component;

@Component
public class App {

    public void execute1(){
        System.out.printf("Task: %s, Current time: %s\n", 1, new Date() +" current thread :" +Thread.currentThread().getName());
    }

    public void execute2(){
        System.out.printf("Task: %s, Current time: %s\n", 2, new Date() +" current thread :" +Thread.currentThread().getName());
    }

}

操作:右键Eclipse编辑区--->点击Run As--->Java Application

报错:

原因:spring配置文件中的 ref 属性值得命名格式应该是驼峰命名法,即将spring配置文件中的ref="App"改为ref="app"即可。

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'App' is defined

时间: 2024-12-20 01:15:44

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'App' is defined的相关文章

(待解答)Spring报错:Exception in thread &quot;main&quot; org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named &#39;car&#39; is defined

新建了一个Spring工程,有2个类:HelloWorld.Car.在Main.java文件里获取类时,发现只能获取到HelloWorld类,不能获取到Car类. HelloWorld.java: 1 package com.tt.spring.beans; 2 3 public class HelloWorld { 4 5 private String name; 6 7 public void setName(String name) { 8 System.out.println("hell

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named &#39;testService&#39; is defined

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'testService' is defined(1)这个错误的原因是编译器找不到bean 的注入,还可能是你的注入名字写错了. (2)spring的配置文件 书写错误,没有加入自动扫描功能 这句<context:component-scan base-package="包名"></context:compone

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named &#39;springSessionRepositoryFilter&#39; is defined

spring-session 集成redis,web.xml配置filter时候出现  No bean named 'springSessionRepositoryFilter' is defined 从spring给出的异常可以看出是没有取到这个bean,然后纠结了半天,各种版本切换找问题依旧未解决,然后Google找了半天, 也是没有找到问题解决方式,stackoverflow上也有许多人提出该问题,但是看了下面的解决方式,然并卵,一个都没用... 只能试着自己解决了. 既然没找到bean,

报错!!!!!!!!!!!org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named &#39;springSessionRepositoryFilter&#39; is defined

报错!!!!!!!!!!! 因用maven项目不是很熟练,经常在Maven转Web项目(为什么要转web项目?因为要在tomcat中跑起来.maven项目好像是可以直接部署到tomcat的,或集成tomcat插件,还没尝试过)的时候会出现很奇怪的问题.下面的一个报错就是最近经常遇到的一个问题.因为每次遇到它时,都会花很长时间去排查,所以在此把它记录下来,以便后面再遇到此问题时,顺利过坑.报错如下: org.springframework.beans.factory.NoSuchBeanDefin

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named &#39;ExtractAbstractServiceHandler&#39; is defined

在利用 Spring 集成 thrift 时,报错如下: javax.servlet.ServletException: Servlet.init() for servlet search-nlp-service threw exception org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) org.apache.catalina.valves.ErrorReportVa

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named &#39;sessionFactory&#39; is defined

如果之前所有测试都通过,并且保证测试sessionFactory可以创建成果,运行项目访问页面后报如下错 问题基本可能出现在web.xml的spring上下文加载的路径问题上. org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sessionFactory' is defined org.springframework.beans.factory.NoSuchBeanDefinition

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException

1.错误描写叙述 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonaming ] { -help | start | stop } 2014-7-13 18:02:58 org.apache.catalina.core.AprLifecycleListener init 信息: Loaded APR based Apache Tomcat Native library 1.1.29 usi

Caused by: org.springframework.beans.factory.BeanCreationException

1.错误原因 2014-7-13 17:36:57 org.apache.jasper.compiler.TldLocationsCache tldScanJar 信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were fou

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field

1 错误描述 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.sc.oa.test.timer.dao.TimerDaoTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationExcep