【发布时间】:2018-12-26 21:30:50
【问题描述】:
我有一个多模块项目,但我的配置有问题。 我在包 nl.example.hots.boot 中有一个我的主要方法
@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan(basePackages = {"nl.*"})
@EntityScan("nl.*")
public class HotsApplication {
public static void main(String[] args) {
SpringApplication.run(HotsApplication.class, args);
}
在 nl.example.hots.core.* 包中我有这个类:
@Service
@AllArgsConstructor
@Transactional(propagation = Propagation.REQUIRED)
public class MapImportService {
private MapInputModelMapper mapInputModelMapper;
private MapEntityRepository mapEntityRepository;
public void add(final MapInputModel mapInputModel) {
System.out.println(mapInputModel.getName());
mapEntityRepository.save(mapInputModelMapper.mapToEntiy(mapInputModel));
}
和:
@Component
@Mapper
public interface MapInputModelMapper {
MapInputModel mapToInputModel(final MapEntity n);
MapEntity mapToEntiy(final MapInputModel n);
}
存储库位于 nl.example.hots.persistence.* 包中
运行应用程序时出现以下错误:
Description:
Parameter 0 of constructor in nl.example.hots.core.dataimport.MapImportService.MapImportService required a bean of type 'nl.timonschultz.hots.core.map.mapper.MapInputModelMapper' that could not be found.
Action:
Consider defining a bean of type 'nl.example.hots.core.map.mapper.MapInputModelMapper' in your configuration.
当我删除 @EnableAutoConfiguration 和 @ComponentScan 注释时,它可以工作。应用程序启动时没有 bean 错误。
在这种情况下,但是我的 restcontroller 不再工作了:
{
"timestamp": "2018-07-18T20:48:39.414+0000",
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/maps"
}
当我删除 MapImportService 类(并且没有弹出 bean 错误)时,它在同一个 url 上工作。
package nl.example.hots.api.data_import;
@RestController
@AllArgsConstructor
public class ImportController {
private static final String URL = // a url
private Reader reader;
@RequestMapping("/maps")
public String abilityStreamImport() {
reader.readStream(URL); // calls a class in nl.example.hots.core.*
return "Greetings from APP!";
}
}
我尝试了几种不同的组合,并且我有一个不同的项目作为示例,其中注释一起使用并且可以正常工作。有人可以解释为什么注释一起使用时会产生 bean 错误吗?以及为什么控制器只使用@SpringBootApplication 时会报错?
在我的 Pom.XML 中,引导模块依赖于 API 层,该 API 层依赖于核心层,而核心层又依赖于持久层。 我在项目中使用了 mapstruct 和 Lombok。
--- 编辑:存储库---
@Entity(name = "MAPS")
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Getter
public class MapEntity extends HasId<Long> {
private String name;
@ElementCollection
private List<String> translations;
}
@Repository
public interface MapEntityRepository extends JpaRepository<MapEntity, Long> {
}
@MappedSuperclass
public abstract class HasId<T> {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Setter
@Getter
private T id;
}
项目结构:
hots-application;
- hots-api
- pom.xml
- nl.example.hots.api.dataimport.ImportController;
- hots-boot
- pom.xml
- nl.example.hots.boot.HotsApplication;
- hots-core
- pom.xml
- nl.example.hots.core.dataimport.mapImportService.MapImportService;
- nl.example.hots.core.map.mapper.MapInputModelMapper
- hots-persistence
- pom.xml
- nl.example.hots.persistence.common.HasId;
- nl.example.hots.persistence.map.MapEntity;
- nl.example.hots.persistence.map.MapEntityRepository;
pom.xml
【问题讨论】:
-
不确定是否可以在
basePackages中使用“*”。据我记得,您需要提供多个基本包,例如basePackages={"a.b.c", "d.e.f"}。 -
或者在这种情况下,只需使用
"nl"。 -
试过了:@ComponentScan(basePackages = {"nl.example.hots.api", "nl.example.hots.core", "nl.example.hots.boot", "nl .example.hots.persistence"}) 以及“nl”但结果相同。 @ComponentScan(basePackages = {"nl.*"}) 在我使用的另一个示例项目中工作正常。
-
仅使用 @SpringBootApplication 时会出现错误,因为默认情况下它会扫描带注释的类的包(在您的情况下为 HotsApplication)。我通常将我的主类放在包层次结构的根部,否则你需要指定 ComponentScan
-
将您的
HotsApplication移动到nl.example.hots并仅保留@SpringBootApplication注释。这样扫描时会自动覆盖所有子包。 Spring Boot 将扫描@SpringBootApplication注释类所在的包和子包。您在boot子包中,因此它不会扫描任何内容,这意味着您基本上必须手动配置所有内容,而不是能够使用Spring Boot 自动配置特性。
标签: java spring-boot