• <legend id='0u2As'><style id='0u2As'><dir id='0u2As'><q id='0u2As'></q></dir></style></legend>

      <tfoot id='0u2As'></tfoot>
    1. <small id='0u2As'></small><noframes id='0u2As'>

    2. <i id='0u2As'><tr id='0u2As'><dt id='0u2As'><q id='0u2As'><span id='0u2As'><b id='0u2As'><form id='0u2As'><ins id='0u2As'></ins><ul id='0u2As'></ul><sub id='0u2As'></sub></form><legend id='0u2As'></legend><bdo id='0u2As'><pre id='0u2As'><center id='0u2As'></center></pre></bdo></b><th id='0u2As'></th></span></q></dt></tr></i><div id='0u2As'><tfoot id='0u2As'></tfoot><dl id='0u2As'><fieldset id='0u2As'></fieldset></dl></div>
      • <bdo id='0u2As'></bdo><ul id='0u2As'></ul>

        将外部化值注入 Spring 注解

        时间:2023-09-28
        <legend id='GnNk3'><style id='GnNk3'><dir id='GnNk3'><q id='GnNk3'></q></dir></style></legend>
            <tbody id='GnNk3'></tbody>

              <i id='GnNk3'><tr id='GnNk3'><dt id='GnNk3'><q id='GnNk3'><span id='GnNk3'><b id='GnNk3'><form id='GnNk3'><ins id='GnNk3'></ins><ul id='GnNk3'></ul><sub id='GnNk3'></sub></form><legend id='GnNk3'></legend><bdo id='GnNk3'><pre id='GnNk3'><center id='GnNk3'></center></pre></bdo></b><th id='GnNk3'></th></span></q></dt></tr></i><div id='GnNk3'><tfoot id='GnNk3'></tfoot><dl id='GnNk3'><fieldset id='GnNk3'></fieldset></dl></div>
              <tfoot id='GnNk3'></tfoot>
              • <bdo id='GnNk3'></bdo><ul id='GnNk3'></ul>
                1. <small id='GnNk3'></small><noframes id='GnNk3'>

                2. 本文介绍了将外部化值注入 Spring 注解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我一直在考虑在编译时评估注释值的 Java 特性,它似乎确实很难将注释值外部化.

                  I've been thinking around the Java feature that evaluates annotation values in compile-time and it seems to really make difficult externalizing annotation values.

                  但是,我不确定这是否真的不可能,所以我很感激任何建议或明确的答案.

                  However, I am unsure whether it is actually impossible, so I'd appreciate any suggestions or definitive answers on this.

                  更重要的是,我正在尝试外部化一个注释值,该值控制 Spring 中计划的方法调用之间的延迟,例如:

                  More to the point, I am trying to externalize an annotation value which controls delays between scheduled method calls in Spring, e.g.:

                  public class SomeClass {
                  
                      private Properties props;
                      private static final long delay = 0;
                  
                      @PostConstruct
                      public void initializeBean() {
                          Resource resource = new ClassPathResource("scheduling.properties");
                          props = PropertiesLoaderUtils.loadProperties(resource);
                          delay = props.getProperties("delayValue");
                      }
                  
                      @Scheduled(fixedDelay = delay)
                      public void someMethod(){
                          // perform something
                      }
                  }
                  

                  假设 scheduling.properties 在类路径中,并且包含属性键 delayValue 及其对应的 long 值.

                  Suppose that scheduling.properties is on classpath and contains property key delayValue along with its corresponding long value.

                  现在,这段代码有明显的编译错误,因为我们试图为 final 变量赋值,但这是强制性的,因为我们不能将变量赋值给注释值,除非它是静态最终.

                  Now, this code has obvious compilation errors since we're trying to assign a value to final variable, but that is mandatory, since we can't assign the variable to annotation value, unless it is static final.

                  有没有办法解决这个问题?我一直在思考 Spring 的自定义注解,但根本问题仍然存在——如何将外部化的值分配给注解?

                  Is there any way of getting around this? I've been thinking about Spring's custom annotations, but the root issue remains - how to assign the externalized value to annotation?

                  欢迎提出任何想法.

                  一个小更新 - 对于这个例子来说,Quartz 集成是多余的.我们只需要以亚分钟级的分辨率定期执行,仅此而已.

                  A small update - Quartz integration is overkill for this example. We just need a periodic execution with sub-minute resolution and that's all.

                  推荐答案

                  Spring v3.2.2 中的 @Scheduled 注解在原来的 3 个 long 参数中添加了 String 参数来处理这个问题.fixedDelayStringfixedRateStringinitialDelayString 现在也可用:

                  The @Scheduled annotation in Spring v3.2.2 has added String parameters to the original 3 long parameters to handle this. fixedDelayString, fixedRateString and initialDelayString are now available too:

                   @Scheduled(fixedDelayString = "${my.delay.property}")
                   public void someMethod(){
                          // perform something
                   }
                  

                  这篇关于将外部化值注入 Spring 注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:注释 vs XML,优点和缺点 下一篇:如何在 Spring 中重新启动死锁/锁定超时事务?

                  相关文章

                  最新文章

                  <i id='6BxdF'><tr id='6BxdF'><dt id='6BxdF'><q id='6BxdF'><span id='6BxdF'><b id='6BxdF'><form id='6BxdF'><ins id='6BxdF'></ins><ul id='6BxdF'></ul><sub id='6BxdF'></sub></form><legend id='6BxdF'></legend><bdo id='6BxdF'><pre id='6BxdF'><center id='6BxdF'></center></pre></bdo></b><th id='6BxdF'></th></span></q></dt></tr></i><div id='6BxdF'><tfoot id='6BxdF'></tfoot><dl id='6BxdF'><fieldset id='6BxdF'></fieldset></dl></div>
                  1. <legend id='6BxdF'><style id='6BxdF'><dir id='6BxdF'><q id='6BxdF'></q></dir></style></legend>
                    • <bdo id='6BxdF'></bdo><ul id='6BxdF'></ul>

                    1. <tfoot id='6BxdF'></tfoot>

                      <small id='6BxdF'></small><noframes id='6BxdF'>