下面列出了org.springframework.boot.context.properties.bind.Binder#get ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
@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);
}
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata annotatedTypeMetadata) {
Binder binder = Binder.get(context.getEnvironment());
Map<String, Object> sslProperties = binder.bind("camel.ssl.config", Bindable.mapOf(String.class, Object.class)).orElse(Collections.emptyMap());
ConditionMessage.Builder message = ConditionMessage.forCondition("camel.ssl.config");
if (sslProperties.size() > 0) {
return ConditionOutcome.match(message.because("enabled"));
}
return ConditionOutcome.noMatch(message.because("not enabled"));
}
/**
* init environment
*
* @param environment environment
*/
@Override
public void setEnvironment(Environment environment) {
this.env = environment;
// bing binder
binder = Binder.get(this.env);
}
/**
* init environment
*
* @param environment environment
*/
@Override
public void setEnvironment(Environment environment) {
this.env = environment;
// bing binder
binder = Binder.get(this.env);
}
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;
}
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
ConditionMessage.Builder message = ConditionMessage.forCondition("OAuth ResourceServer Condition");
Environment environment = context.getEnvironment();
if (!(environment instanceof ConfigurableEnvironment)) {
return ConditionOutcome.noMatch(message.didNotFind("A ConfigurableEnvironment").atAll());
}
if (hasOAuthClientId(environment)) {
return ConditionOutcome.match(message.foundExactly("client-id property"));
}
Binder binder = Binder.get(environment);
String prefix = "security.oauth2.resource.";
if (binder.bind(prefix + "jwt", STRING_OBJECT_MAP).isBound()) {
return ConditionOutcome.match(message.foundExactly("JWT resource configuration"));
}
if (binder.bind(prefix + "jwk", STRING_OBJECT_MAP).isBound()) {
return ConditionOutcome.match(message.foundExactly("JWK resource configuration"));
}
if (StringUtils.hasText(environment.getProperty(prefix + "user-info-uri"))) {
return ConditionOutcome.match(message.foundExactly("user-info-uri property"));
}
if (StringUtils.hasText(environment.getProperty(prefix + "token-info-uri"))) {
return ConditionOutcome.match(message.foundExactly("token-info-uri property"));
}
if (ClassUtils.isPresent(AUTHORIZATION_ANNOTATION, null)) {
if (AuthorizationServerEndpointsConfigurationBeanCondition.matches(context)) {
return ConditionOutcome.match(message.found("class").items(AUTHORIZATION_ANNOTATION));
}
}
return ConditionOutcome
.noMatch(message.didNotFind("client ID, JWT resource or authorization server").atAll());
}
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.bindOrCreate(environmentConfigurationPropertyName,
propertiesClass);
properties.setOrder(index + 1);
return properties;
}
private Cache determineCache(Environment environment, CacheManager cacheManager) {
if (cacheManager != null) {
Binder binder = Binder.get(environment);
boolean cache = binder.bind("spring.mustache.cache", Boolean.class).orElse(true);
if (cache) {
return cacheManager.getCache("initializr.templates");
}
}
return new NoOpCache("templates");
}
@Override
public void setEnvironment(@NonNull Environment environment) {
binder = Binder.get(environment);
}
@Override
public void setEnvironment(@NonNull Environment environment) {
binder = Binder.get(environment);
}
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 SpringApplication createSpringApplication(String[] args) {
var environment = new StandardEnvironment();
environment.setDefaultProfiles("exporter", "gateway");
environment.getPropertySources().addFirst(new SimpleCommandLinePropertySource(args));
var pluginsDir = environment.getProperty("plugins.dir", String.class, "./plugins");
var pathMatcher = environment.getProperty("plugins.pathMatcher", String.class, "*.jar");
var pluginsRoot = Paths.get(pluginsDir).toAbsolutePath().normalize();
log.info("Loading plugins from '{}' with matcher: '{}'", pluginsRoot, pathMatcher);
var pluginManager = new LiiklusPluginManager(pluginsRoot, pathMatcher);
pluginManager.loadPlugins();
pluginManager.startPlugins();
var binder = Binder.get(environment);
var application = new SpringApplication(Application.class) {
@Override
protected void load(ApplicationContext context, Object[] sources) {
// We don't want the annotation bean definition reader
}
};
application.setWebApplicationType(WebApplicationType.REACTIVE);
application.setApplicationContextClass(ReactiveWebServerApplicationContext.class);
application.setEnvironment(environment);
application.addInitializers(
new StringCodecInitializer(false, true),
new ResourceCodecInitializer(false),
new ReactiveWebServerInitializer(
binder.bind("server", ServerProperties.class).orElseGet(ServerProperties::new),
binder.bind("spring.resources", ResourceProperties.class).orElseGet(ResourceProperties::new),
binder.bind("spring.webflux", WebFluxProperties.class).orElseGet(WebFluxProperties::new),
new NettyReactiveWebServerFactory()
),
new GatewayConfiguration(),
(GenericApplicationContext applicationContext) -> {
applicationContext.registerBean("health", RouterFunction.class, () -> {
return RouterFunctions.route()
.GET("/health", __ -> ServerResponse.ok().bodyValue("OK"))
.build();
});
applicationContext.registerBean(PluginManager.class, () -> pluginManager);
}
);
application.addInitializers(
pluginManager.getExtensionClasses(ApplicationContextInitializer.class).stream()
.map(it -> {
try {
return it.getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
})
.toArray(ApplicationContextInitializer[]::new)
);
return application;
}