Tag

how to use properties in spring

Browsing

After a decade of setting property values in Spring XML configuration and calling setter methods on bean instances, Spring Boot provides an easier way for setting property values via configuration files (properties file and yaml file).

Spring provides the @PropertySource annotation to specify the list of configuration files and Spring Boot takes it one step further by automatically registering a PropertyPlaceHolderConfigurer bean using the aplication.properties file in the root classpath by default. You can also create profile specific configuration files using the filename as application-{profile}.properties

For example, you can have application.properties, which contains the default properties values, application-dev.properties, which contains the dev profile configuration, and application-prod.properties, which contains the production profile configuration values. If you want to configure properties that are common for all the profiles, you can configure them in application-default.properties.

After create properties files, we need to set the created profile for the beans or app config.

@Bean
@Profile("!prod")
public CommandLineRunner dataLoader(IngredientRepository repo,
UserRepository userRepo, PasswordEncoder encoder) {
...
}

Here, the exclamation mark (!) negates the profile name. Effectively, it states that the CommandLineRunner bean will be created if the profile prod is not active.

It is also possible to use @Profile on an entire @Configuration annotated class. In the following example, the bean will only be created if both profiles prod, qa profiles aren’t active

@Profile({"!prod", "!qa"})
@Configuration
public class DevelopmentConfig {
@Bean
public CommandLineRunner dataLoader(IngredientRepository repo,
UserRepository userRepo, PasswordEncoder encoder) {
...
}
}

Activating profiles

Spring handle a list of active profile names in spring.properties.active property.

spring:
 profiles:
  active:
   - prod

But that is perhaps the worst possible way to set an active profile. Instead, you should set the profile via the environment variable SPRING_PROFILES_ACTIVE like this

% export SPRING_PROFILES_ACTIVE=prod

How to binding Configuration Properties value to properties

Spring provides the @Value annotation to bind any property value to a bean property. Suppose you had the
following application.properties file:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/testdatabase
jdbc.username=root
jdbc.password=pass

You can bind these property values into bean properties using @Value as follows:

@Configuration
public class AppConfig
{
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
...........
}

Or you can also simply annotate DataSourceConfig with @ConfigurationProperties(prefix=”jdbc”) to
automatically bind the properties that start with jdbc.*.

@Component
@ConfigurationProperties(prefix="jdbc")
public class AppConfig
{
private String driver;
private String url;
private String username;
private String password;
...........
}

By using prefix value in @ConfigurationProperties, Spring Boot will get value in properties file automatically based on property name in your bean without specifying @value annotation.

Using special property values in Spring Boot

When setting properties, you are not limited to declaring their values as hard-coded and String numeric values. Instead, you can derive their values from other configuration properties, for example

greeting:
  welcome: You are using ${spring.application.name}.