我有一个由多个模块组成的maven项目。其中三个模块是
在启动时,我得到以下异常:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.foo.bar.data.OrderRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
我知道Spring Boot Applications默认情况下只查看他们自己和bean的子包,但在这种情况下,bean是在兄弟包中。我已经尝试在Application类中添加@ComponentScan({"org.foo.bar.data"})
,但无济于事。
使用@EnableJpaRepositories(basePackages = {"org.foo.bar.data"})
投掷
Caused by: java.lang.IllegalArgumentException: Not an managed type: class org.foo.bar.core.model.Order
我做错了什么?
目录树(已修剪)如下所示:
project
├── project-core
│ └── src
│ └── main
│ └── java
│ └── org
│ └── foo
│ └── bar
│ └── core
| └── model
| └── Order.java
├── project-data
│ └── src
│ └── main
│ └── java
│ └── org
│ └── foo
│ └── bar
│ └── data
| └── OrderRepository.java
│
└── project-spring
└── src
└── main
└── java
└── org
└── foo
└── bar
└── spring
└── Application.java
OrderRepository:
public interface OrderRepository extends JpaRepository<Order, Integer> {
List<Order> findByDeliveryOverview(DeliveryOverview deliveryOverview);
List<Order> findByDeliveryOverviewAndCustomer(DeliveryOverview deliveryOverview, Customer customer);
}
Application.java
@SpringBootApplication
@ComponentScan({"org.foo.bar.data"})
public class Application {
private static final Logger log = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
答案 0 :(得分:6)
主要问题是你的结构。
要扫描实体,您需要在应用程序类上使用@EntityScan
并启用所需的存储库@EnableJpaRepositories
。
@SpringBootApplication
@EntityScan({"org.foo.bar.core.model"})
@EnableJpaRepositories(basePackages = {"org.foo.bar.data"})
public class Application { ... }
但我建议将Application
课程移至org.foo.bar
,然后您只需要@SpringBootApplication
,因为它将从该目录开始扫描。
答案 1 :(得分:0)
使用OrderRepository
注释@Repository
,并在{"org.foo.bar.data"}
@ComponentScan