Reputation: 4747
I am new to Java and I am working on a Spring MVC project. we have a servlet-context where we store information we are going to need at runtime. I have a set of URLs that are important on my system and they are all based on a base URL that is also defined on the servlet context, like this:
<bean id="applicationConfiguration"
class="example.ApplicationConfig">
<property name="baseUrl" value="http://localhost:8080/"></property>
</bean>
Now, I have a second bean in which I would like to reference the 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 is a little bit simpler than the real one, to focus on the problem.
I would like to reference the baseUrl property, so I am searching and trying things like
${applicationConfiguration.baseUrl}
@applicationConfiguration.baseUrl
using
<property name="entry1value" ref="applicationConfiguration.baseUrl"></property>
and so on.
The first two are used as string, not "resolved". The second one does not work because .baseUrl is not a bean.
Is there any way that I can reference the property to use it as a value?
Upvotes: 1
Views: 548
Reputation: 240870
You would have to define baseUrl as another bean in that case
<bean id="baseUrl" class="java.lang.String">
<constructor-arg value="http://localhost:8080/"/>
</bean>
and then use it reference
for example
<property name="baseUrl" ref="baseUrl/">
in your particular example it is more suitable to 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 then refer properties directly in application context by property key name for example
<property name="baseurl" value="${baseurl}" />
provided that your yourApp.properties
is placed on root of classpath and it has a following property
baseurl=somevalue
Upvotes: 1