springboot监听文件笔记

本文介绍了如何在SpringBoot项目中监听外部配置文件,包括创建监听器类、配置监视器以及启动类的设置,旨在实现根据文件内容动态调整项目参数。同时,文章讨论了如何动态获取配置文件的绝对路径,避免硬编码。
摘要由CSDN通过智能技术生成

最近公司项目需要对外部配置文件进行监听,根据文件内容进行相应的修改。所以就学习了下

一、监听器类

@Component
public class FileListener extends FileAlterationListenerAdaptor {
   
	
	private Logger log = LoggerFactory.getLogger(FileListener.class);
	 // 声明业务服务
	@Autowired
	Service sevice;
	
    // 文件创建执行
    @Override
    public void onFileCreate(File file) {
   
        log.info("[新建]:" + file.getAbsolutePath());
        
        System.out.println("文件创建了。。。。。");
    }
 
    // 文件创建修改
    @Override
    public void onFileChange(File file) {
   
        log.info("[修改]:" + file.getAbsolutePath());

    }
 
    // 文件创建删除
    @Override
    public void onFileDelete(File file) {
   
        log
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Spring Boot 中监听 MySQL 的增删改日志文件,可以使用 MySQL 的 binlog,binlog 是 MySQL 的二进制日志,记录了 MySQL 的所有更新操作,包括增删改等。 下面是使用 Spring Boot 监听 MySQL binlog 的步骤: 1. 在 MySQL 配置文件中开启 binlog,可以在 my.cnf 或 my.ini 文件中添加如下配置: ``` [mysqld] log-bin=mysql-bin binlog-format=ROW ``` 这里将 binlog 日志文件存储在名为 mysql-bin 的文件中,格式为 ROW。 2. 在 Spring Boot 中添加 MySQL 驱动和 binlog 相关的依赖,例如: ``` <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> <dependency> <groupId>com.github.shyiko</groupId> <artifactId>mysql-binlog-connector-java</artifactId> <version>0.17.0</version> </dependency> ``` 这里使用了 mysql-connector-java 和 mysql-binlog-connector-java 两个依赖。 3. 在 Spring Boot 中编写监听 binlog 日志的代码,例如: ``` @Component public class BinlogListener { @Autowired private DataSource dataSource; @PostConstruct public void init() { BinaryLogClient client = new BinaryLogClient("localhost", 3306, "root", "password"); client.setServerId(1); client.setBinlogFilename("mysql-bin.000001"); client.registerEventListener(new BinaryLogClient.EventListener() { @Override public void onEvent(Event event) { EventData data = event.getData(); if (data instanceof WriteRowsEventData) { WriteRowsEventData write = (WriteRowsEventData) data; System.out.println("inserted rows: " + write.getRows()); } else if (data instanceof UpdateRowsEventData) { UpdateRowsEventData update = (UpdateRowsEventData) data; System.out.println("updated rows: " + update.getRows()); } else if (data instanceof DeleteRowsEventData) { DeleteRowsEventData delete = (DeleteRowsEventData) data; System.out.println("deleted rows: " + delete.getRows()); } } }); try { client.connect(); } catch (IOException e) { e.printStackTrace(); } } } ``` 这里使用了 BinaryLogClient 类来连接 MySQL,通过 setServerId 和 setBinlogFilename 方法设置 binlog 相关参数,通过 registerEventListener 方法注册事件监听器来监听 binlog 日志的写入、更新、删除操作。 需要注意的是,直接监听 MySQL 的 binlog 日志文件可能会对性能和稳定性造成影响,建议在使用前先进行充分测试和评估。同时,也建议使用专业的数据库同步工具来进行 MySQL 数据库的同步,如阿里云的 DTS、腾讯云的 CDC 等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值