/** * 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); }
/** * 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)); }
/** * 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 handler the bind handler (may be {@code null}) * @param <T> the bound type * @return the binding result (never {@code null}) */ public <T> BindResult<T> bind(String name, Bindable<T> target, BindHandler handler) { return bind(ConfigurationPropertyName.of(name), target, handler); }
/** * 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(String name, Bindable<T> target) { return bind(ConfigurationPropertyName.of(name), target, null); }
/** * Returns list of values of the `type` field from the `spring.cloud.config.server.composite` collection. */ public static List<String> getCompositeTypeList(Environment environment) { return Binder.get(environment) .bind("spring.cloud.config.server", CompositeConfig.class) .get().getComposite().stream() .map(map -> (String)map.get("type")) .collect(Collectors.toList()); }
/** * 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); } }
private Set<Profile> getProfiles(Binder binder, String name) { return binder.bind(name, STRING_ARRAY).map(this::asProfileSet) .orElse(Collections.emptySet()); }
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 handler the bind handler (may be {@code null}) * @param <T> the bound type * @return the binding result (never {@code null}) */ public <T> BindResult<T> bind(ConfigurationPropertyName name, Bindable<T> target, BindHandler handler) { Assert.notNull(name, "Name must not be null"); Assert.notNull(target, "Target must not be null"); handler = (handler != null) ? handler : BindHandler.DEFAULT; Context context = new Context(); T bound = bind(name, target, handler, context, false); return BindResult.of(bound); }
private <P extends EnvironmentRepositoryProperties> P bindProperties( int index, Class<P> propertiesClass, Environment environment) { Binder binder = Binder.get(environment); String environmentConfigurationPropertyName = String.format("spring.cloud.config.server.composite[%d]", index); P properties = binder.bind(environmentConfigurationPropertyName, propertiesClass).orElseCreate(propertiesClass); properties.setOrder(index + 1); return properties; } }
@Bean @ConditionalOnMissingBean public TemplateRenderer templateRenderer(Environment environment) { Binder binder = Binder.get(environment); boolean cache = binder.bind("spring.mustache.cache", Boolean.class).orElse(true); TemplateRenderer templateRenderer = new TemplateRenderer(); templateRenderer.setCache(cache); return templateRenderer; }
private List<Document> asDocuments(List<PropertySource<?>> loaded) { if (loaded == null) { return Collections.emptyList(); } return loaded.stream().map((propertySource) -> { Binder binder = new Binder( ConfigurationPropertySources.from(propertySource), this.placeholdersResolver); return new Document(propertySource, binder.bind("spring.profiles", STRING_ARRAY).orElse(null), getProfiles(binder, ACTIVE_PROFILES_PROPERTY), getProfiles(binder, INCLUDE_PROFILES_PROPERTY)); }).collect(Collectors.toList()); }
private Object bindBean(ConfigurationPropertyName name, Bindable<?> target, BindHandler handler, Context context, boolean allowRecursiveBinding) { if (containsNoDescendantOf(context.getSources(), name) || isUnbindableBean(name, target, context)) { return null; } BeanPropertyBinder propertyBinder = (propertyName, propertyTarget) -> bind( name.append(propertyName), propertyTarget, handler, context, false); Class<?> type = target.getType().resolve(Object.class); if (!allowRecursiveBinding && context.hasBoundBean(type)) { return null; } return context.withBean(type, () -> { Stream<?> boundBeans = BEAN_BINDERS.stream() .map((b) -> b.bind(name, target, context, propertyBinder)); return boundBeans.filter(Objects::nonNull).findFirst().orElse(null); }); }
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; } }
public void bind(Bindable<?> target) { ConfigurationProperties annotation = target .getAnnotation(ConfigurationProperties.class); Assert.state(annotation != null, () -> "Missing @ConfigurationProperties on " + target); List<Validator> validators = getValidators(target); BindHandler bindHandler = getBindHandler(annotation, validators); getBinder().bind(annotation.prefix(), target, bindHandler); }
public static void bind(Object o, Map<String, Object> properties, String configurationPropertyName, String bindingName, Validator validator, ConversionService conversionService) { Object toBind = getTargetObject(o); new Binder(Collections.singletonList(new MapConfigurationPropertySource(properties)), null, conversionService) .bind(configurationPropertyName, Bindable.ofInstance(toBind)); if (validator != null) { BindingResult errors = new BeanPropertyBindingResult(toBind, bindingName); validator.validate(toBind, errors); if (errors.hasErrors()) { throw new RuntimeException(new BindException(errors)); } } }
@Override public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { ConfigurableEnvironment environment = event.getEnvironment(); Binder.get(environment) .bind("spring.output.ansi.enabled", AnsiOutput.Enabled.class) .ifBound(AnsiOutput::setEnabled); AnsiOutput.setConsoleAvailable(environment .getProperty("spring.output.ansi.console-available", Boolean.class)); }
@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); }
private void bind(DataSource result) { ConfigurationPropertySource source = new MapConfigurationPropertySource( this.properties); ConfigurationPropertyNameAliases aliases = new ConfigurationPropertyNameAliases(); aliases.addAliases("url", "jdbc-url"); aliases.addAliases("username", "user"); Binder binder = new Binder(source.withAliases(aliases)); binder.bind(ConfigurationPropertyName.EMPTY, Bindable.ofInstance(result)); }
private static InitializrProperties load(Resource resource) { ConfigurationPropertySource source = new MapConfigurationPropertySource( loadProperties(resource)); Binder binder = new Binder(source); return binder.bind("initializr", InitializrProperties.class).get(); }