Andre
Andre

Reputation: 661

Using parameter in .xml file?

In a .java file I can use "getProperty(PARAMETER_NAME)"

To get there value of a parameter

I have this block of code in a .xml

<bean class="org.springframework.security.ui.cas.ServiceProperties"
          id="authenticationServiceProperties">
        <property name="service">
            <value>http://v-repte-lnx.nwc.ac.za:8024/jasperserver-pro/j_spring_cas_security_check</value>
        </property>
        <property name="sendRenew">
            <value>false</value>
        </property>
</bean>

What I want to do is to not have the link (4th line) hardcoded

So the 4th line should look something like this

<value>getProperty(PARAMETER_NAME)</value>

What can I use in this .xml file to achieve this ?

EXTRA:

I am using JasperReports Server 5.0.1

My tree looks something like this

Webap>
  applicationContext-security.xml
  internal>
      jasperreports.properties

EDIT:

I implemented user2550754's solution but can't get it to work

See comment in user2550754's post

UPDATE ON FILES NOW:

applicationContext-security.xml file

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="../WEB-INF/internal/jasperserver-pro.properties"/>
</bean>

<bean class="org.springframework.security.ui.cas.ServiceProperties"
          id="authenticationServiceProperties">
        <property name="service">
            <value>${SERVICE_URL}</value>
        </property>
        <property name="sendRenew">
            <value>false</value>
        </property>
</bean>

jasperserver-pro.properties file

SERVICE_URL=http://b-reptes-lnx1.nuw.ac.za:8024/jasperserver-pro/j_spring_cas_security_check

Upvotes: 2

Views: 15591

Answers (3)

Maksym Demidas
Maksym Demidas

Reputation: 7817

In last versions of Spring you can load properties in one line using properties tag from util namespace:

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"   
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:util="http://www.springframework.org/schema/util"  
    xsi:schemaLocation="  
        http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> 

    <util:properties id="appProps" location="classpath:app.properties" />

and use them using ${key} syntax in xml file:

<bean id="service" class="com.mycompany.Service">
    <property name="someParameter" value="${someParameterKey}"/>
</bean>

or in annotation:

@Value("${someParameterKey}")
private String someParameter;

Upvotes: 2

user2550754
user2550754

Reputation: 905

Store your configuration in a properties file, say application.properties

url=http://v-repte-lnx.nwc.ac.za:8024/jasperserver-pro/j_spring_cas_security_check

Then, add in your .xml as follows

<bean id="applicationProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" lazy-init="default">
    <property name="location" value="classpath:application.properties"/>
</bean>

And configure your code like this

<bean class="org.springframework.security.ui.cas.ServiceProperties"
      id="authenticationServiceProperties">
    <property name="service">
        <value>${url}</value>
    </property>
    <property name="sendRenew">
        <value>false</value>
    </property>
</bean>

Upvotes: 2

Luca Basso Ricci
Luca Basso Ricci

Reputation: 18403

Use Spring property placeholder capability:
1. externalize configuration file,
2. load with org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
3. replace <value>${x.y.z}</value>

Upvotes: 1

Related Questions