34 lines
883 B
Java
34 lines
883 B
Java
package eu.eudat.commons.validation;
|
|
|
|
import org.springframework.context.ApplicationContext;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
@Service
|
|
public class ValidatorFactory {
|
|
|
|
private final ApplicationContext applicationContext;
|
|
|
|
public ValidatorFactory(ApplicationContext applicationContext) {
|
|
this.applicationContext = applicationContext;
|
|
}
|
|
|
|
public <T extends Validator> T validator(Class<T> type){
|
|
|
|
T item = this.applicationContext.getBean(type);
|
|
|
|
if (item == null || !(item instanceof Validator)) throw new RuntimeException("unrecognized validator " + type.getSimpleName());
|
|
|
|
return item;
|
|
}
|
|
|
|
public Validator validator(String validtor){
|
|
|
|
Validator item = this.applicationContext.getBean(validtor, Validator.class);
|
|
|
|
if (item == null || !(item instanceof Validator)) throw new RuntimeException("unrecognized validator " + validtor);
|
|
|
|
return item;
|
|
}
|
|
|
|
}
|