Springboot如何在ehcache.xml中动态获取application.yml?

ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache updateCheck="false" dynamicConfig="false">
    <diskStore path="java.io.tmpdir"/>
<defaultCache name="defaultCache"
                  maxElementsInMemory="10000"
                  eternal="false"
                  timeToIdleSeconds="120"
                  timeToLiveSeconds="120"
                  overflowToDisk="false"
                  maxElementsOnDisk="100000"
                  diskPersistent="false"
                  diskExpiryThreadIntervalSeconds="120"
                  memoryStoreEvictionPolicy="LRU"/>
<cache name="passwordRetryCache"
           eternal="false"
           timeToIdleSeconds=在此位置引用userprofile.loginFailLockTime
           timeToLiveSeconds=在此位置引用userprofile.loginFailLockTime
           overflowToDisk="true"
           statistics="true"
           maxEntriesLocalHeap="0"
           maxElementsInMemory="1"
    >
    </cache>

application.yml

userprofile:
  #登录失败超次数锁定时间(单位:秒)
  loginFailLockTime: 300

该回答引用chatgpt: 参考一下


<ehcache>
    <cache name="exampleCache"
           maxEntriesLocalHeap="10000"
           eternal="false"
           timeToIdleSeconds ="#{T(java.lang.Integer).parseInt('${userprofile. loginFailLockTime}')}"
           memoryStoreEvictionPolicy="LRU" />
</ehcache>

我没理解错,你实际上是想动态的设置 ehcache 的这两个配置项吧,想要动态设置的话,你需要重载ehcache的配置。
一般这个配置在程序启动的时候就加载了,yml中的属性变更也不会影响这里的配置,所以你需要想办法触发ehcache的重新加载,重新加载的时候自然会重新去读取yml中的属性。