Are there any significant differences between injecting fields of a class with @Value annotation and looking them up using the Spring Environment API? Is one preferable over the other (and under what circumstances)?
Example Using @Value:
class Config {
@Value("${db.driverClassName}")
private String driverClassName;
@Value("${db.url}")
private String url;
@Value("${db.username}")
private String username;
@Value("${db.password}")
private String password;
@Bean
public javax.sql.DataSource dataSource(){
PoolProperties poolProperties = new PoolProperties();
poolProperties.setDriverClassName(driverClassName);
poolProperties.setUrl(url);
poolProperties.setUsername(username);
poolProperties.setPassword(password);
return new org.apache.tomcat.jdbc.pool.DataSource(poolProperties);
}
}
Example Using Environment API:
class Config {
@Autowired
private Environment environment;
@Bean
public javax.sql.DataSource dataSource(){
PoolProperties poolProperties = new PoolProperties();
poolProperties.setDriverClassName(environment.getProperty("db.driverClassName"));
poolProperties.setUrl(environment.getProperty("db.url"));
poolProperties.setUsername(environment.getProperty("db.username"));
poolProperties.setPassword(environment.getProperty("db.password"));
return new org.apache.tomcat.jdbc.pool.DataSource(poolProperties);
}
}
Spring @Value annotation is used to assign default values to variables and method arguments.
@Value spring boot annotation is used to inject values from the properties file into the java variables that have been configured. The @Value annotation in spring boot is used to assign values to variables and method parameters from properties files or system environments.
One of the most important annotations in spring is @Value annotation which is used to assign default values to variables and method arguments. We can read spring environment variables as well as system variables using @Value annotation. It also supports Spring Expression Language (SpEL).
No, this is not (directly) possible. The default value of an annotation property must be a compile-time constant.
The Environment
is a combination of profiles and properties.
A profile is a named, logical group of bean definitions that can be active or inactive based on your environment. Beans may be assigned to a profile whether defined in XML or via annotations. For ex. you may have one profile for development mode and another for production mode. You can look up the docs for @Profile
here to see more details about it.
To quote the Environment
docs:
The role of the Environment object with relation to profiles is in determining which profiles (if any) are currently active, and which profiles (if any) should be active by default.
Unless you need to access that information, you should stick to using the placeholder notation with ${..}
format and the @Value
annotations. Again, to quote the docs:
In most cases, however, application-level beans should not need to interact with the Environment directly but instead may have to have ${...} property values replaced by a property placeholder configurer such as PropertySourcesPlaceholderConfigurer, which itself is EnvironmentAware and as of Spring 3.1 is registered by default when using
<context:property-placeholder/>
.
So, to summarize:
Environement
object, you can access the information related to profiles. You can't do that with @Value
@Value
annotations.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With