Using static-method makes code more clean. So I wanna use static-method even used with @Service, @Repository class in it. You can more easily understand by code. Very short one and It works!!
But I want to know it is okay to use in any situation. I didn't see like that code before, so I am afraid it is the effective code to use. If you have any idea about that, could you advise me, please?
@Repository
public class TruckRepository {
public Integer selectWheelCount() {
//which is searching truck database to get some data about trucks.
//Such as how many wheels does the truck have, something like that.
}
}
@Component
public class CarFactory {
private static TruckRepository truckRepository;
//@Autowired << can be omitted after spring 4.3 as I know
NewsSourceFactory(TruckRepository truckRepository) {
this.truckRepository = truckRepository;
}
public static Integer getWheelCount(String carType) {
swtich(carType) {
case TRUCK:
return truckRepository.selectWheelCount();
}
}
}
@Component
public class SomeCode {
public void something() {
Integer count = CarFactory.getWheelCount("TRUCK");
}
}
Add Comments
I very empressive the code of "Duration.class", "Stream.class" in java. They are also using static-method, Of course they have no dynamic injection in there. Just in case of thinking about the concise of code or clearness, isn't it the merit of static-method, dont you think? is it really harmless?