Java Spring启动组件扫描不工作
我创建了一个示例Spring引导应用程序,当服务类都在一个包中时,该应用程序运行得非常好,但是如果一个服务类引用了另一个包中的另一个服务类,那么我会得到BeanCreationException 下面给出了应用程序的代码 Main.javaJava Spring启动组件扫描不工作,java,eclipse,spring,maven,spring-boot,Java,Eclipse,Spring,Maven,Spring Boot,我创建了一个示例Spring引导应用程序,当服务类都在一个包中时,该应用程序运行得非常好,但是如果一个服务类引用了另一个包中的另一个服务类,那么我会得到BeanCreationException 下面给出了应用程序的代码 Main.java @SpringBootApplication public class Main { public static void main(String[] args) { SpringApplication.run(Main.clas
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
我的跑步者
@Component
@ComponentScan(basePackages ="com.springboot.helloworld")
public class MyRunner implements CommandLineRunner {
@Autowired
private GreeterService greeterService;
@Override
public void run(String... args) throws Exception {
System.out.println(greeterService.greetGuest("John Doe"));
}
}
GreeterService和GreeterServiceImpl位于com.springboot.helloworld.Greeter中
@Service
public class GreeterServiceImpl implements GreeterService {
@Autowired
Sample service;
@Override
public String greetGuest(String greeter) {
if (greeter == null) {
return "Looged in as Guest";
}
return "Hello " + greeter + " How are you?";
}
}
Sample和SampleImpl位于不同的包com.springboot.helloworld.Sampler中
收到的错误是
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'greeterServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.springboot.helloworld.Sampler.Sample com.springboot.helloworld.Greeter.GreeterServiceImpl.service; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.springboot.helloworld.Sampler.Sample] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at com.springboot.helloworld.Main.main(Main.java:10)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.springboot.helloworld.Sampler.Sample com.springboot.helloworld.Greeter.GreeterServiceImpl.service; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.springboot.helloworld.Sampler.Sample] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 15 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.springboot.helloworld.Sampler.Sample] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
... 17 common frames omitted
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'greeterServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.springboot.helloworld.Sampler.Sample com.springboot.helloworld.Greeter.GreeterServiceImpl.service; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.springboot.helloworld.Sampler.Sample] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at com.springboot.helloworld.Main.main(Main.java:10)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.springboot.helloworld.Sampler.Sample com.springboot.helloworld.Greeter.GreeterServiceImpl.service; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.springboot.helloworld.Sampler.Sample] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 15 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.springboot.helloworld.Sampler.Sample] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
... 17 more
您的应用程序显然需要一个命令行参数,而您没有提供任何参数 应用程序显然需要一个命令行参数,而您没有提供任何参数 您必须使用
@组件注释,如下所示:
@ComponentScan({"com.springboot.helloworld","service"})
您必须使用@组件注释,如下所示:
@ComponentScan({"com.springboot.helloworld","service"})
我在切换到spring 5后出现了这个问题。问题是在构建时运行注释处理器以生成一些帮助器类,其副作用是生成了一个文件spring.properties。这被部署到META-INF中。当spring 5扫描组件时,它会检查该文件是否存在(作为类加载器加载的资源),如果存在,则在查找组件时只考虑文件中列出的类。在我的例子中,生成文件的注释处理器没有查找组件,因此组件类不在文件中。修复方法是删除该文件。如果没有该文件,则组件扫描将按预期工作 切换到spring 5后,我遇到了这个问题。问题是在构建时运行注释处理器以生成一些帮助器类,其副作用是生成了一个文件spring.properties。这被部署到META-INF中。当spring 5扫描组件时,它会检查该文件是否存在(作为类加载器加载的资源),如果存在,则在查找组件时只考虑文件中列出的类。在我的例子中,生成文件的注释处理器没有查找组件,因此组件类不在文件中。修复方法是删除该文件。如果没有该文件,则组件扫描将按预期工作 您是否尝试过mvn spring boot:run
?是否尝试过向其传递参数?代码似乎需要在命令行上传入arg[0]。是否尝试了mvn spring boot:run
?是否尝试了传入arg?看起来代码需要一个arg[0],它需要在命令行中传入。您能再解释一下吗?您能再解释一下吗?