@SpringBootApplication和@ComponentScan无法一起工作(bean配置)

Bow*_*ick 5 java spring-boot

我有一个多模块项目,但我的配置有问题.我在包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);
    }
Run Code Online (Sandbox Code Playgroud)

在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));
    }
Run Code Online (Sandbox Code Playgroud)

和:

@Component
@Mapper
public interface MapInputModelMapper {

    MapInputModel mapToInputModel(final MapEntity n);

    MapEntity mapToEntiy(final MapInputModel n);

}
Run Code Online (Sandbox Code Playgroud)

存储库位于包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.
Run Code Online (Sandbox Code Playgroud)

当我删除@EnableAutoConfiguration和@ComponentScan注释时,它可以正常工作.应用程序启动时没有bean错误.

在那种情况下,我的restcontroller不再工作:

{
    "timestamp": "2018-07-18T20:48:39.414+0000",
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/maps"
}
Run Code Online (Sandbox Code Playgroud)

当我删除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!";
    }
}
Run Code Online (Sandbox Code Playgroud)

我尝试了几种不同的组合,我有一个不同的项目和示例,其中注释一起使用,它的工作正常.有人可以解释为什么注释在一起使用时会产生bean错误?为什么控制器在仅使用@SpringBootApplication时会出错?

在我的Pom.XML中,引导模块依赖于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;
}
Run Code Online (Sandbox Code Playgroud)

项目结构:

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
Run Code Online (Sandbox Code Playgroud)

Mat*_*ttR 1

您的 @AllArgsConstructor 上缺少 @Autowired 注释,请尝试:

@AllArgsConstructor(onConstructor = @__(@Autowired))
Run Code Online (Sandbox Code Playgroud)