SpringBoot @Value注解

作者: jiafegn | 来源:发表于2022-11-04 11:29 被阅读0次

    作者:小猿聊编程
    更多资料:https://www.techlearn.cn/

    作用:@Value注解主要用于是将配置文件中的键对应的值分配给某类内带注解的属性。

    使用方式:

    1、非配置文件注入,

    2、配置文件注入,通过@Value注解将配置文件中的属性注入到容器内组件中(可用在@Controller@Service@Configuration@Component)

    定义

    @Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface Value {
        String value();
    }
    

    参数:value参数代表需要配置文件中的键或是值。

    非配置文件方式注入

    1、注入普通字符串(非配置文件注入)

    @Service
    public class ValueService {
    
        @Value("Hello")
        private String name;
    
        public String getName() {
            return "my name is : " + name;
        }
    }
    
    @SpringBootTest
    class DemoApplicationTests {
    
        @Autowired
        private ValueService valueService;
    
        @Test
        void contextLoads() {
            System.out.println(valueService.getName());
        }
    
    }
    

    运行结果输出:

    my name is : Hello
    

    从输出结果可以看,ValueService类的属性name被赋予了一个初始的值

    2、注入JAVA系统变量(非配置文件注入)

    @Service
    public class ValueService {
    
        @Value("#{systemProperties['os.name']}")
        private String osName;
    
        public String getName() {
            return "os name is : " + osName;
        }
    }
    
    @SpringBootTest
    class DemoApplicationTests {
    
        @Autowired
        private ValueService valueService;
    
        @Test
        void contextLoads() {
            System.out.println(valueService.getName());
        }
    
    }
    

    运行结果输出:

    os name is : Mac OS X
    

    从输入结果中可以看出,ValueService类的属性osName被赋予了一个初始的值,该值是JAVA的系统变量systemProperties

    3、注入文件资源(非配置文件注入)

    @Service
    public class ValueService {
    
        @Value("classpath:application.properties")
        private Resource resource;
    }
    

    4、注入URL资源(非配置文件注入)

    @Service
    public class ValueService {
    
        @Value("http://www.baidu.com")
        private Resource resource;
    }
    

    基于配置文件注入

    @Value主要使用在于从配置文件中读取相对应的配置项,@Value 默认只读取application.properties文件中的配置项,如果需要读取其它配置文件中的配置项,需要使用@PropertySource注解引入对应的文件,使用方式如下:

    @Service
    @PropertySource({"classpath:db.properties"})
    public class ValueService {
    
    }
    

    1、注入普通字符串(配置文件属性注入)

    application.properties配置文件中添加以下配置项:

    demo.datasource.config.host=127.0.0.1
    
    @Service
    public class ValueService {
    
        @Value("${demo.datasource.config.host}")
        private String host;
    
        public String getName() {
            return "datasource.config.host is : " + host;
        }
    }
    
    @SpringBootTest
    class DemoApplicationTests {
    
        @Autowired
        private ValueService valueService;
    
        @Test
        void contextLoads() {
            System.out.println(valueService.getName());
        }
    
    }
    

    测试运行输出:

    datasource.config.host : 127.0.0.1
    

    从测试结果可以看出,此时host的值为配置文件中的127.0.0.1,当配置文件中demo.datasource.config.host为空时,此时host的值 为"".

    同时也可以在注入属性设置相对应的默认值,

    注意只有配置文件在没有配置相关的配置项,默认值才起效,如果在配置文件有相关的配置项,无论该配置项是否为空,默认值都不会起效。

    @Service
    public class ValueService {
    
        @Value("${demo.datasource.config.host:127.0.0.1}")
        private String host;
    
        public String getName() {
            return "datasource.config.host : " + host;
        }
    }
    
    @SpringBootTest
    class DemoApplicationTests {
    
        @Autowired
        private ValueService valueService;
    
        @Test
        void contextLoads() {
            System.out.println(valueService.getName());
        }
    
    }
    

    运行输出结果:

    datasource.config.host : 127.0.0.1
    

    从输出中可以看出,host被赋予了127.0.0.1的值。而demo.datasource.config.host并没有在配置文件进行配置。

    2、注入数组类型(配置文件属性注入)

    application.properties配置文件中添加以下配置项:

    demo.ids=1,2,3
    
    @Service
    public class ValueService {
    
        @Value("${demo.ids}")
        public int[] ids;
    
        public String getName() {
            for (int id: ids){
                System.out.println(id);
            }      
        }
    }
    
    @SpringBootTest
    class DemoApplicationTests {
    
        @Autowired
        private ValueService valueService;
    
        @Test
        void contextLoads() {
            System.out.println(valueService.getName());
        }
    
    }
    

    运行输出结果:

    1
    2
    3
    

    3、注入List类型(配置文件属性注入)

    application.properties配置文件中添加以下配置项:

    demo.ids=1,2,3
    
    @Service
    public class ValueService {
    
        //#{ } 里面写表 SpEL 达式
        @Value("#{'${demo.ids}'.split(',')}")
        public List<String> ids;
    
        public String getName() {
            System.out.println(ids);
            return "";
        }
    }
    
    @SpringBootTest
    class DemoApplicationTests {
    
        @Autowired
        private ValueService valueService;
    
        @Test
        void contextLoads() {
            System.out.println(valueService.getName());
        }
    
    }
    

    运行输出结果:

    [1, 2, 3]
    

    4、注入Map类型

    application.properties配置文件中添加以下配置项

    demo.user={name:'admin',age:'23'}
    
    @Service
    public class ValueService {
    
        @Value("#{${demo.user}}")
        public Map<String, String> userMap;
    
        public String getName() {
            System.out.println(userMap);
            return "datasource.config.host : " + host;
        }
    }
    
    @SpringBootTest
    class DemoApplicationTests {
    
        @Autowired
        private ValueService valueService;
    
        @Test
        void contextLoads() {
            System.out.println(valueService.getName());
        }
    
    }
    

    运行输出结果:

    {name=admin, age=23}
    

    相关文章

      网友评论

        本文标题:SpringBoot @Value注解

        本文链接:https://www.haomeiwen.com/subject/enwytdtx.html