Spring @ComponentScan(“com.junk”) allows autowiring to work?

假如想象 提交于 2020-01-07 00:52:51

问题


I have a simple Spring Boot Java application and it is using @Autowired.

Unless I add in the @ComponentScan annotation I get :org.springframework.beans.factory.NoSuchBeanDefinitionException failures at run time.

Weirdly any @ComponentScan fixes the wiring, even if it's referring to a non-existent package.

Shouldn't @ComponentScan required valid package as an argument ?

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
// @ComponentScan // This one causes auto-wired problems. See below.
@ComponentScan("com.junk.foo") // This one works?

public class DemoApplication {

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

EDIT in response to questions in comments ...

By autowire problems I mean with @ComponentScan or without that annotation I get ...

   Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.foo.Foo] 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:1373)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
        ... 25 more

whereas if I use @ComponentScan("com.junk.foo") I get no auto-wire problems.

The error is :

 :: Spring Boot ::        (v1.3.2.RELEASE)


// SNIP FOR BREVITY

Exception in thread "main" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:62)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:54)
    ... 1 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'barService': Injection of autowired dependencies failed; nested exception is  

// SNIP FOR BREVITY

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.foo.Foo] 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:1373)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
    ... 25 more

My project is like this ...

├── pom.xml
└── src
    └── main
        ├── java
        │   └── com
        │       ├── bar
        │       │   └── BarService.java
        │       ├── example
        │       │   └── demo
        │       │       └── DemoApplication.java
        │       └── foo
        │           └── Foo.java
        └── resources

回答1:


With the errors you posted i can see you do have Autowire problem in both the case.

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.foo.Foo] 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)} is there in both case.

Now to answer you question:

@ComponentScan takes the package of the annotated class as default package to scan, which in this e.g. is com.example.demo. So spring is only scanning com.example.demo and would scan any sub-package i.e. com.example.demo.blah

If you want an easy solution where you don't want to get into pain of managing ComponentScan by yourself consider structuring your code as suggested in spring docs.

If you want to continue with current structure try this on main method of DemoApplication.java:

@ComponentScan({"com.bar", "com.example.demo", "com.foo"})

Likewise, in future if you have another package say com.one.more.pack then you would have to add that in ComponentScan like this @ComponentScan({"com.bar", "com.example.demo", "com.foo", "com.one.more.pack"})



来源:https://stackoverflow.com/questions/35751273/spring-componentscancom-junk-allows-autowiring-to-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!