Skip to main content
Chapter 13 of 18 · 2 sections

Spring: Property Sources

Load properties from configuration files and inject values into Spring-managed beans.

Original chapter: Property Source. The text and examples below are preserved from the historical source; read the corrections above before using them.

Section 13.1: Sample xml configuration using PropertyPlaceholderConfigurer

Source example · PDF page 55
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="locations">
    <list>
          <value>classpath:ReleaseBundle.properties</value>
    </list>
</bean>

Section 13.2: Annotation

Sample property file : nexus.properties

Sample property file content:

Source example · PDF page 55
nexus.user=admin
nexus.pass=admin
nexus.rest.uri=http://xxx.xxx.xxx.xxx:xxxx/nexus/service/local/artifact/maven/content

Sample Context File xml configuration

Source example · PDF page 55
<context:property-placeholder location="classpath:ReleaseBundle.properties" />

Sample Property Bean using annotations

Source example · PDF page 55
@Component
@PropertySource(value = { "classpath:nexus.properties" })
public class NexusBean {

   @Value("${" + NexusConstants.NEXUS_USER + "}")
   private String user;

   @Value("${" + NexusConstants.NEXUS_PASS + "}")
   private String pass;

   @Value("${" + NexusConstants.NEXUS_REST_URI + "}")
   private String restUri;
}

Sample Constant class

Source example · PDF page 55
public class NexusConstants {
   public static final String NexusConstants.NEXUS_USER="";
   public static final String NexusConstants.NEXUS_PASS="";
   public static final String NexusConstants.NEXUS_REST_URI="";
}
Buy me a coffee
Daily Practice
0/0