0

I want call get bus/bus station information open api from area code in user's address.

Area is city, city provide different bus information api each other.

TrafficController.java

@Controller
public class TrafficController {

    ....
    @Autowired
    public UserService userService;

    public TrafficService trafficService;
    ....

    @RequestMapping(value="/api/traffic/stations")
    public List<....> getNearStations(HttpServerletRequest req) {
        String userId = SessionAttrs.getUserId(req.getSession());
        User user = userSerivce.getUser(userId);

        trafficService = (TrafficService) Class.forName("com.myservice.service.impl.TrafficService_"+user.house.apt.address.area_code + "_Impl").newInstence();
        return trafficService.getStations(user.house.apt.latitude, user.house.apt.longitude, 300, 2);

    }
}

TrafficService_11_Impl.java

@Service
public Class TrafficService_11_Impl implemnet TrafficService {
    @Autowired
    TrafficRepository trafficRepository;

    @Override
    public List<Station> getStations(double lat, double lng, int ratius, int retryCount) {

        final String cacheKey = "getStations " + lat + " " + lng + " " + radius;
        TrafficeCache cache = trafficRepository.fineOne(chcheKey); // run time NullPointerException, trafficRepository is null

        if ( null == cache) {
            ...
            get area code 11 api call logic
            ...
        }

        ...
        return ...;
    }
}

TrafficRepository.java

public interface TrafficRepository extends JpaRepository<TrafficCache, String> {
}

This code occured runtime NullPointerException at

TrafficService_11_Impl.java at

TrafficeCache cache = trafficRepository.fineOne(chcheKey);

run time NullPointerException, trafficRepository is null

Otherly in TrafficController.java

@Autowired
public TrafficService trafficService

and drop code

trafficService = (TrafficService) Class.forName("com.myservice.service.impl.TrafficService_"+user.house.apt.address.area_code + "_Impl").newInstence();

was correct result

How can injection service class depending on user's area code in String boot?

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
  • Indented the code, added better formatting. Question is still poorly asked – dingo_d Nov 20 '15 at 07:19
  • Are you planning to create a separate implementation of your `TrafficService` for *each* possible area code? Regardless of what you mean by "area code" and how many there could be, that's really not how you should do it. Create a single implementation of `TrafficService` that encapsulates differences between area codes instead. – kryger Nov 20 '15 at 22:00
  • Possible duplicate of [Why is my Spring @Autowired field null?](http://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null) – chrylis -cautiouslyoptimistic- Nov 23 '15 at 04:12

1 Answers1

0

TrafficController.java

@Controller
public class TrafficController {

    ....
    @Autowired
    private ApplicationContext context;

    @Autowired
    public UserService userService;

    public TrafficService trafficService;
    ....

    @RequestMapping(value="/api/traffic/stations")
    public List<....> getNearStations(HttpServerletRequest req) {
        String userId = SessionAttrs.getUserId(req.getSession());
        User user = userSerivce.getUser(userId);

        trafficService = (TrafficService) context.getBean(Class.forName("com.myservice.service.impl.TrafficService_"+user.house.apt.address.area_code + "_Impl"));
        return trafficService.getStations(user.house.apt.latitude, user.house.apt.longitude, 300, 2);

    }
}

It's correct result