Service Locator for Vaadin
public class ServiceLocator {
static ApplicationContext ctx;
public staticT getService(Class t){
return ctx.getBean(t);
}
public Object getService(String beanName){
return ctx.getBean(beanName);
}
public static void main(String[] args) {
String name = getService(String.class);
}
public static void setCtx(ApplicationContext ctx) {
ServiceLocator.ctx = ctx;
}
}
If you like to hide the Application context setter, you can use reflection to set Application context.
public void setServiceLocator(ApplicationContext ctx) throws NoSuchFieldException, IllegalAccessException {
Field modifiersField = ServiceLocator.class.getDeclaredField("ctx");
modifiersField.setAccessible(true);
modifiersField.set(null, ctx);
}

