要在 public static void main(String[] args) 中调用 Service 的方法,需要在 Application 类中手动获取 Spring 容器,并从中获取 Service 的实例。

示例如下:
启动入口程序

@SpringBootApplication
public class RouteApplication {

    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(RouteApplication.class, args);
        SeaRouteService seaRouteService = context.getBean(SeaRouteService.class);
        Feature feature = seaRouteService.getRoute(113, 23, 10, 50);
        String json = GeometryUtil.GeometryToGeoJson(feature.getGeometry());
        System.out.println(json);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

Service类

@Service
public class SeaRouteService {

    ...

    public Feature getRoute(double oLon, double oLat, double dLon, double dLat) {
        return getRoute(oLon, oLat, dLon, dLat);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.