Spring @Import注释无法按预期工作

时间:2019-10-24 16:26:43

标签: spring spring-boot spring-data-jpa

我正在使用Spring Boot 2.0.5.RELEASE,Java 8开发一个多模块项目。它由模型,DAO和各种微服务组成。 DAO bean的使用由@Import批注提供,如下所示:

微服务:

@SpringBootApplication
@EnableBatchProcessing
@EnableDao
public class Application {

    public static ConfigurableApplicationContext CONTEXT;

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

DAO:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(DaoConfiguration.class)
public @interface EnableDao {
/* empty */
}
@Configuration
@EntityScan("blah.blah.blah.dao")
@EnableJpaRepositories("blah.blah.blah.dao.repo")
public class DaoConfiguration {
/* Some tricks with EntityManager also */
}
@Repository
public interface SomethingRepo extends JpaRepository<Something, Long> {
/* + some default methods */
}

但是我已经运行了代码:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'blah.blah.blah.dao.repo.SomethingRepo' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

为什么我的代码无法创建bean?可能是什么原因?

1 个答案:

答案 0 :(得分:0)

仍然不知道为什么会有这种行为,但是我发现解决方案添加了注释 @EnableJpaRepositories(basePackageClasses = {SomethingRepo.class})Application.class