Java Code Examples for org.springframework.boot.context.properties.bind.Binder#bind()
The following examples show how to use
org.springframework.boot.context.properties.bind.Binder#bind() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BinderBootstrap.java From thinking-in-spring-boot-samples with Apache License 2.0 | 7 votes |
public static void main(String[] args) { ConfigurableApplicationContext context = new SpringApplicationBuilder(BinderBootstrap.class) .web(WebApplicationType.NONE) // 非 Web 应用 .properties("user.city.postCode=0731") .run(args); ConfigurableEnvironment environment = context.getEnvironment(); // 从 Environment 中获取 ConfigurationPropertySource 集合 Iterable<ConfigurationPropertySource> sources = ConfigurationPropertySources.get(environment); // 构造 Binder 对象,并使用 ConfigurationPropertySource 集合作为配置源 Binder binder = new Binder(sources); // 构造 ConfigurationPropertyName(Spring Boot 2.0 API) ConfigurationPropertyName propertyName = ConfigurationPropertyName.of("user.city.post-code"); // 构造 Bindable 对象,包装 postCode Bindable<String> postCodeBindable = Bindable.of(String.class); BindResult<String> result = binder.bind(propertyName, postCodeBindable); String postCode = result.get(); System.out.println("postCode = " + postCode); // 关闭上下文 context.close(); }
Example 2
Source File: ExternalizedConfigurationBinderBootstrap.java From thinking-in-spring-boot-samples with Apache License 2.0 | 7 votes |
public static void main(String[] args) throws IOException { // application.properties 文件资源 classpath 路径 String location = "application.properties"; // 编码化的 Resource 对象(解决乱码问题) EncodedResource resource = new EncodedResource(new ClassPathResource(location), "UTF-8"); // 加载 application.properties 文件,转化为 Properties 对象 Properties properties = PropertiesLoaderUtils.loadProperties(resource); // 创建 Properties 类型的 PropertySource PropertiesPropertySource propertySource = new PropertiesPropertySource("map", properties); // 转化为 Spring Boot 2 外部化配置源 ConfigurationPropertySource 集合 Iterable<ConfigurationPropertySource> propertySources = ConfigurationPropertySources.from(propertySource); // 创建 Spring Boot 2 Binder 对象(设置 ConversionService ,扩展类型转换能力) Binder binder = new Binder(propertySources, null, conversionService()); // 执行绑定,返回绑定结果 BindResult<User> bindResult = binder.bind("user", User.class); // 获取绑定对象 User user = bindResult.get(); // 输出结果 System.out.println(user); }
Example 3
Source File: GenericPropertiesConfiguration.java From camunda-bpm-platform with Apache License 2.0 | 6 votes |
@Override public void preInit(SpringProcessEngineConfiguration springProcessEngineConfiguration) { GenericProperties genericProperties = camundaBpmProperties.getGenericProperties(); final Map<String, Object> properties = genericProperties.getProperties(); if (!CollectionUtils.isEmpty(properties)) { ConfigurationPropertySource source = new MapConfigurationPropertySource(properties); Binder binder = new Binder(source); try { if (genericProperties.isIgnoreUnknownFields()) { binder.bind(ConfigurationPropertyName.EMPTY, Bindable.ofInstance(springProcessEngineConfiguration)); } else { binder.bind(ConfigurationPropertyName.EMPTY, Bindable.ofInstance(springProcessEngineConfiguration), new NoUnboundElementsBindHandler(BindHandler.DEFAULT)); } } catch (Exception e) { throw LOG.exceptionDuringBinding(e.getMessage()); } logger.debug("properties bound to configuration: {}", genericProperties); } }
Example 4
Source File: BrpcProperties.java From brpc-java with Apache License 2.0 | 6 votes |
public BrpcConfig getServiceConfig(Class<?> serviceInterface) { BrpcConfig brpcConfig = new BrpcConfig(global); if (brpcConfig.getClient() == null) { brpcConfig.setClient(new RpcClientConfig()); } if (brpcConfig.getServer() == null) { brpcConfig.setServer(new RpcServerConfig()); } if (brpcConfig.getNaming() == null) { brpcConfig.setNaming(new RpcNamingConfig()); } String prefix = "brpc.custom." + normalizeName(serviceInterface.getName()) + "."; Binder binder = Binder.get(environment); binder.bind(prefix + "client", Bindable.ofInstance(brpcConfig.getClient())); binder.bind(prefix + "server", Bindable.ofInstance(brpcConfig.getServer())); binder.bind(prefix + "naming", Bindable.ofInstance(brpcConfig.getNaming())); rewriteMap(brpcConfig.getNaming().getExtra()); return brpcConfig; }
Example 5
Source File: BinderDubboConfigBinder.java From dubbo-spring-boot-project with Apache License 2.0 | 6 votes |
@Override public void bind(Map<String, Object> configurationProperties, boolean ignoreUnknownFields, boolean ignoreInvalidFields, Object configurationBean) { Iterable<PropertySource<?>> propertySources = asList(new MapPropertySource("internal", configurationProperties)); // Converts ConfigurationPropertySources Iterable<ConfigurationPropertySource> configurationPropertySources = from(propertySources); // Wrap Bindable from DubboConfig instance Bindable bindable = Bindable.ofInstance(configurationBean); Binder binder = new Binder(configurationPropertySources, new PropertySourcesPlaceholdersResolver(propertySources)); // Get BindHandler BindHandler bindHandler = getBindHandler(ignoreUnknownFields, ignoreInvalidFields); // Bind binder.bind("", bindable, bindHandler); }
Example 6
Source File: NacosBootConfigurationPropertiesBinder.java From nacos-spring-boot-project with Apache License 2.0 | 6 votes |
@Override protected void doBind(Object bean, String beanName, String dataId, String groupId, String configType, NacosConfigurationProperties properties, String content, ConfigService configService) { String name = "nacos-bootstrap-" + beanName; NacosPropertySource propertySource = new NacosPropertySource(name, dataId, groupId, content, configType); environment.getPropertySources().addLast(propertySource); Binder binder = Binder.get(environment); ResolvableType type = getBeanType(bean, beanName); Bindable<?> target = Bindable.of(type).withExistingValue(bean); binder.bind(properties.prefix(), target); publishBoundEvent(bean, beanName, dataId, groupId, properties, content, configService); publishMetadataEvent(bean, beanName, dataId, groupId, properties); environment.getPropertySources().remove(name); }
Example 7
Source File: AbstractExtendedBindingProperties.java From spring-cloud-stream with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") private void bindToDefault(String binding) { T extendedBindingPropertiesTarget = (T) BeanUtils .instantiateClass(this.getExtendedPropertiesEntryClass()); Binder binder = new Binder( ConfigurationPropertySources .get(this.applicationContext.getEnvironment()), new PropertySourcesPlaceholdersResolver( this.applicationContext.getEnvironment()), IntegrationUtils.getConversionService( this.applicationContext.getBeanFactory()), null); binder.bind(this.getDefaultsPrefix(), Bindable.ofInstance(extendedBindingPropertiesTarget)); this.bindings.put(binding, extendedBindingPropertiesTarget); }
Example 8
Source File: JasyptEncryptorConfigurationProperties.java From jasypt-spring-boot with MIT License | 6 votes |
public static JasyptEncryptorConfigurationProperties bindConfigProps(ConfigurableEnvironment environment) { final BindHandler handler = new IgnoreErrorsBindHandler(BindHandler.DEFAULT); final MutablePropertySources propertySources = environment.getPropertySources(); final Binder binder = new Binder(ConfigurationPropertySources.from(propertySources), new PropertySourcesPlaceholdersResolver(propertySources), ApplicationConversionService.getSharedInstance()); 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; }
Example 9
Source File: GenericPropertiesConfiguration.java From camunda-bpm-spring-boot-starter with Apache License 2.0 | 6 votes |
@Override public void preInit(SpringProcessEngineConfiguration springProcessEngineConfiguration) { GenericProperties genericProperties = camundaBpmProperties.getGenericProperties(); final Map<String, Object> properties = genericProperties.getProperties(); if (!CollectionUtils.isEmpty(properties)) { ConfigurationPropertySource source = new MapConfigurationPropertySource(properties); Binder binder = new Binder(source); try { if (genericProperties.isIgnoreUnknownFields()) { binder.bind(ConfigurationPropertyName.EMPTY, Bindable.ofInstance(springProcessEngineConfiguration)); } else { binder.bind(ConfigurationPropertyName.EMPTY, Bindable.ofInstance(springProcessEngineConfiguration), new NoUnboundElementsBindHandler(BindHandler.DEFAULT)); } } catch (Exception e) { throw LOG.exceptionDuringBinding(e.getMessage()); } logger.debug("properties bound to configuration: {}", genericProperties); } }
Example 10
Source File: KafkaStreamsBinderSupportAutoConfiguration.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 5 votes |
@Bean @ConfigurationProperties(prefix = "spring.cloud.stream.kafka.streams.binder") public KafkaStreamsBinderConfigurationProperties binderConfigurationProperties( KafkaProperties kafkaProperties, ConfigurableEnvironment environment, BindingServiceProperties properties, ConfigurableApplicationContext context) throws Exception { final Map<String, BinderConfiguration> binderConfigurations = getBinderConfigurations( properties); for (Map.Entry<String, BinderConfiguration> entry : binderConfigurations .entrySet()) { final BinderConfiguration binderConfiguration = entry.getValue(); final String binderType = binderConfiguration.getBinderType(); if (binderType != null && (binderType.equals(KSTREAM_BINDER_TYPE) || binderType.equals(KTABLE_BINDER_TYPE) || binderType.equals(GLOBALKTABLE_BINDER_TYPE))) { Map<String, Object> binderProperties = new HashMap<>(); this.flatten(null, binderConfiguration.getProperties(), binderProperties); environment.getPropertySources().addFirst( new MapPropertySource(entry.getKey() + "-kafkaStreamsBinderEnv", binderProperties)); Binder binder = new Binder(ConfigurationPropertySources.get(environment), new PropertySourcesPlaceholdersResolver(environment), IntegrationUtils.getConversionService(context.getBeanFactory()), null); final Constructor<KafkaStreamsBinderConfigurationProperties> kafkaStreamsBinderConfigurationPropertiesConstructor = ReflectionUtils.accessibleConstructor(KafkaStreamsBinderConfigurationProperties.class, KafkaProperties.class); final KafkaStreamsBinderConfigurationProperties kafkaStreamsBinderConfigurationProperties = BeanUtils.instantiateClass(kafkaStreamsBinderConfigurationPropertiesConstructor, kafkaProperties); final BindResult<KafkaStreamsBinderConfigurationProperties> bind = binder.bind("spring.cloud.stream.kafka.streams.binder", Bindable.ofInstance(kafkaStreamsBinderConfigurationProperties)); context.getBeanFactory().registerSingleton( entry.getKey() + "-KafkaStreamsBinderConfigurationProperties", bind.get()); } } return new KafkaStreamsBinderConfigurationProperties(kafkaProperties); }
Example 11
Source File: DataSourceSpiBuilder.java From ByteJTA with GNU Lesser General Public License v3.0 | 5 votes |
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)); }
Example 12
Source File: BindingServiceProperties.java From spring-cloud-stream with Apache License 2.0 | 5 votes |
private void bindToDefault(String binding) { BindingProperties bindingPropertiesTarget = new BindingProperties(); Binder binder = new Binder( ConfigurationPropertySources .get(this.applicationContext.getEnvironment()), new PropertySourcesPlaceholdersResolver( this.applicationContext.getEnvironment()), IntegrationUtils.getConversionService( this.applicationContext.getBeanFactory()), null); binder.bind("spring.cloud.stream.default", Bindable.ofInstance(bindingPropertiesTarget)); this.bindings.put(binding, bindingPropertiesTarget); }
Example 13
Source File: ServiceBrokerPropertiesTest.java From spring-cloud-open-service-broker with Apache License 2.0 | 5 votes |
private ServiceBrokerProperties bindProperties() { ConfigurationPropertySource source = new MapConfigurationPropertySource(this.map); Binder binder = new Binder(source); ServiceBrokerProperties serviceBrokerProperties = new ServiceBrokerProperties(); binder.bind(PREFIX, Bindable.ofInstance(serviceBrokerProperties)); return serviceBrokerProperties; }
Example 14
Source File: XADataSourceBuilder.java From teiid-spring-boot with Apache License 2.0 | 5 votes |
private void bindXaProperties(Bindable<XADataSource> target) { ConfigurationPropertySource source = new MapConfigurationPropertySource( this.properties); ConfigurationPropertyNameAliases aliases = new ConfigurationPropertyNameAliases(); aliases.addAliases("url", "jdbc-url"); aliases.addAliases("username", "user"); aliases.addAliases("portNumber", "port"); aliases.addAliases("serverName", "server"); aliases.addAliases("databaseName", "database"); Binder binder = new Binder(source.withAliases(aliases)); binder.bind(ConfigurationPropertyName.EMPTY, target); }
Example 15
Source File: NacosConfigPropertiesUtils.java From nacos-spring-boot-project with Apache License 2.0 | 5 votes |
public static NacosConfigProperties buildNacosConfigProperties( ConfigurableEnvironment environment) { NacosConfigProperties nacosConfigProperties = new NacosConfigProperties(); Binder binder = Binder.get(environment); ResolvableType type = ResolvableType.forClass(NacosConfigProperties.class); Bindable<?> target = Bindable.of(type).withExistingValue(nacosConfigProperties); binder.bind(NacosConfigConstants.PREFIX, target); logger.info("nacosConfigProperties : {}", nacosConfigProperties); return nacosConfigProperties; }
Example 16
Source File: DetectCustomFieldParser.java From synopsys-detect with Apache License 2.0 | 5 votes |
public CustomFieldDocument parseCustomFieldDocument(final Map<String, String> currentProperties) throws DetectUserFriendlyException { try { final ConfigurationPropertySource source = new MapConfigurationPropertySource(currentProperties); final Binder objectBinder = new Binder(source); final BindResult<CustomFieldDocument> fieldDocumentBinding = objectBinder.bind("detect.custom.fields", CustomFieldDocument.class); final CustomFieldDocument fieldDocument = fieldDocumentBinding.orElse(new CustomFieldDocument()); fieldDocument.getProject().forEach(this::filterEmptyQuotes); fieldDocument.getVersion().forEach(this::filterEmptyQuotes); return fieldDocument; } catch (final Exception e) { throw new DetectUserFriendlyException("Unable to parse custom fields.", e, ExitCodeType.FAILURE_CONFIGURATION); } }
Example 17
Source File: DataSourceFactory.java From Milkomeda with MIT License | 4 votes |
@SuppressWarnings("rawtypes") private void bind(DataSource result, Map properties) { ConfigurationPropertySource source = new MapConfigurationPropertySource(properties); Binder binder = new Binder(source.withAliases(ALIASES)); binder.bind(ConfigurationPropertyName.EMPTY, Bindable.ofInstance(result)); }
Example 18
Source File: DataSourceCciBuilder.java From ByteJTA with GNU Lesser General Public License v3.0 | 4 votes |
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)); }