java - How to reference a bean's property inside servlet-context? -


i new java , working on spring mvc project. have servlet-context store information going need @ runtime. have set of urls important on system , based on base url defined on servlet context, this:

<bean id="applicationconfiguration"         class="example.applicationconfig">         <property name="baseurl" value="http://localhost:8080/"></property>     </bean> 

now, have second bean in reference baseurl:

   <bean id="menu"             class="example.menu">             <property name="entry1name" value="entry1"></property>             <property name="entry1value" value="<baseurl>"></property>             <property name="entry2name" value="entry2"></property>             <property name="entry2value" value="<baseurl>/page2"></property>         </bean> 

the second bean little bit simpler real one, focus on problem.

i reference baseurl property, searching , trying things like

${applicationconfiguration.baseurl}  @applicationconfiguration.baseurl 

using

<property name="entry1value" ref="applicationconfiguration.baseurl"></property> 

and on.

the first 2 used string, not "resolved". second 1 not work because .baseurl not bean.

is there way can reference property use value?

you have define baseurl bean in case

<bean id="baseurl" class="java.lang.string">   <constructor-arg value="http://localhost:8080/"/> </bean> 

and use reference

for example

<property name="baseurl" ref="baseurl/"> 

in particular example more suitable use propertyplaceholderconfigurer instead of defining separate bean per property

<bean class="org.springframework.beans.factory.config.propertyplaceholderconfigurer">      <property name="location">         <value>yourapp.properties</value>     </property> </bean> 

and refer properties directly in application context property key name example

<property name="baseurl" value="${baseurl}" /> 

provided yourapp.properties placed on root of classpath , has following property

baseurl=somevalue 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jquery - Keeping Kendo Datepicker in min/max range -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -