无法在jar中自动装配bean

时间:2015-09-16 05:40:35

标签: spring-boot

Spring引导无法在jar中自动装配bean,并在加载时导致错误。

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.xxx.xxx.core.orm.XXXSqlTemplate] 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)
    ... 18 more

我的Starter课程是

@SpringBootApplication
@EnableAutoConfiguration
@EnableWebMvc
@ComponentScan("com.sds")
@Configuration
public class Starter {

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

}

com.xxx.xxx.core.orm.XXXSqlTemplate类位于jar中,jar也包含在类路径中。

我该如何解决这个问题?我将tomcat更改为jetty作为嵌入式servlet容器。 但是没有解决方法。

1 个答案:

答案 0 :(得分:0)

如果SqlTemplate类不在com.sds的子包中,则必须将类的包(或其超级包)添加到ComponentScan注释中,如下所示:

@ComponentScan({"com.sds", "com.xxx"})