@Value批注和环境API之间的区别? [英] Differences between @Value annotation and the Environment API?

查看:172
本文介绍了@Value批注和环境API之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注入带有@Value注释的类的字段与使用Spring 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)?

使用@Value的示例:

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

    }

}

使用环境API的示例:

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

}

推荐答案

The Environment is a combination of profiles and properties.

配置文件是一个命名的,逻辑定义的bean定义组,可以根据您的环境来激活或不激活.可以将Bean分配给概要文件,无论是以XML定义还是通过注释定义.对于前.您可能有一个配置文件用于开发模式,另一个配置文件用于生产模式.您可以查找 @Profile 在此处查看有关它的更多详细信息.

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.

引用Environment文档:

与配置文件相关的环境对象的作用是确定哪些配置文件(如果有)当前处于活动状态,以及哪些配置文件(如果有)在默认情况下应处于活动状态.

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.

除非需要访问该信息,否则应坚持使用具有${..}格式和@Value注释的占位符.同样,引用 docs :

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:

但是,在大多数情况下,应用程序级Bean不需要直接与环境交互,而是可能必须将$ {...}属性值替换为诸如PropertySourcesPlaceholderConfigurer之类的属性占位符配置程序,后者本身就是EnvironmentAware使用<context:property-placeholder/>时,默认情况下从Spring 3.1开始注册.

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

因此,总结一下:

  1. 使用Environement对象,您可以访问与配置文件有关的信息.您不能使用@Value
  2. 除非您需要与配置文件相关的信息(而且您可能不需要),否则应坚持使用@Value批注.
  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.

这篇关于@Value批注和环境API之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆