zoukankan      html  css  js  c++  java
  • 【Spring注解驱动开发】如何使用@Value注解为bean的属性赋值,我们一起吊打面试官!

    写在前面

    在之前的文章中,我们探讨了如何向Spring的IOC容器中注册bean组件,讲解了有关bean组件的生命周期的知识。今天,我们就来一起聊聊@Value注解的用法。

    项目工程源码已经提交到GitHub:https://github.com/sunshinelyz/spring-annotation

    @Value注解

    Spring中的@Value注解可以为bean中的属性赋值。我们先来看看@Value注解的源码,如下所示。

    package org.springframework.beans.factory.annotation;
    import java.lang.annotation.Documented;
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface Value {
    	String value();
    }
    

    从@Value注解的源码,我们可以看出:@Value注解可以标注在字段、方法、参数、注解上,在程序运行期间生效。

    @Value注解用法

    1.不通过配置文件注入属性的情况

    通过@Value将外部的值动态注入到Bean中,使用的情况有:

    • 注入普通字符串
    @Value("normal")
    private String normal; // 注入普通字符串
    
    • 注入操作系统属性
    @Value("#{systemProperties['os.name']}")
    private String systemPropertiesName; // 注入操作系统属性
    
    • 注入表达式结果
    @Value("#{ T(java.lang.Math).random() * 100.0 }")
    private double randomNumber; //注入表达式结果
    
    • 注入其他Bean属性
    @Value("#{person.name}")
    private String name; // 注入其他Bean属性:注入person对象的属性name
    
    • 注入文件资源
    @Value("classpath:io/mykit/spring/config/config.properties")
    private Resource resourceFile; // 注入文件资源
    
    • 注入URL资源
    @Value("http://www.baidu.com")
    private Resource url; // 注入URL资源
    

    2.通过配置文件注入属性的情况

    通过@Value(“${app.name}”)语法将属性文件的值注入到bean的属性中,如下所示。

    @Component
    // 引入外部配置文件组:${app.configinject}的值来自config.properties。
    // 如果相同
    @PropertySource({"classpath:io/mykit/spring/config/config.properties",
        "classpath:io/mykit/spring/config/config_${anotherfile.configinject}.properties"})
    public class ConfigurationFileInject{
        // 这里的值来自application.properties,spring boot启动时默认加载此文件
        @Value("${app.name}")
        private String appName; 
    
        // 注入第一个配置外部文件属性
        @Value("${book.name}")
        private String bookName; 
    
        // 注入第二个配置外部文件属性
        @Value("${book.name.placeholder}")
        private String bookNamePlaceholder; 
    
        // 注入环境变量对象,存储注入的属性值
        @Autowired
        private Environment env;  
    
        public String toString(){
            StringBuilder sb = new StringBuilder();
            sb.append("bookName=").append(bookName).append("
    ")
            .append("bookNamePlaceholder=").append(bookNamePlaceholder).append("
    ")
            .append("appName=").append(appName).append("
    ")
            .append("env=").append(env).append("
    ")
            // 从eniroment中获取属性值
            .append("env=").append(env.getProperty("book.name.placeholder")).append("
    ");
            return sb.toString();
        }   
    }
    

    3.@Value中#{..}和${...}的区别

    我们这里提供一个测试属性文件:advance_value_inject.properties,大致的内容如下所示。

    server.name=server1,server2,server3
    author.name=binghe
    

    测试类AdvanceValueInject:引入advance_value_inject.properties文件,作为属性的注入

    @Component
    @PropertySource({"classpath:io/mykit/spring/config/advance_value_inject.properties"})
    public class AdvanceValueInject {
    ...
    }
    

    ${...}的用法

    {}里面的内容必须符合SpEL表达式, 通过@Value(“${spelDefault.value}”)可以获取属性文件中对应的值,但是如果属性文件中没有这个属性,则会报错。可以通过赋予默认值解决这个问题,如下所示。

    @Value("${author.name:binghe}")
    

    上述代码的含义表示向bean的属性中注入配置文件中的author.name属性的值,如果配置文件中没有author.name属性,则向bean的属性中注入默认值binghe。例如下面的代码片段。

    @Value("${author.name:binghe}")
    private String name;
    

    #{…}的用法

    // SpEL:调用字符串Hello World的concat方法
    @Value("#{'Hello World'.concat('!')}")
    private String helloWorld;
    
    // SpEL: 调用字符串的getBytes方法,然后调用length属性
    @Value("#{'Hello World'.bytes.length}")
    private String helloWorldbytes;
    

    ${…}和#{…}混合使用

    ${...}和#{...}可以混合使用,如下文代码执行顺序:通过${server.name}从属性文件中获取值并进行替换,然后就变成了 执行SpEL表达式{‘server1,server2,server3’.split(‘,’)}。

    // SpEL: 传入一个字符串,根据","切分后插入列表中, #{}和${}配置使用(注意单引号,注意不能反过来${}在外面,#{}在里面)
    @Value("#{'${server.name}'.split(',')}")
    private List<String> servers;
    

    在上文中#{}在外面,${}在里面可以执行成功,那么反过来是否可以呢?也就是说能否让${}在外面,#{}在里面,如下代码所示。

    // SpEL: 注意不能反过来${}在外面,#{}在里面,这个会执行失败
    @Value("${#{'HelloWorld'.concat('_')}}")
    private List<String> servers2;
    

    答案是不能。因为Spring执行${}时机要早于#{},当Spring执行外层的${}时,内部的#{}为空,所以会执行失败!

    @Value注解用法小结:

    • #{…} 用于执行SpEl表达式,并将内容赋值给属性。
    • ${…} 主要用于加载外部属性文件中的值。
    • #{…} 和${…} 可以混合使用,但是必须#{}外面,${}在里面。

    @Value注解案例

    这里,我们还是以一个小案例的形式来说明。

    首先,我们来创建一个Person类作为测试的bean组件,如下所示。

    package io.mykit.spring.plugins.register.bean;
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import lombok.ToString;
    import java.io.Serializable;
    /**
     * @author binghe
     * @version 1.0.0
     * @description 测试实体类
     */
    @Data
    @ToString
    @NoArgsConstructor
    @AllArgsConstructor
    public class Person implements Serializable {
        private static final long serialVersionUID = 7387479910468805194L;
        private String name;
        private Integer age;
    }
    

    接下来,创建一个新的配置类PropertyValueConfig,用来配置Spring的bean组件,我们在PropertyValueConfig类中将Person类的对象注册到IOC容器中,如下所示。

    package io.mykit.spring.plugins.register.config;
    import io.mykit.spring.plugins.register.bean.Person;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    /**
     * @author binghe
     * @version 1.0.0
     * @description 测试属性赋值
     */
    @Configuration
    public class PropertyValueConfig {
        @Bean
        public Person person(){
            return new Person();
        }
    }
    
    

    我们再来创建一个测试类PropertyValueTest,在PropertyValueTest类中创建测试方法testPropertyValue01(),并在testPropertyValue01()方法中通过PropertyValueConfig类创建AnnotationConfigApplicationContext对象,打印出目前IOC容器中存在的bean名称,如下所示。

    package io.mykit.spring.test;
    import io.mykit.spring.plugins.register.config.PropertyValueConfig;
    import org.junit.Test;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import java.util.Arrays;
    /**
     * @author binghe
     * @version 1.0.0
     * @description 测试bean的生命周期
     */
    public class PropertyValueTest {
        @Test
        public void testPropertyValue01(){
            //创建IOC容器
            AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(PropertyValueConfig.class);
            String[] names = context.getBeanDefinitionNames();
            Arrays.stream(names).forEach(System.out::println);
        }
    }
    

    此时,我们运行PropertyValueTest类的testPropertyValue01()方法,输出的结果信息如下所示。

    org.springframework.context.annotation.internalConfigurationAnnotationProcessor
    org.springframework.context.annotation.internalAutowiredAnnotationProcessor
    org.springframework.context.annotation.internalCommonAnnotationProcessor
    org.springframework.context.event.internalEventListenerProcessor
    org.springframework.context.event.internalEventListenerFactory
    propertyValueConfig
    person
    

    从输出的结果信息中,可以看出,IOC容器中除了Spring框架注册的bean之外,还包含我们自己向IOC容器中注册的bean组件:propertyValueConfig和person。

    接下来,我们改造下PropertyValueTest类的testPropertyValue01()方法,输出Person对象的信息,如下所示。

    package io.mykit.spring.test;
    import io.mykit.spring.plugins.register.bean.Person;
    import io.mykit.spring.plugins.register.config.PropertyValueConfig;
    import org.junit.Test;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import java.util.Arrays;
    /**
     * @author binghe
     * @version 1.0.0
     * @description 测试bean的生命周期
     */
    public class PropertyValueTest {
        @Test
        public void testPropertyValue01(){
            //创建IOC容器
            AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(PropertyValueConfig.class);
            String[] names = context.getBeanDefinitionNames();
            Arrays.stream(names).forEach(System.out::println);
    
            System.out.println("================================");
            Person person = (Person) context.getBean("person");
            System.out.println(person);
        }
    }
    

    接下来,再次运行PropertyValueTest类的testPropertyValue01()方法,输出的结果信息如下所示。

    org.springframework.context.annotation.internalConfigurationAnnotationProcessor
    org.springframework.context.annotation.internalAutowiredAnnotationProcessor
    org.springframework.context.annotation.internalCommonAnnotationProcessor
    org.springframework.context.event.internalEventListenerProcessor
    org.springframework.context.event.internalEventListenerFactory
    propertyValueConfig
    person
    ================================
    Person(name=null, age=null)
    

    可以看到,向IOC容器中注册的Person对象的name属性为null,age属性为null。那如何向Person对象的name属性和age属性赋值呢?此时,Spring中的@Value注解就派上了用场。

    如果我们通过XML文件为bean的属性赋值,则可以通过如下配置的方式实现。

    <bean id = "person" class="io.mykit.spring.plugins.register.bean.Person">
        <property name="name" value="binghe"></property>
        <property name="age" value="18"></property>
    </bean>
    

    如果使用注解该如何实现呢?别急,往下看!

    我们可以在Person类的属性上使用@Value注解为属性赋值,如下所示。

    package io.mykit.spring.plugins.register.bean;
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import lombok.ToString;
    import org.springframework.beans.factory.annotation.Value;
    import java.io.Serializable;
    /**
     * @author binghe
     * @version 1.0.0
     * @description 测试实体类
     */
    @Data
    @ToString
    @NoArgsConstructor
    @AllArgsConstructor
    public class Person implements Serializable {
        private static final long serialVersionUID = 7387479910468805194L;
        @Value("binghe")
        private String name;
        @Value("#{20-2}")
        private Integer age;
    }
    

    此时,我们再次运行PropertyValueTest类的testPropertyValue01()方法,输出的结果信息如下所示。

    org.springframework.context.annotation.internalConfigurationAnnotationProcessor
    org.springframework.context.annotation.internalAutowiredAnnotationProcessor
    org.springframework.context.annotation.internalCommonAnnotationProcessor
    org.springframework.context.event.internalEventListenerProcessor
    org.springframework.context.event.internalEventListenerFactory
    propertyValueConfig
    person
    ================================
    Person(name=binghe, age=18)
    

    可以看到,使用@Value注解已经向Person对象的name属性中注入了binghe,向age属性中注入了18。

    好了,咱们今天就聊到这儿吧!别忘了给个在看和转发,让更多的人看到,一起学习一起进步!!

    项目工程源码已经提交到GitHub:https://github.com/sunshinelyz/spring-annotation

    写在最后

    如果觉得文章对你有点帮助,请微信搜索并关注「 冰河技术 」微信公众号,跟冰河学习Spring注解驱动开发。公众号回复“spring注解”关键字,领取Spring注解驱动开发核心知识图,让Spring注解驱动开发不再迷茫。

    部分参考:blog.csdn.net/hry2015/article/details/72453920

  • 相关阅读:
    js四舍五入
    文本框只能输入整数,输入其他的自动不显示
    [转]关于C#程序部署到Android
    ajax在火狐中传中文出现乱码的解决方法
    Vue 记录 Cannot read property '_withTask' of undefined
    vs中 VMDebugger未能加载导致异常
    System.InvalidOperationException: 支持“XXX”上下文的模型已在数据库创建后发生更改。请考虑使用 Code First 迁移更新数据库(http://go.microsoft.com/fwlink/?LinkId=238269)。
    eclipse中将java项目转换成javaweb项目
    Android之SOAP协议与WebService服务器交互,解决超时的问题
    SymmetricDS 快速和灵活的数据库复制
  • 原文地址:https://www.cnblogs.com/binghe001/p/13216798.html
Copyright © 2011-2022 走看看