9 Ocak 2023 Pazartesi

SpringContext ServiceLocatorFactoryBean Sınıfı

Giriş
Şu satırı dahil ederiz
import  org.springframework.beans.factory.config.ServiceLocatorFactoryBean;
Örnek
Elimizde bir arayüz ve bunun iki tane gerçekleştirimi olsun
public interface TourService {
  public String place(TourAdvicer request);
}

@Service("Singapore")
public class Singapore implements TourService {
  @Override
  public String place(TourAdvicer request) {
    ...
  }
}

@Service("Maldives")
public class Maldives implements TourService {
  @Override
  public String place(TourAdvicer request) {
    ...
  }
}
Bean ismine göre bulmak için bir registry yaratırız. Şöyle yaparız
public interface TourRegistry {
  public TourService getServiceBean(String serviceName);
}

//Now we need to register this registry class in our service locator 
// to get the desired bean config on run time
@Configuration
public class ServiceLocatorConfig {

  @Bean
  public FactoryBean<?> getBean() {
    ServiceLocatorFactoryBean bean = new ServiceLocatorFactoryBean();
    bean.setServiceLocatorInterface(TourRegistry.class);
    return bean;
  }
}
Şöyle yaparız
@Autowired
TourRegistry registry;

public String paymentAmount(@RequestBody String placeOfVisit) {
  return registry.getServiceBean(placeOfVisit).place(request);
}




Hiç yorum yok:

Yorum Gönder