简体   繁体   中英

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

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:

   <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?

You can use Spring Expression Language :

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

or without elvis bean:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM