Java Spring:似乎无法使用@ SpringBootApplication,@ ComponentScan找到本地控制器?

时间:2019-11-01 20:35:38

标签: java spring spring-boot

我将项目分为不同的存储库,现在我的所有端点都为404(显然,它们整体运行良好)。我将核心代码移至另一个存储库,并通过Gradle作为实现将其包括在内。我将其视为外部依赖项,并且没有得到未解析的符号或任何东西,因此在依赖项方面似乎很好。

当前,应用程序启动似乎不错,但是当我尝试命中端点时,它们全都是404:

{
    "timestamp": "2019-11-01T19:22:52.697+0000",
    "path": "/v1/location/ipv4/1.0.1.0",
    "status": 404,
    "error": "Not Found",
    "message": null,
    "requestId": "db040351"
}

控制器:

package com.projectname.location.api.controller;

import ...

@RequestMapping("/v1/location")
@RestController
public class LocationController {

    @Autowired
    private LocationService locationService;

    @GetMapping("/ipv4/{ip}")
    public Mono<Location> getLocationByIpv4(@PathVariable String ip) {
        return locationService.getLocationByIpv4(ip);
    }
}

应用程序:

package com.projectname.location;

import ...

@SpringBootApplication
@ComponentScan({"projectname.core.location"})
public class Application {

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

如果没有用于外部库的@ComponentScan,则应用程序将无法启动(unsatisfied dependency)。

似乎控制器未包含在类路径中(尽管根据我的研究,@SpringBootApplication应该这样做-@ComponentScan是否覆盖了它?),所以我尝试添加到组件中扫描:

@ComponentScan({"com.projectname.location.api", "projectname.core.location"})

                ^ trying to get local controller      ^ external core stuff

我尝试了各种路径,但是所有路径均导致unsatisfied dependency或bean错误。谁能帮我解决这个问题?

Stacktrace(在我尝试捕获本地控制器时发生):

  

org.springframework.beans.factory.UnsatisfiedDependencyException:   创建名称为“ locationController”的bean时出错:不满意   通过字段“ locationService”表示的依赖关系;嵌套异常   是org.springframework.beans.factory.UnsatisfiedDependencyException:   创建名称为“ locationServiceImpl”的bean时出错:不满意   通过字段“ locationRepository”表示的依赖关系;嵌套的   异常是org.springframework.beans.factory.BeanCreationException:   创建名称为“ locationRedisRepositoryImpl”的bean时出错:查找   方法解析失败;嵌套异常为   java.lang.IllegalStateException:无法自省类   [com.projectname.location.core.repositories.LocationRedisRepositoryImpl]   从ClassLoader   [jdk.internal.loader.ClassLoaders$AppClassLoader@4b85612c]

LocationRedisRepositoryImpl:

package com.projectname.location.core.repositories;

import ...

@Repository
public class LocationRedisRepositoryImpl implements LocationRepository {

    @Autowired
    private ReactiveRedisTemplate<String, String> redisTemplate;

    @Autowired
    private ReactiveRedisOperations<String, Location> reactiveRedisOperations;

    @Autowired
    private ReactiveRedisConnectionFactory factory;

    public Mono<Location> getLocationByIpv4(Ipv4Request ipv4Request) {
        return reactiveRedisOperations.opsForValue().get(ipv4Request.getIpAddress());
    }

    public Mono<Boolean> writeLocation(Location location) {
        return reactiveRedisOperations.opsForZSet().add("ip-loc", location, Long.parseLong(location.getMaxIpInt()));
    }

    public Mono<RepositoryStatus> getStatus() {
        return Mono.empty();
    }

    public Mono<Boolean> writeStatus() {
        return Mono.empty();
    }

    public Mono<String> flushAll() {
        return factory.getReactiveConnection().serverCommands().flushAll();
    }

}

LocationRepository:

package com.projectname.location.core.repositories;

import ...

@Repository
public interface LocationRepository {

    public Mono<Boolean> writeLocation(Location location);

    public Mono<Location> getLocationByIpv4(Ipv4Request ipv4Request);

    public Mono<RepositoryStatus> getStatus();

    public Mono<Boolean> writeStatus();

    public Mono<String> flushAll();
}

1 个答案:

答案 0 :(得分:1)

您可能对组件扫描软件包的配置错误。在使用@SpringBootApplication时,@ ComponentScan注释在您的情况下是多余的,因为SpringBootApplication注释会扫描放置有该注释的类所在的包及其所有子包。

此外,您在ComponentScan批注中有错误的程序包名称-您编写了@ComponentScan({“ projectname.core.location”}),但正确的程序包名称应为@ComponentScan({“ com.projectname.location”})。

因此,如果要使用ComponentScan批注,它应该看起来像这样(但是ComponentScan是多余的):

package com.projectname.location;

import ...

@SpringBootApplication
@ComponentScan({"com.projectname.location"})
public class Application {

}