Spring 服务自动装配产生 NPE

问题描述 投票:0回答:0

主课:

@SpringBootApplication
public class MicroserviceClientApplication implements ApplicationRunner {
    private static final Logger LOGGER = LoggerFactory.getLogger(MicroserviceClientApplication.class);

    private List<Client> clients = new ArrayList<Client>();

    @Autowired
    private Environment environment;
    @Autowired
    ApplicationContext applicationContext;

    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder()
                .web(WebApplicationType.NONE)
                .sources(MicroserviceClientApplication.class)
                .headless(true)
                .build()
                .run(args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        populateClients();
        LOGGER.info("Starting clients...");
        for (Client client : clients) {
            client.start();
        }
        runTest();
    }
}

服务等级:

package au.com.consunet.microserviceclient.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class WorldEnvironmentService {
    private static final Logger LOGGER = LoggerFactory.getLogger(WorldEnvironmentService.class);
    private static final double northWestCornerX = 138.4730987548828;
    private static final double northWestCornerY = -34.72359848022461;
    private static final double southEastCornerX = 138.70030212402344;
    private static final double southEastCornerY = -35.04520034790039;
    private static final int rowCount = 3574;
    private static final int colCount = 2071;
    private static final double colWidth = 1.0970708311956784E-4;
    private static final double rowHeight = 8.998373466026336E-5;

    public int normaliseLatitude(double latitudeDeg) {
        double diff = northWestCornerY - latitudeDeg;
        int diffCell = (int) (diff / rowHeight);
        return Math.min(Math.max(diffCell, 0), rowCount - 1);
    }

    public int normaliseLongitude(double longitudeDeg) {
        double diff = longitudeDeg - northWestCornerX;
        int diffCell = (int) (diff / colWidth);
        return Math.min(Math.max(diffCell, 0), colCount - 1);
    }
}

我尝试在其中使用服务的类(调用 normaliseLat/Long() 会导致 NPE

package au.com.consunet.microserviceclient.client;

import au.com.consunet.microserviceclient.client.request.HeightRequest;
import au.com.consunet.microserviceclient.model.Messages;
import au.com.consunet.microserviceclient.service.WorldEnvironmentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Response;

import java.util.HashMap;
import java.util.Map;

@Component
public class BoundingBoxRedisHashClient extends RedisHashClient {
    private static final Logger LOGGER = LoggerFactory.getLogger(BoundingBoxRedisHashClient.class);

    @Autowired
    private WorldEnvironmentService worldEnvironmentService;

    private Map<String, Response> getHeightsInBoundingBox(Messages.Location location) {
        double startLat = worldEnvironmentService.normaliseLatitude(Double.parseDouble(location.getLatitude()));
        double startLong = worldEnvironmentService.normaliseLongitude(Double.parseDouble(location.getLongitude()));
        double endLat = worldEnvironmentService.normaliseLatitude(Double.parseDouble(location.getEndLatitude()));
        double endLong = worldEnvironmentService.normaliseLongitude(Double.parseDouble(location.getEndLongitude()));

        Pipeline pl = jedis.pipelined();
        Map<String, Response> responses = new HashMap<>();
        ...
        pl.sync();
        return responses;
    }
    @Override
    protected void requestHeights() {
        LOGGER.info("Sending Redis Requests for RedisHash bounding box");
        for(int i=0;i<5;i++){
            getHeightsInBoundingBox(HeightRequest.getRandomLocation());
        }
    }

    @Override
    public void runTest() {
        requestHeights();
    }
}

我运行了调试器,在调用 normalise() 函数的断点处 worldEnvironmentService 绝对为空。我不确定为什么会这样?是用@Service注解的,是组件类中的@Autowired试图使用它?

java spring javabeans
© www.soinside.com 2019 - 2024. All rights reserved.