/** * Create a new {@link Bindable} of the specified type. * @param <T> the source type * @param type the type (must not be {@code null}) * @return a {@link Bindable} instance * @see #of(ResolvableType) */ public static <T> Bindable<T> of(Class<T> type) { Assert.notNull(type, "Type must not be null"); return of(ResolvableType.forClass(type)); }
/** * Create a new {@link Bindable} {@link Set} of the specified element type. * @param <E> the element type * @param elementType the set element type * @return a {@link Bindable} instance */ public static <E> Bindable<Set<E>> setOf(Class<E> elementType) { return of(ResolvableType.forClassWithGenerics(Set.class, elementType)); }
/** * Bind the specified target {@link Class} using this binder's * {@link ConfigurationPropertySource property sources}. * @param name the configuration property name to bind * @param target the target class * @param <T> the bound type * @return the binding result (never {@code null}) * @see #bind(ConfigurationPropertyName, Bindable, BindHandler) */ public <T> BindResult<T> bind(String name, Class<T> target) { return bind(name, Bindable.of(target)); }
/** * Create a new {@link Bindable} {@link List} of the specified element type. * @param <E> the element type * @param elementType the list element type * @return a {@link Bindable} instance */ public static <E> Bindable<List<E>> listOf(Class<E> elementType) { return of(ResolvableType.forClassWithGenerics(List.class, elementType)); }
/** * Create a new {@link Bindable} {@link Map} of the specified key and value type. * @param <K> the key type * @param <V> the value type * @param keyType the map key type * @param valueType the map value type * @return a {@link Bindable} instance */ public static <K, V> Bindable<Map<K, V>> mapOf(Class<K> keyType, Class<V> valueType) { return of(ResolvableType.forClassWithGenerics(Map.class, keyType, valueType)); }
/** * Create a new {@link Bindable} of the type of the specified instance with an * existing value equal to the instance. * @param <T> the source type * @param instance the instance (must not be {@code null}) * @return a {@link Bindable} instance * @see #of(ResolvableType) * @see #withExistingValue(Object) */ @SuppressWarnings("unchecked") public static <T> Bindable<T> ofInstance(T instance) { Assert.notNull(instance, "Instance must not be null"); Class<T> type = (Class<T>) instance.getClass(); return of(type).withExistingValue(instance); }
private void bindIndexed(ConfigurationPropertySource source, ConfigurationPropertyName root, AggregateElementBinder elementBinder, IndexedCollectionSupplier collection, ResolvableType elementType) { MultiValueMap<String, ConfigurationProperty> knownIndexedChildren = getKnownIndexedChildren( source, root); for (int i = 0; i < Integer.MAX_VALUE; i++) { ConfigurationPropertyName name = root .append((i != 0) ? "[" + i + "]" : INDEX_ZERO); Object value = elementBinder.bind(name, Bindable.of(elementType), source); if (value == null) { break; } knownIndexedChildren.remove(name.getLastElement(Form.UNIFORM)); collection.get().add(value); } assertNoUnboundChildren(knownIndexedChildren); }
private <T> boolean bind(BeanSupplier<T> beanSupplier, BeanPropertyBinder propertyBinder, BeanProperty property) { String propertyName = property.getName(); ResolvableType type = property.getType(); Supplier<Object> value = property.getValue(beanSupplier); Annotation[] annotations = property.getAnnotations(); Object bound = propertyBinder.bindProperty(propertyName, Bindable.of(type).withSuppliedValue(value).withAnnotations(annotations)); if (bound == null) { return false; } if (property.isSettable()) { property.setValue(beanSupplier, bound); } else if (value == null || !bound.equals(value.get())) { throw new IllegalStateException( "No setter found for property: " + property.getName()); } return true; }
private void bind(Object bean, String beanName, ConfigurationProperties annotation) { ResolvableType type = getBeanType(bean, beanName); Validated validated = getAnnotation(bean, beanName, Validated.class); Annotation[] annotations = (validated != null) ? new Annotation[] { annotation, validated } : new Annotation[] { annotation }; Bindable<?> target = Bindable.of(type).withExistingValue(bean) .withAnnotations(annotations); try { this.configurationPropertiesBinder.bind(target); } catch (Exception ex) { throw new ConfigurationPropertiesBindException(beanName, bean, annotation, ex); } }
@Bean(name = CONFIG_SINGLETON) public Singleton<JasyptEncryptorConfigurationProperties> configProps( @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") final EnvCopy envCopy, final ConfigurableBeanFactory bf) { return new Singleton<>(() -> { final BindHandler handler = new IgnoreErrorsBindHandler(BindHandler.DEFAULT); final MutablePropertySources propertySources = envCopy.get().getPropertySources(); final Binder binder = new Binder(ConfigurationPropertySources.from(propertySources), new PropertySourcesPlaceholdersResolver(propertySources), ApplicationConversionService.getSharedInstance(), bf::copyRegisteredEditorsTo); final JasyptEncryptorConfigurationProperties config = new JasyptEncryptorConfigurationProperties(); final ResolvableType type = ResolvableType.forClass(JasyptEncryptorConfigurationProperties.class); final Annotation annotation = AnnotationUtils.findAnnotation(JasyptEncryptorConfigurationProperties.class, ConfigurationProperties.class); final Annotation[] annotations = new Annotation[] {annotation}; final Bindable<?> target = Bindable.of(type).withExistingValue(config).withAnnotations(annotations); binder.bind("jasypt.encryptor", target, handler); return config; }); }
private static boolean isEnabled(Environment environment, String prefix, boolean defaultValue) { String property = prefix.endsWith(".") ? prefix + "enabled" : prefix + ".enabled"; Binder binder = Binder.get(environment); return binder.bind(property, Bindable.of(Boolean.class)).orElse(defaultValue); } }
public static void main(String[] args) { // Fluent API 构建新 Bindable 对象(不变) Bindable bindable = Bindable.of(int.class) .withExistingValue(1) .withAnnotations(); echo(bindable); }
private LocElasticJobProperties resolverJobProperties() { try { return Binder.get(environment) .bind("loc.elasticjob", Bindable.of(LocElasticJobProperties.class)) .orElseThrow( () -> new FatalBeanException("Could not bind DataSourceSettings properties")); } catch (Exception e) { //ignore log.error("Could not bind job properties, error message is {}", e.getMessage(), e); throw new FatalBeanException("Could not bind job properties", e); } }
private boolean hasCustomServerPort(List<String> properties) { Binder binder = new Binder(convertToConfigurationPropertySource(properties)); return binder.bind("server.port", Bindable.of(String.class)).isBound(); }
protected <T> T resolverSetting(Class<T> clazz, MutablePropertySources propertySources) { return new Binder(ConfigurationPropertySources.from(propertySources)) .bind("loc", Bindable.of(clazz)) .orElseThrow(() -> new FatalBeanException("Could not bind properties")); } }
private <T extends DataSource> T bind(Class<T> clazz, Map properties) { ConfigurationPropertySource source = new MapConfigurationPropertySource(properties); Binder binder = new Binder(new ConfigurationPropertySource[]{source.withAliases(aliases)}); // 通过类型绑定参数并获得实例对象 return binder.bind(ConfigurationPropertyName.EMPTY, Bindable.of(clazz)).get(); }
/** * create a hikari data source * @see org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration.Hikari#dataSource */ public HikariDataSource createDataSource() { HikariDataSource dataSource = dataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build(); if (StringUtils.hasText(dataSourceProperties.getName())) { dataSource.setPoolName(dataSourceProperties.getName()); } Bindable<?> target = Bindable.of(HikariDataSource.class).withExistingValue(dataSource); this.binder.bind("spring.datasource.hikari", target); return dataSource; }
private WebApplicationType getWebApplicationType( MergedContextConfiguration configuration) { ConfigurationPropertySource source = new MapConfigurationPropertySource( TestPropertySourceUtils.convertInlinedPropertiesToMap( configuration.getPropertySourceProperties())); Binder binder = new Binder(source); return binder .bind("spring.main.web-application-type", Bindable.of(WebApplicationType.class)) .orElseGet(this::deduceWebApplicationType); }
@Bean(name = CONFIG_SINGLETON) public Singleton<JasyptEncryptorConfigurationProperties> configProps( @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") final EnvCopy envCopy, final ConfigurableBeanFactory bf) { return new Singleton<>(() -> { final BindHandler handler = new IgnoreErrorsBindHandler(BindHandler.DEFAULT); final MutablePropertySources propertySources = envCopy.get().getPropertySources(); final Binder binder = new Binder(ConfigurationPropertySources.from(propertySources), new PropertySourcesPlaceholdersResolver(propertySources), ApplicationConversionService.getSharedInstance(), bf::copyRegisteredEditorsTo); final JasyptEncryptorConfigurationProperties config = new JasyptEncryptorConfigurationProperties(); final ResolvableType type = ResolvableType.forClass(JasyptEncryptorConfigurationProperties.class); final Annotation annotation = AnnotationUtils.findAnnotation(JasyptEncryptorConfigurationProperties.class, ConfigurationProperties.class); final Annotation[] annotations = new Annotation[] {annotation}; final Bindable<?> target = Bindable.of(type).withExistingValue(config).withAnnotations(annotations); binder.bind("jasypt.encryptor", target, handler); return config; }); }