java 创建 Spring 枚举 bean 并传递方法调用的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10702693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:10:57  来源:igfitidea点击:

Creating a Spring enum bean and passing the value of a method call

javaspringenums

提问by DarVar

I have this Singleton:

我有这个单身人士:

   public enum Elvis {
       INSTANCE;
       private int age;

       public int getAge() {
           return age;
       }
   }

I know how to create the enum bean in spring:

我知道如何在 spring 中创建 enum bean:

   <bean id="elvis" class="com.xyz.Elvis" factory-method="valueOf">
           <constructor-arg>
               <value>INSTANCE</value>
           </constructor-arg>
   </bean> 

How do I pass the int returned by INSTANCE.getAge() into another beans constructor?

如何将 INSTANCE.getAge() 返回的 int 传递给另一个 bean 构造函数?

采纳答案by axtavt

You can use Spring Expression Language:

您可以使用Spring 表达式语言

<constructor-arg value = "#{elvis.age}" />

or without elvisbean:

或不加elvis豆:

<constructor-arg value = "#{T(com.xyz.Elvis).INSTANCE.age}" />