Java Spring启动服务为空

Java Spring启动服务为空,java,spring,spring-boot,null,Java,Spring,Spring Boot,Null,我目前正在使用Spring Boot,创建CommandLineRunner。在我尝试@Autowired我的类之前,一切都很正常:它们总是空的,并且从Spring得到了相同的错误:“创建名为'initBatch'的bean时出错”:自动连接依赖项的注入失败: 找不到[Utils]类型的符合依赖项条件的bean:应至少有1个bean符合此依赖项的autowire候选项。依赖项批注。我仍然无法找出发生此错误的原因。 这是我的代码: @SpringBootApplication public cla

我目前正在使用Spring Boot,创建CommandLineRunner。在我尝试@Autowired我的类之前,一切都很正常:它们总是空的,并且从Spring得到了相同的错误:“创建名为'initBatch'的bean时出错”:自动连接依赖项的注入失败: 找不到[Utils]类型的符合依赖项条件的bean:应至少有1个bean符合此依赖项的autowire候选项。依赖项批注。我仍然无法找出发生此错误的原因。 这是我的代码:

@SpringBootApplication
public class InitBatch implements CommandLineRunner {

@Autowired
private Utils Utils;

@Override
public void run(String... args) throws Exception {
    System.out.println("Hello World");          
}

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


@Bean
public ReloadableResourceBundleMessageSource messageSource() {
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();

       messageSource.setBasename("instances");
    return messageSource;
}
这是导致问题的Utils类:

@Configurable
@Service
public class Utils { 

private static final Logger LOG = LoggerFactory.getLogger(Utils.class);

     //NUMEROUS METHODS...
 }
另外,我还有另一个Init,它将de-app作为WS加载。在服务器上运行所有东西,同样的类工作得很好。这是另一个正在工作的Init:

@Configuration
@ComponentScan({ "ws.controller","ws.service",
"ws.dao", "ws.util", "ws.filtro",
"ws.repository", "ws.model.log", "ws.logger.impl"})
@EnableAutoConfiguration
public class Init extends SpringBootServletInitializer { 

private static final int SECS = 10;

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(Init.class);
}

/**
 * Main method.
 *
 * @param args String[].
 * @throws Exception Exception.
 */
public static void main(String[] args) throws Exception {
    SpringApplication.run(Init.class, args);
}


@Bean
public ReloadableResourceBundleMessageSource messageSource() {
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();

    messageSource.setBasename("instances");
    messageSource.setCacheSeconds(SECS);
    return messageSource;
}
}
我只是不明白为什么在Init.java(作为tomcat web应用程序)中使用相同的文件和相同的配置,但在CommandLineRunner中所有相同的文件都是空的

有什么建议吗


谢谢!

需要指定
ComponentScan
注释以及
InitBatch
处的包(作为不同包中的
Utils
类),以便在运行时扫描bean

@Configuration
@ComponentScan("ws.util")
@EnableAutoConfiguration
public class InitBatch implements CommandLineRunner {
...
}
@SpringBootApplication
文档-

许多Spring Boot开发人员总是对其主类进行注释 使用@Configuration、@EnableAutoConfiguration和@ComponentScan。 因为这些注释经常一起使用(特别是在 如果您遵循上面的最佳实践),Spring Boot将提供 方便的@springboot应用程序替代方案


你说得对,我之前的测试中使用了这个配置。如果我使用@ComponentScan,它的效果会很好。谢谢!