Spring: insert Scala constants

Simple internal constants that should not be externalized for properties:

object InternalConstant { val CONSTANT_ONE: Byte = 21 val CONSTANT_TWO: Byte = 42 } 

Introducing them in the same way as with Java:

 <bean id="daBean" class="my.package.DaClass"> <constructor-arg> <util:constant static-field="my.package.InternalConstant.CONSTANT_TWO"/> </constructor-arg> </bean> 

getting java.lang.NoSuchFieldException: CONSTANT_TWO

(package path is correct)

+4
source share
1 answer

This is because behind the scenes, InternalConstant compiled into a class with the 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()}"/> 

Did not check though.

+6
source

All Articles