/java/

Spring. Read values from properties file

2015-02-25 00:05:44

In this example you will learn how to read properties form properties file in nice way.

We want to read this properties in java class.

./src/main/resources/ftp.properties [text] ftp.host = https://btbw.test.local ftp.user = michal ftp.password = 123 [/text]

For that we have to create new bean "FTPConfiguration" and use @Value with correct keys.

FTPConfiguration.java

package pl.btbw;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("ftpConfiguration")
public class FTPConfiguration {

    private final static Logger LOG = Logger.getLogger(FTPConfiguration.class);

    @Value("${ftp.host}")
    private String host;

    @Value("${ftp.user}")
    private String user;

    @Value("${ftp.password}")
    private String password;

    public FTPConfiguration() {
        LOG.info("initialization");
    }

    public String getHost() {
        return host;
    }

    public String getUser() {
        return user;
    }

    public String getPassword() {
        return password;
    }
}

Of course we have to inform Spring, that we want use properties file.

./src/main/resources/application-context.xml

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>

<beans beans="" context="" http:="" schema="" spring-beans.xsd="" spring-context-4.0.xsd&quot;="" www.springframework.org="" xmlns='"http://www.springframework.org/schema/beans"' xmlns:context='"http://www.springframework.org/schema/context"' xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance"' xsi:schemalocation='"http://www.springframework.org/schema/beans'>
<context:property-placeholder location='"classpath:ftp.properties"/'>
<context:component-scan base-package='"pl.btbw"/'>
</context:component-scan></context:property-placeholder></beans>

Test

Application.java

package pl.btbw;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Application {

    public static void main(String[] args) throws InterruptedException {

        ApplicationContext context =
                new ClassPathXmlApplicationContext("application-context.xml");

        FTPConfiguration ftpConfiguration = context.getBean("ftpConfiguration", FTPConfiguration.class);

        System.out.println(ftpConfiguration.getHost());
        System.out.println(ftpConfiguration.getUser());
        System.out.println(ftpConfiguration.getPassword());
    }
}

pom.xml

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>

<project http:="" maven-4.0.0.xsd&quot;="" maven.apache.org="" xmlns='"http://maven.apache.org/POM/4.0.0"' xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance"' xsd="" xsi:schemalocation='"http://maven.apache.org/POM/4.0.0'>
<modelversion>4.0.0</modelversion>
<groupid>pl.btbw</groupid>
<artifactid>spring-bean-demo</artifactid>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-core</artifactid>
<version>4.0.4.RELEASE</version>
</dependency>
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-context</artifactid>
<version>4.0.4.RELEASE</version>
</dependency>
<dependency>
<groupid>commons-logging</groupid>
<artifactid>commons-logging</artifactid>
<version>1.1.3</version>
</dependency>
<dependency>
<groupid>log4j</groupid>
<artifactid>log4j</artifactid>
<version>1.2.17</version>
</dependency>
</dependencies>
</project>