JAVA中YML:几个用法

本文介绍了如何在SpringBoot项目中使用YAML配置文件,包括容器加载配置、将YAML转换为Java对象、配置文件的打印以及处理可能的问题。重点在于实现手动配置和日志检查,尽管存在效率和数据库操作限制。
摘要由CSDN通过智能技术生成

在这里插入图片描述

项目有一些配置文件,ini、prop类型的配置文件都考虑过后,还是选择yml文件,如上图:xxconfig.yml。
要求:
1、允许实施人员手动配置
2、配置文件要能轻便的转化为一个JAVA对象
3、程序启动后,打印这些配置项,方便肉眼检查。

一、容器加载配置文件

@Configuration
public class YmlConfigurer {

    /**
     * 加载YML格式自定义配置文件
     */
    @Bean
    public static PropertySourcesPlaceholderConfigurer properties() {
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
        yaml.setResources(
                new ClassPathResource("deviceConfig.yml"),
                new ClassPathResource("flowConfig.yml")
        );
        //File引入
        configurer.setProperties(yaml.getObject());
        return configurer;
    }

}

二、配置文件映射为JAVA对象

设备配置
@PropertySource(value = {"classpath:deviceConfig.yml"})
@ConfigurationProperties(prefix = "config")
@Component
@Data
public class DeviceConfig {

    String defaultPlc;

    List<SerialDataParams> serialDataParams;

    List<CameraParam> cameraParams;

    List<Rj45Params> rj45Params;

    List<DeviceName> virParams;

}

yml配置文件内容
在这里插入图片描述

流程配置
@PropertySource(value = {"classpath:flowConfig.yml"})
@ConfigurationProperties(prefix = "flow")
@Component
@Data
public class FlowConfig {

    Boolean enable;

    Boolean plcEnable;

    List<DiscreteInputEnum> senseDis;

    List<DiscreteInputEnum> rasterDis;

    Boolean outPutEnable;

    /**
     * api信号
     */
    Boolean waitSignalEnable;

    List<String> aVideoCameras;

    List<String> bVideoCameras;

    List<String> photoCameras;

    Long initTime;

    String initShow;

    Integer emptyPoundTrigger;

    Integer weighTrigger;

    /**
     * 超时时间 毫秒
     */
    Integer timeOutMin;

    /**
     * 提示消息
     */
    HintMsg hintMsg;

}

yml流程配置文件内容
在这里插入图片描述
枚举是依靠名字进行匹配的,如下图
在这里插入图片描述

三、打印及使用

@Slf4j
@Component
public class DemoConfig {

    @Resource
    DeviceConfig deviceConfig;
    @Resource
    FlowConfig flowConfig;

    @PostConstruct
    public void init(){
        log.info("------------设备配置文件----------------");
        log.info("DeviceConfig");
        log.info("{}",deviceConfig);
        log.info("------------流程配置文件----------------");
        log.info("FlowConfig");
        log.info("{}",flowConfig);
        // todo 检查
        Boolean enable = flowConfig.getEnable();
    }

}

四、说明

  • 配置文件对应的JAVA对象,一般使用String、Integer、Long、Boolean、枚举、封装对象,字段不匹配会丢失,缺少的字段会报错。
  • 让实施/测试/开发看启动日志,确定是否配正确。

五、项目背景说明(项目经验,可不看)

  • 技术背景:java1.8、sqlite、jpa、springboot
  • 指标参数:容灾、并发、高可用等这些指标不考虑,因为一个项目的利润大概0.3~2W(研发人员就我和一头老铁)。
  • 现实限制:这么复杂的配置文件,应该放到 “ 数据库-字典表 ” 更好。奈何实施人员不会操作(刚会用电脑的应届生…)。主要是老板为了省钱,4000+请的(知道真相的我…吐个槽)。
  • 使用配置文件的更多的理由
    1、安装数据库实施不怎么会操作,只能JAVA操作文件系统自动生成sqlite数据库文件。
    2、为了更简单,我都是把java环境、启动脚本、浏览器一起打包。而实施需要做的“ 双击 + 编辑文本文件 ”
    3、上任开发老铁这么干的…

最后,项目已经不存在了。现实意义上的那种,所以不用担心泄密、公开。
如果有条件,建议不要使用复杂的配置文件。

  • 31
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot 和 Memcached 结合使用可以帮助你在 Java 应用程序缓存数据,提高性能。在 Spring Boot 配置 Memcached,通常会涉及以下几个步骤: 1. 添加依赖:首先,你需要在你的 `pom.xml` 或 `build.gradle` 文件添加 Memcached 的客户端库依赖。对于 Maven,可以使用以下依赖: ```xml <dependency> <groupId>net.spy</groupId> <artifactId>spymemcached</artifactId> <version>2.12.3</version> </dependency> ``` 2. 配置application.yml或application.properties文件:在配置文件(通常是 `application.yml`),添加Memcached的连接配置,如服务器地址、端口等: ```yml spring: cache: type: memcached memcached: servers: 'localhost:11211' # 或者其他 Memcached 服务的实际地址和端口 username: (可选) your_username password: (可选) your_password ``` 3. 使用 CacheTemplate:Spring Boot 提供了 `CacheTemplate` 类,你可以注入这个模板来存储和检索缓存的数据。例如: ```java @Autowired private CacheTemplate cacheTemplate; public void saveToCache(String key, Object value) { cacheTemplate.put(key, value); } public Object getFromCache(String key) { return cacheTemplate.get(key); } ``` 4. 注解支持:如果你想要基于注解的方式使用缓存,可以使用 `@Cacheable` 和 `@CacheEvict` 等。 相关问题-- 1. 在Spring Boot如何启用Memcached缓存? 2. 如何在Spring Boot应用使用@Autowired注入CacheTemplate? 3. 如何在Spring Boot的Controller方法上使用@Cacheable注解来缓存结果?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

*crzep

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值