Spring framework umožňuje jednoduše načítat hodnoty z properties souboru a to pomocí anotace @Value. Nejjednodušší je vše ukázat na příkladu.
Struktura projektu
│ pom.xml │ ├───src │ ├───main │ │ └───java │ │ └───cz │ │ └───vitfo │ │ └───spring │ │ App.java │ │ MyClass.java │ │ │ ├───resource │ │ app.properties │ │ beans.xml
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cz.vitfo</groupId>
<artifactId>spring01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring01</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-version>4.3.5.RELEASE</spring-version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-version}</version>
</dependency>
</dependencies>
</project>
Soubor app.properties
my.string=Hello my.int=123
Soubor se Spring konfigurací beans.xml
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="cz.vitfo.spring"/> <context:property-placeholder location="classpath:app.properties"/> <bean class="cz.vitfo.spring.MyClass"/> </beans>
Element <context:property-placeholder location="classpath:app.properties"/> určuje, kde se nachází properties soubor s hodnotami. Element <context:component-scan base-package="cz.vitfo.spring"/> nastavuje skenování balíčku cz.vitfo.spring. Pokud v souborech v tomto balíčku Spring při procházení narazí na anotaci @Value, pokusí se do takto anotované proměnné dosadit hodnotu ze souboru app.properties.
Třída MyClass.java
import org.springframework.beans.factory.annotation.Value;
public class MyClass {
@Value("${my.string}")
private String myString;
@Value("${my.int}")
private int myInt;
public String getMyString() {
return myString;
}
public void setMyString(String myString) {
this.myString = myString;
}
public int getMyInt() {
return myInt;
}
public void setMyInt(int myInt) {
this.myInt = myInt;
}
}
Anotace @Value používá pro určení dosazované hodnoty identifikátor ${...}.
Třída s metodou main.
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main( String[] args ) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
MyClass bean = context.getBean(MyClass.class);
System.out.println(bean.getMyString());
System.out.println(bean.getMyInt());
context.close();
}
}
Nejdříve se vytvoří Spring kontext načtením konfiguračního souboru beans.xml. Následně se vytvoří instance (beana) třídy MyClass a zavolají se (a zobrazí) hodnoty proměnných myString a myInt. Po skončení se kontext zavře.
Výsledek spuštění.
INFO: Loading XML bean definitions from class path resource [beans.xml] INFO: Loading properties file from class path resource [app.properties] Hello 123
Pokud byste zakomentovali <context:component-scan base-package="cz.vitfo.spring"/> v beans.xml, výsledek by byl následující.
null 0
Spring by totiž neprošel balíček cz.vitfo.spring a tím pádem by ani nenasetoval hodnoty do proměnných myString a myInt.