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()); }
/** * Create a new {@link Binder} instance from the specified environment. * @param environment the environment source (must have attached * {@link ConfigurationPropertySources}) * @return a {@link Binder} instance */ public static Binder get(Environment environment) { return new Binder(ConfigurationPropertySources.get(environment), new PropertySourcesPlaceholdersResolver(environment)); }
private Binder getBinder() { if (this.binder == null) { this.binder = new Binder(getConfigurationPropertySources(), getPropertySourcesPlaceholdersResolver(), getConversionService(), getPropertyEditorInitializer()); } return this.binder; }
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 <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(); }
/** * Extract the raw content based on the specified {@link PropertySource}. * @param propertySource the property source to use * @return the raw content */ protected Map<String, Object> extractContent(PropertySource<?> propertySource) { return new Binder(ConfigurationPropertySources.from(propertySource)) .bind("", STRING_OBJECT_MAP).orElseGet(LinkedHashMap::new); }
@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; }); }
@Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = (ConfigurableApplicationContext) applicationContext; this.propertySources = this.applicationContext.getEnvironment().getPropertySources(); this.binder = new Binder(getConfigurationPropertySources(), getPropertySourcesPlaceholdersResolver(), getConversionService(), getPropertyEditorInitializer()); }
private boolean hasCustomServerPort(List<String> properties) { Binder binder = new Binder(convertToConfigurationPropertySource(properties)); return binder.bind("server.port", Bindable.of(String.class)).isBound(); }
/** * 绑定参数,以下三个方法都是参考DataSourceBuilder的bind方法实现的,目的是尽量保证我们自己添加的数据源构造过程与springboot保持一致 * * @param result * @param properties */ private void bind(DataSource result, Map properties) { ConfigurationPropertySource source = new MapConfigurationPropertySource(properties); Binder binder = new Binder(new ConfigurationPropertySource[]{source.withAliases(aliases)}); // 将参数绑定到对象 binder.bind(ConfigurationPropertyName.EMPTY, Bindable.ofInstance(result)); }
@SuppressWarnings("unchecked") private void bindToDefault(String binding) { T extendedBindingPropertiesTarget = (T) BeanUtils.instantiateClass(this.getExtendedPropertiesEntryClass()); Binder binder = new Binder(ConfigurationPropertySources.get(applicationContext.getEnvironment()), new PropertySourcesPlaceholdersResolver(applicationContext.getEnvironment()), IntegrationUtils.getConversionService(applicationContext.getBeanFactory()), null); binder.bind(this.getDefaultsPrefix(), Bindable.ofInstance(extendedBindingPropertiesTarget)); this.bindings.put(binding, extendedBindingPropertiesTarget); } }
private void bindToDefault(String binding) { BindingProperties bindingPropertiesTarget = new BindingProperties(); Binder binder = new Binder(ConfigurationPropertySources.get(applicationContext.getEnvironment()), new PropertySourcesPlaceholdersResolver(applicationContext.getEnvironment()), IntegrationUtils.getConversionService(applicationContext.getBeanFactory()), null); binder.bind("spring.cloud.stream.default", Bindable.ofInstance(bindingPropertiesTarget)); this.bindings.put(binding, bindingPropertiesTarget); } }
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 void bind(DataSource dataSource) { ConfigurationPropertySource source = new MapConfigurationPropertySource(this.properties); ConfigurationPropertyNameAliases aliases = new ConfigurationPropertyNameAliases(); Binder binder = new Binder(source.withAliases(aliases)); binder.bind(ConfigurationPropertyName.EMPTY, Bindable.ofInstance(dataSource)); }
private List<City> getCities() { Properties yaml = loadCitiesYaml(); MapConfigurationPropertySource source = new MapConfigurationPropertySource(yaml); return new Binder(source).bind("cities", Bindable.listOf(City.class)).get(); }
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(); }
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); }
private void bind(XADataSource 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)); }