Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring在基本Spring启动应用程序中找不到bean_Java_Spring_Spring Boot - Fatal编程技术网

Java Spring在基本Spring启动应用程序中找不到bean

Java Spring在基本Spring启动应用程序中找不到bean,java,spring,spring-boot,Java,Spring,Spring Boot,我是Spring Boot新手,将应用程序与DemoApplication(主类)和CodeChallenge类放在一起。这两个类都在同一个文件夹中。我将CodeChallenge类自动连接到主类中,并使用@EventListener(ApplicationReadyEvent.class)以便在编译时触发该类。但是,每当我编译应用程序时,都会出现以下错误: Field codechallenge in com.example.demo.DemoApplication required a be

我是Spring Boot新手,将应用程序与DemoApplication(主类)和CodeChallenge类放在一起。这两个类都在同一个文件夹中。我将CodeChallenge类自动连接到主类中,并使用
@EventListener(ApplicationReadyEvent.class)
以便在编译时触发该类。但是,每当我编译应用程序时,都会出现以下错误:

Field codechallenge in com.example.demo.DemoApplication required a bean of 
type 'com.example.demo.CodeChallenge' that could not be found.
如何成功配置此应用程序,以便在编译程序时避免此错误并运行CodeChallenge类和testMethod

DemoApplication类是:

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;

@SpringBootApplication    
public class DemoApplication {

    @Autowired
    private CodeChallenge codechallenge;  

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

      @EventListener(ApplicationReadyEvent.class)
      public void callTestMethod() {
          codechallenge.testMethod();   
      }

}
package com.example.demo;

public class CodeChallenge {
    public void testMethod() {          
        System.out.println("hello world");
    }
}
CodeChallenge类是:

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;

@SpringBootApplication    
public class DemoApplication {

    @Autowired
    private CodeChallenge codechallenge;  

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

      @EventListener(ApplicationReadyEvent.class)
      public void callTestMethod() {
          codechallenge.testMethod();   
      }

}
package com.example.demo;

public class CodeChallenge {
    public void testMethod() {          
        System.out.println("hello world");
    }
}

您必须在
公共类CodeChallenge
上添加
@Service
@Component
注释,让Spring知道这是一个bean

@Service
public class CodeChallenge {
    public void testMethod() { 
        System.out.println("hello world");
    }
}

您必须在
公共类CodeChallenge
上添加
@Service
@Component
注释,让Spring知道这是一个bean

@Service
public class CodeChallenge {
    public void testMethod() { 
        System.out.println("hello world");
    }
}