Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between @Value annotation and the Environment API?

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);
    }

}
like image 320
Jigish Avatar asked Nov 12 '14 21:11

Jigish


People also ask

What is @value annotation is used for?

Spring @Value annotation is used to assign default values to variables and method arguments.

What is the purpose of @value spring?

@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.

What is the use of @value in spring boot?

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).

Can we use @value in interface?

No, this is not (directly) possible. The default value of an annotation property must be a compile-time constant.


1 Answers

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:

  1. With the Environement object, you can access the information related to profiles. You can't do that with @Value
  2. Unless you need the info related to profiles (and you probably shouldn't), you should stick to the @Value annotations.
like image 129
Bhashit Parikh Avatar answered Oct 23 '22 17:10

Bhashit Parikh