简体   繁体   中英

Populate Spring “property” from Java code

I have a Java class MySettings which loads the properties for my application from a JSON file / database / something else.

I have configured MySettings to be a bean in the Spring context.

<bean id="mySettings" class="com.myapp.MySettings" />

I now want to use one of the values in mySettings in the spring context file for something that doesn't support SpEL, like the Spring Integration logging channel adapter, eg

<int:logging-channel-adapter id="logMessageToRemote" 
     logger-name="toRemote" level="#{mySettings.getRemoteLogLevel}"
     expression="payload" />

Is there a way I can setup a "property" that can be populated using SpEL?

from spring docs http://static.springsource.org/spring/docs/3.0.5.RELEASE/reference/expressions.html#expressions-beandef

You can also refer to other bean properties by name, for example.

<bean id="numberGuess" class="org.spring.samples.NumberGuess">
   <property name="randomNumber" value="#{ T(java.lang.Math).random() * 100.0 }"/>
   <!-- other properties -->
</bean>


<bean id="shapeGuess" class="org.spring.samples.ShapeGuess">
  <property name="initialShapeSeed" value="#{ numberGuess.randomNumber }"/>
   <!-- other properties -->
</bean>

Is this something you are looking for?

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