spring常用语法

etl表达式解析

if (rawValue != null && rawValue.startsWith("#{") && entryValue.endsWith("}")) {

// assume it's spel

StandardEvaluationContext context = new StandardEvaluationContext();

context.setBeanResolver(new BeanFactoryResolver(beanFactory));

Expression expression = parser.parseExpression(entryValue,

new TemplateParserContext());

value = expression.getValue(context);

}

属性绑定
复制代码
Bindable<T> bindable = Bindable.of(this.configurable.getConfigClass());T t =new Binder(propertySources, null, conversionService)
      .bindOrCreate(configurationPropertyName, bindable, handler);

ApplicationContext context = SpringApplication.run(Application.class, args);

Binder binder = Binder.get(context.getEnvironment());

绑定简单配置

FooProperties foo = binder.bind("com.didispace", Bindable.of(FooProperties.class)).get();

System.out.println(foo.getFoo());

绑定List配置

List<String> post = binder.bind("com.didispace.post", Bindable.listOf(String.class)).get();

System.out.println(post);

List<PostInfo> posts=binder.bind("com.didispace.posts",Bindable.listOf(PostInfo.class)).get();

System.out.println(posts);

// 读取配置 System.out.println(context.getEnvironment().containsProperty("com.didispace.database-platform"));

System.out.println(context.getEnvironment().containsProperty("com.didispace.databasePlatform"));

配置绑定到Map对象
复制代码
Map<String, DsConfig> dsMap = binder.bind("demo.dynamic", Bindable.mapOf(String.class, DsConfig.class)).get();
System.out.println("Map Config: " + dsMap);
配置转换处理
复制代码
String decPwd = binder.bind("demo.enc.pwd", Bindable.of(String.class))
        .map(s -> new String(Base64Utils.decodeFromString(s))).get();
System.out.println("解码之后的数据是: " + decPwd);
注册绑定过程回调

String dec = binder.bindOrCreate("demo.enc.pwd", Bindable.of(String.class), new BindHandler() {

@Override

public <T> Bindable<T> onStart(ConfigurationPropertyName name, Bindable<T> target, BindContext context) {

System.out.println("开始绑定: " + name);

return BindHandler.super.onStart(name, target, context);

}

@Override

public Object onSuccess(ConfigurationPropertyName name, Bindable<?> target, BindContext context, Object result) {

System.out.println("绑定成功!" + name + " val:" + target.getValue() + " res: " + result);

return new String(Base64Utils.decodeFromString((String) result));

}

@Override

public Object onCreate(ConfigurationPropertyName name, Bindable<?> target, BindContext context, Object result) {

System.out.println("创建: " + name + " val:" + target.getValue() + " res: " + result);

return BindHandler.super.onCreate(name, target, context, result);

}

@Override

public Object onFailure(ConfigurationPropertyName name, Bindable<?> target, BindContext context, Exception error) throws Exception {

System.out.println("绑定失败! " + name + " " + error.getMessage());

return BindHandler.super.onFailure(name, target, context, error);

}

@Override

public void onFinish(ConfigurationPropertyName name, Bindable<?> target, BindContext context, Object result) throws Exception {

System.out.println("绑定结束: " + name + " val:" + target.getValue() + " res: " + result);

BindHandler.super.onFinish(name, target, context, result);

}

});

System.out.println("绑定回调: " + dec);

相关推荐
碎梦归途3 天前
23种设计模式-创建型模式之原型模式(Java版本)
java·开发语言·jvm·设计模式·原型模式
巴巴_羊3 天前
JavsScript 原型链
开发语言·javascript·原型模式
Leaf吧4 天前
java 设计模式 原型模式
java·设计模式·原型模式
大猫会长4 天前
js原型链prototype解释
原型模式
GISer_Jing6 天前
[终极版]Javascript面试全解
javascript·面试·原型模式
尘寰ya10 天前
如何实现一个“纯净”的空对象(无原型链属性)?
javascript·面试·原型模式
尘寰ya10 天前
什么是原型污染?如何防止原型污染?
前端·面试·原型模式
啊QQQQQ11 天前
设计模式-原型模式
java·设计模式·原型模式
Attacking-Coder12 天前
前端面试宝典---原型链
前端·javascript·原型模式
天下代码客13 天前
【JS】关于原型/原型链
原型模式