SpringBootApplication入口调用service类方法

要在 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);
    }
}

Service类

@Service
public class SeaRouteService {

    ...

    public Feature getRoute(double oLon, double oLat, double dLon, double dLat) {
        return getRoute(oLon, oLat, dLon, dLat);
    }
}
posted @ 2024-07-25 16:41  槑孒  阅读(145)  评论(0)    收藏  举报