I want to autowired service in enum like following code.
public enum TopupChargeService {
TYPE1{
public Response fun() {
return new ServiceRepo().getService().func1();
}
},
TYPE2{
public Response fun() {
return new ServiceRepo().getService().func2();
}
};
public abstract Response fun();
@Component
public static class ServiceRepo {
private Service service;
public Service getService() {
return service ;
}
public ServiceRepo () {
service = new ServiceImpl();
}
}
}
and Service code is like following code.
@Service
public class ServiceImpl implements Service {
@Autowired
private RestTempalte restTemplate;
Response fun1(){
// use restTemplate in this fun
}
Response fun2(){
// use restTemplate in this fun
}
}
Also I defined restTemplate as bean in config class. But restTemplate is null.
My questions: 1- Is it correct way to call services? 2- Why restTemplate is null?