@Override public <T> T bind(ConfigurationPropertyName name, Bindable<T> target, Context context, BeanPropertyBinder propertyBinder) { boolean hasKnownBindableProperties = hasKnownBindableProperties(name, context); Bean<T> bean = Bean.get(target, hasKnownBindableProperties); if (bean == null) { return null; } BeanSupplier<T> beanSupplier = bean.getSupplier(target); boolean bound = bind(propertyBinder, bean, beanSupplier); return (bound ? beanSupplier.get() : null); }
private <T> T handleBindResult(ConfigurationPropertyName name, Bindable<T> target, BindHandler handler, Context context, Object result) throws Exception { if (result != null) { result = handler.onSuccess(name, target, context, result); result = context.getConverter().convert(result, target); } handler.onFinish(name, target, context, result); return context.getConverter().convert(result, target); }
private <C> C convert(Object value, ResolvableType type, Annotation... annotations) { value = getContext().getPlaceholdersResolver().resolvePlaceholders(value); return getContext().getConverter().convert(value, type, annotations); }
/** * Bind the environment to the {@link SpringApplication}. * @param environment the environment to bind */ protected void bindToSpringApplication(ConfigurableEnvironment environment) { try { Binder.get(environment).bind("spring.main", Bindable.ofInstance(this)); } catch (Exception ex) { throw new IllegalStateException("Cannot bind to SpringApplication", ex); } }
@Override public <C extends AbstractConfig> void bind(String prefix, C dubboConfig) { Iterable<PropertySource<?>> propertySources = getPropertySources(); // Converts ConfigurationPropertySources Iterable<ConfigurationPropertySource> configurationPropertySources = from(propertySources); // Wrap Bindable from DubboConfig instance Bindable<C> bindable = Bindable.ofInstance(dubboConfig); Binder binder = new Binder(configurationPropertySources, new PropertySourcesPlaceholdersResolver(propertySources)); // Get BindHandler BindHandler bindHandler = getBindHandler(); // Bind binder.bind(prefix, bindable, bindHandler); }
/** * 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)); }
private <T> Object bindAggregate(ConfigurationPropertyName name, Bindable<T> target, BindHandler handler, Context context, AggregateBinder<?> aggregateBinder) { AggregateElementBinder elementBinder = (itemName, itemTarget, source) -> { boolean allowRecursiveBinding = aggregateBinder .isAllowRecursiveBinding(source); Supplier<?> supplier = () -> bind(itemName, itemTarget, handler, context, allowRecursiveBinding); return context.withSource(source, supplier); }; return context.withIncreasedDepth( () -> aggregateBinder.bind(name, target, elementBinder)); }
/** * Bind the specified target {@link Bindable} using this binder's * {@link ConfigurationPropertySource property sources}. * @param name the configuration property name to bind * @param target the target bindable * @param <T> the bound type * @return the binding result (never {@code null}) * @see #bind(ConfigurationPropertyName, Bindable, BindHandler) */ public <T> BindResult<T> bind(ConfigurationPropertyName name, Bindable<T> target) { return bind(name, target, null); }
public void bindEntries(ConfigurationPropertySource source, Map<Object, Object> map) { if (source instanceof IterableConfigurationPropertySource) { for (ConfigurationPropertyName name : (IterableConfigurationPropertySource) source) { Bindable<?> valueBindable = getValueBindable(name); ConfigurationPropertyName entryName = getEntryName(source, name); Object key = getContext().getConverter() .convert(getKeyName(entryName), this.keyType); map.computeIfAbsent(key, (k) -> this.elementBinder.bind(entryName, valueBindable)); } } }
private <T> T handleBindError(ConfigurationPropertyName name, Bindable<T> target, BindHandler handler, Context context, Exception error) { try { Object result = handler.onFailure(name, target, context, error); return context.getConverter().convert(result, target); } catch (Exception ex) { if (ex instanceof BindException) { throw (BindException) ex; } throw new BindException(name, target, context.getConfigurationProperty(), ex); } }
public <T> T convert(Object result, Bindable<T> target) { return convert(result, target.getType(), target.getAnnotations()); }
private AggregateBinder<?> getAggregateBinder(Bindable<?> target, Context context) { Class<?> resolvedType = target.getType().resolve(Object.class); if (Map.class.isAssignableFrom(resolvedType)) { return new MapBinder(context); } if (Collection.class.isAssignableFrom(resolvedType)) { return new CollectionBinder(context); } if (target.getType().isArray()) { return new ArrayBinder(context); } return null; }
private <T> Object bindProperty(Bindable<T> target, Context context, ConfigurationProperty property) { context.setConfigurationProperty(property); Object result = property.getValue(); result = this.placeholdersResolver.resolvePlaceholders(result); result = context.getConverter().convert(result, target); return result; }
private BindConverter(ConversionService conversionService, Consumer<PropertyEditorRegistry> propertyEditorInitializer) { Assert.notNull(conversionService, "ConversionService must not be null"); List<ConversionService> conversionServices = getConversionServices( conversionService, propertyEditorInitializer); this.conversionService = new CompositeConversionService(conversionServices); }
@Override public Object onFailure(ConfigurationPropertyName name, Bindable<?> target, BindContext context, Exception error) throws Exception { if (context.getDepth() == 0 && error instanceof ConverterNotFoundException) { return null; } throw error; }
@SuppressWarnings("unchecked") static <T> BindResult<T> of(T value) { if (value == null) { return (BindResult<T>) UNBOUND; } return new BindResult<>(value); }
/** * Bind the given name to a target bindable. * @param name the name to bind * @param target the target bindable * @return a bound object or {@code null} */ default Object bind(ConfigurationPropertyName name, Bindable<?> target) { return bind(name, target, null); }
@Override public Object onFailure(ConfigurationPropertyName name, Bindable<?> target, BindContext context, Exception error) throws Exception { return this.parent.onFailure(name, target, context, error); }
Bean(ResolvableType resolvableType, Class<?> type) { this.resolvableType = resolvableType; this.type = type; putProperties(type); }
private ClientProperties getClientProperties(ConditionContext context) { ClientProperties clientProperties = new ClientProperties(context.getEnvironment()); Binder.get(context.getEnvironment()).bind("spring.boot.admin.client", Bindable.ofInstance(clientProperties)); return clientProperties; } }