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?