开发者

Spring: Injecting a Scala Constant

开发者 https://www.devze.com 2023-04-12 14:43 出处:网络
Simple internal constants, that should not be externalized to properties: object InternalConstant { val CONSTANT_ONE: Byte = 21

Simple internal constants, that should not be externalized to properties:

object InternalConstant {

  val CONSTANT_ONE: Byte = 21
  val CONSTANT_TWO: Byte = 42

}

Injecting them as I would with Java:

<bean id="daBean" class="my.package.DaClass">
    <constructor-arg>
        <util:开发者_开发知识库constant static-field="my.package.InternalConstant.CONSTANT_TWO"/>
    </constructor-arg>
</bean>

getting a java.lang.NoSuchFieldException: CONSTANT_TWO

( the package path is correct )


That's because behind the scenes InternalConstant is compiled into a class with static CONSTANT_ONE() method returning 21, not a field. And calling static methods is possible in Spring with SpEL:

<constructor-arg value="#{T(my.package.InternalConstant).CONSTANT_TWO()}"/>

Haven't tested it though.

0

精彩评论

暂无评论...
验证码 换一张
取 消