Spring @ComponentScan(“com.junk”)允许自动装配工作吗?

时间:2016-03-02 15:19:55

标签: spring spring-mvc

我有一个简单的Spring Boot Java应用程序,它正在使用@Autowired。

除非我添加@ComponentScan注释,否则我会在运行时遇到org.springframework.beans.factory.NoSuchBeanDefinitionException个失败。

奇怪的是,任何 @ComponentScan修复了布线,即使它指的是不存在的包。

@ComponentScan不应该将有效包作为参数吗?

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);
    }
}

编辑回复评论中的问题......

通过autowire问题我的意思是@ComponentScan或没有那个注释我得到......

   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

如果我使用@ComponentScan("com.junk.foo"),我就不会遇到自动线问题。

错误是:

 :: 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

我的项目是这样的......

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

1 个答案:

答案 0 :(得分:0)

根据您发布的错误,我可以看到您在这两种情况下都遇到了Autowire问题。

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)} 两种情况都有。

现在回答你的问题:

@ComponentScan将带注释的类的包作为要扫描的默认包,在此处例如是com.example.demo。因此,spring仅扫描com.example.demo并扫描任何子包,即com.example.demo.blah

如果您想要一个简单的解决方案,您不希望自己管理ComponentScan,请考虑按spring docs中的建议构建代码。

如果您想继续使用当前结构,请尝试使用DemoApplication.java的主要方法:

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

同样,将来如果你有另一个包说com.one.more.pack那么你必须在ComponentScan中添加这样的包 @ComponentScan({"com.bar", "com.example.demo", "com.foo", "com.one.more.pack"})