Monday, March 8, 2010

External configuration with Seam

Sometimes you want to load environment-specific configuration properties from a file located in the file system. With Spring this is very easy as all you need to do is to use a property placeholder

<context:property-placeholder location="file:/opt/etc/"/>

Seam does not have an out-of-the-box component to do that but you can write your own

public class Environment {
    private Properties properties;

    public void setup() {
        try {
            properties = new Properties();
            properties.load(new FileInputStream("/opt/etc/"));
        } catch (IOException e) {
            throw new RuntimeException("Unable to load", e);

    public String getProperty(String name) {
        return properties.getProperty(name);

Then you can use this component anywhere in your application. For example:

<?xml version="1.0" encoding="UTF-8"?>
<components xmlns=""

    <core:init transaction-management-enabled="false"/>


    <mail:mail-session session-jndi-name="mail/pdfGeneratorSession"/>

    <component name="ftpHandler">
        <property name="ftpHost">#{environment.getProperty('ftpHost')}</property>
        <property name="ftpPort">#{environment.getProperty('ftpPort')}</property>
        <property name="ftpUser">#{environment.getProperty('ftpUser')}</property>
        <property name="ftpPassw">#{environment.getProperty('ftpPassw')}</property>


No comments:

Post a Comment