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);

相关推荐

  1. spring语法

    2024-02-02 22:22:01       50 阅读
  2. Spring注解!!!

    2024-02-02 22:22:01       67 阅读
  3. spring 注解

    2024-02-02 22:22:01       30 阅读
  4. TCL语法

    2024-02-02 22:22:01       42 阅读
  5. solibity语法

    2024-02-02 22:22:01       36 阅读
  6. Spring Boot注解

    2024-02-02 22:22:01       64 阅读
  7. Spring框架注解

    2024-02-02 22:22:01       48 阅读
  8. (自用)Spring配置

    2024-02-02 22:22:01       39 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-02 22:22:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-02 22:22:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-02 22:22:01       87 阅读
  4. Python语言-面向对象

    2024-02-02 22:22:01       96 阅读

热门阅读

  1. Linux查询日志命令

    2024-02-02 22:22:01       44 阅读
  2. 学习C语言的第31天

    2024-02-02 22:22:01       44 阅读
  3. centos7安装redis

    2024-02-02 22:22:01       53 阅读
  4. C#基础题

    2024-02-02 22:22:01       53 阅读
  5. C++:默认参数竟是静态的?

    2024-02-02 22:22:01       47 阅读