javax.validation.Validation.byDefaultProvider()方法的使用及代码示例

x33g5p2x  于2022-01-31 转载在 其他  
字(9.6k)|赞(0)|评价(0)|浏览(339)

本文整理了Java中javax.validation.Validation.byDefaultProvider()方法的一些代码示例,展示了Validation.byDefaultProvider()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Validation.byDefaultProvider()方法的具体详情如下:
包路径:javax.validation.Validation
类名称:Validation
方法名:byDefaultProvider

Validation.byDefaultProvider介绍

[英]Builds a Configuration. The provider list is resolved using the strategy provided to the bootstrap state.

Configuration<?> configuration = Validation 
.byDefaultProvider() 
.providerResolver( new MyResolverStrategy() ) 
.configure(); 
ValidatorFactory factory = configuration.buildValidatorFactory();

The provider can be specified in the XML configuration. If the XML configuration does not exist or if no provider is specified, the first available provider will be returned.
[中]构建一个配置。使用提供给引导状态的策略解析提供程序列表

Configuration<?> configuration = Validation 
.byDefaultProvider() 
.providerResolver( new MyResolverStrategy() ) 
.configure(); 
ValidatorFactory factory = configuration.buildValidatorFactory();

可以在XML配置中指定提供程序。如果XML配置不存在或未指定提供程序,将返回第一个可用的提供程序。

代码示例

代码示例来源:origin: javax.validation/validation-api

/**
 * Builds and returns a {@link ValidatorFactory} instance based on the
 * default Bean Validation provider and following the XML configuration.
 * <p>
 * The provider list is resolved using the default validation provider resolver
 * logic.
 * <p>
 * The code is semantically equivalent to
 * {@code Validation.byDefaultProvider().configure().buildValidatorFactory()}.
 *
 * @return {@code ValidatorFactory} instance
 *
 * @throws NoProviderFoundException if no Bean Validation provider was found
 * @throws ValidationException if a Bean Validation provider was found but the
 * {@code ValidatorFactory} cannot be built
 */
public static ValidatorFactory buildDefaultValidatorFactory() {
  return byDefaultProvider().configure().buildValidatorFactory();
}

代码示例来源:origin: jersey/jersey

@Override
  public Configuration get() {
    try {
      if (!inOsgi) {
        return Validation.byDefaultProvider().configure();
      } else {
        return Validation
            .byDefaultProvider()
            .providerResolver(new ValidationProviderResolver() {
              @Override
              public List<ValidationProvider<?>> getValidationProviders() {
                final List<ValidationProvider<?>> validationProviders = new ArrayList<>();
                for (final ValidationProvider validationProvider : ServiceFinder
                    .find(ValidationProvider.class)) {
                  validationProviders.add(validationProvider);
                }
                return validationProviders;
              }
            })
            .configure();
      }
    } catch (final ValidationException e) {
      // log and re-trow
      LOGGER.log(Level.FINE, LocalizationMessages.VALIDATION_EXCEPTION_PROVIDER(), e);
      throw e;
    }
  }
}

代码示例来源:origin: org.springframework.boot/spring-boot

@Override
public MessageInterpolator getObject() throws BeansException {
  try {
    return Validation.byDefaultProvider().configure()
        .getDefaultMessageInterpolator();
  }
  catch (ValidationException ex) {
    MessageInterpolator fallback = getFallback();
    if (fallback != null) {
      return fallback;
    }
    throw ex;
  }
}

代码示例来源:origin: spring-projects/spring-framework

GenericBootstrap bootstrap = Validation.byDefaultProvider();
if (this.validationProviderResolver != null) {
  bootstrap = bootstrap.providerResolver(this.validationProviderResolver);

代码示例来源:origin: org.springframework/spring-context

GenericBootstrap bootstrap = Validation.byDefaultProvider();
if (this.validationProviderResolver != null) {
  bootstrap = bootstrap.providerResolver(this.validationProviderResolver);

代码示例来源:origin: wildfly/wildfly

private ValidatorFactory initFactory() {
  final ClassLoader oldTCCL = WildFlySecurityManager.getCurrentContextClassLoaderPrivileged();
  try {
    WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(classLoader);
    return Validation.byDefaultProvider().providerResolver(new WildFlyProviderResolver()).configure()
        .buildValidatorFactory();
  } finally {
    WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(oldTCCL);
  }
}

代码示例来源:origin: wildfly/wildfly

private ValidatorFactory initFactory() {
  final ClassLoader oldTCCL = WildFlySecurityManager.getCurrentContextClassLoaderPrivileged();
  try {
    WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(classLoader);
    if (configuration == null) {
      return Validation.byDefaultProvider().providerResolver(new WildFlyProviderResolver()).configure()
          .buildValidatorFactory();
    } else {
      return configuration.buildValidatorFactory();
    }
  } finally {
    WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(oldTCCL);
  }
}

代码示例来源:origin: hibernate/hibernate-validator

public StatisticalValidationState() {
  ValidatorFactory factory = null;
  final Configuration<?> configuration = Validation.byDefaultProvider().configure();
  try ( InputStream mappingStream = StatisticalValidation.class.getResourceAsStream( "mapping.xml" ) ) {
    configuration.addMapping( mappingStream );
    factory = configuration.buildValidatorFactory();
    assertThat( factory ).isNotNull();
  }
  catch (IOException e) {
    throw new IllegalStateException( "Mappings cannot be read. Validation factory cannot be configured correctly.", e );
  }
  validator = factory.getValidator();
  entitiesUnderTest = IntStream.rangeClosed( 0, NUMBER_OF_TEST_ENTITIES )
      .mapToObj( index -> new TestEntity( index % 10 ) )
      .collect( Collectors.toList() );
}

代码示例来源:origin: resteasy/Resteasy

BootstrapConfiguration getConfig()
{
 BootstrapConfiguration tmpConfig = bootstrapConfiguration;
 if (tmpConfig == null)
 {
   synchronized (RD_LOCK)
   {
    tmpConfig = bootstrapConfiguration;
    if (tmpConfig == null)
    {
      config = Validation.byDefaultProvider().configure();
      bootstrapConfiguration = tmpConfig = config.getBootstrapConfiguration();
    }
   }
 }
 return tmpConfig;
}

代码示例来源:origin: com.atlassian.plugins.rest/atlassian-rest-common

public ValidationInterceptor(MessageInterpolator messageInterpolator) {
  // Yes, this cast is unnecessary in Java 6, but seems to be required in Java 5
  this.factory = ((Configuration) Validation.byDefaultProvider().configure().messageInterpolator(messageInterpolator)).buildValidatorFactory();
}

代码示例来源:origin: hibernate/hibernate-validator

private Configuration<?> getConfiguration() {
  return validationProviderHelper.isDefaultProvider() ?
      Validation.byDefaultProvider().configure() :
      Validation.byProvider( org.hibernate.validator.HibernateValidator.class ).configure();
}

代码示例来源:origin: apache/servicecomb-java-chassis

@Override
public <T> void beforeMethodInvoke(SwaggerInvocation invocation, SwaggerProducerOperation producerOperation,
  Object[] args)
  throws ConstraintViolationException {
 if (null == executableValidator) {
  ValidatorFactory factory =
    Validation.byDefaultProvider()
      .configure()
      .parameterNameProvider(new DefaultParameterNameProvider())
      .buildValidatorFactory();
  executableValidator = factory.getValidator().forExecutables();
 }
 Set<ConstraintViolation<Object>> violations =
   executableValidator.validateParameters(producerOperation.getProducerInstance(),
     producerOperation.getProducerMethod(),
     args,
     Default.class);
 if (violations.size() > 0) {
  LOGGER.warn("Parameter validation failed : " + violations.toString());
  throw new ConstraintViolationException(violations);
 }
}

代码示例来源:origin: hibernate/hibernate-validator

public ValidationExtension() {
  Configuration<?> config = Validation.byDefaultProvider().configure();
  // we use the default ParameterNameProvider here as we cannot use the injected one
  // as it hasn't been turned into a managed bean yet and might not be able to
  // return a parameter name. At this stage, we don't care about the parameter names.
  config.parameterNameProvider( config.getDefaultParameterNameProvider() );
  BootstrapConfiguration bootstrap = config.getBootstrapConfiguration();
  globalExecutableTypes = bootstrap.getDefaultValidatedExecutableTypes();
  isExecutableValidationEnabled = bootstrap.isExecutableValidationEnabled();
  validatorFactory = config.buildValidatorFactory();
  validator = validatorFactory.getValidator();
  getterPropertySelectionStrategyHelper = GetterPropertySelectionStrategyHelper.forValidationFactory( validatorFactory );
  executableHelper = new ExecutableHelper( new TypeResolutionHelper() );
}

代码示例来源:origin: caelum/vraptor

@PostConstruct
public void buildFactory() {
instance = Validation.byDefaultProvider().configure()
  .constraintValidatorFactory(constraintValidatorFactory).buildValidatorFactory();
logger.debug("Initializing Bean Validation (1.1 supported)");
}

代码示例来源:origin: caelum/vraptor

@PostConstruct
public void buildFactory() {
  factory = Validation.byDefaultProvider()
    .configure()
    .messageInterpolator(interpolator)
    .buildValidatorFactory();
logger.debug("Initializing Bean Validation (1.0 supported)");
}

代码示例来源:origin: apache/cxf

public BeanValidationProvider(ValidationConfiguration cfg) {
  try {
    Configuration<?> factoryCfg = Validation.byDefaultProvider().configure();
    initFactoryConfig(factoryCfg, cfg);
    factory = factoryCfg.buildValidatorFactory();
  } catch (final ValidationException ex) {
    LOG.severe("Bean Validation provider can not be found, no validation will be performed");
    throw ex;
  }
}

代码示例来源:origin: org.apache.cxf/cxf-core

public BeanValidationProvider(ValidationConfiguration cfg) {
  try {
    Configuration<?> factoryCfg = Validation.byDefaultProvider().configure();
    initFactoryConfig(factoryCfg, cfg);
    factory = factoryCfg.buildValidatorFactory();
  } catch (final ValidationException ex) {
    LOG.severe("Bean Validation provider can not be found, no validation will be performed");
    throw ex;
  }
}

代码示例来源:origin: torakiki/sejda

private DefaultValidationContext() {
  Configuration<?> validationConfig = Validation.byDefaultProvider().configure();
  if (new DefaultSejdaContext().isIgnoreXmlConfiguration()) {
    validationConfig.ignoreXmlConfiguration();
  }
  ValidatorFactory factory = validationConfig.buildValidatorFactory();
  validator = factory.getValidator();
}

代码示例来源:origin: actframework/actframework

private void init(App app) {
  config = Validation.byDefaultProvider().configure();
  config.messageInterpolator(new ActValidationMessageInterpolator(config.getDefaultMessageInterpolator(), app.config()));
  ensureFactoryValidator();
}

代码示例来源:origin: org.seedstack.seed/seed-core

private GlobalValidatorFactory() {
  validatorFactory = Validation.byDefaultProvider()
      .configure()
      .parameterNameProvider(new ReflectionParameterNameProvider())
      .messageInterpolator(new SeedMessageInterpolator())
      .buildValidatorFactory();
}

相关文章