Spring注解--@Value、@PropertySource

喜欢ヅ旅行 2021-08-13 16:46 534阅读 0赞

Bean

  1. package pers.zhang.bean;
  2. import org.springframework.beans.factory.annotation.Value;
  3. public class Person {
  4. //使用@Value赋值
  5. //1. 基本数值
  6. //2. SpEL: #{}
  7. //3. ${}:取出配置文件中的值(在运行环境中的值)
  8. @Value("张三")
  9. private String name;
  10. @Value("#{20 - 2}")
  11. private Integer age;
  12. @Value("${person.nickName}")
  13. private String nickName;
  14. public String getNickName() {
  15. return nickName;
  16. }
  17. public void setNickName(String nickName) {
  18. this.nickName = nickName;
  19. }
  20. public Person() {
  21. }
  22. public Person(String name, Integer age) {
  23. this.name = name;
  24. this.age = age;
  25. }
  26. public String getName() {
  27. return name;
  28. }
  29. public void setName(String name) {
  30. this.name = name;
  31. }
  32. public Integer getAge() {
  33. return age;
  34. }
  35. public void setAge(Integer age) {
  36. this.age = age;
  37. }
  38. @Override
  39. public String toString() {
  40. return "Person{" +
  41. "name='" + name + '\'' +
  42. ", age=" + age +
  43. ", nickName='" + nickName + '\'' +
  44. '}';
  45. }
  46. }

主配置文件

  1. package pers.zhang.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.context.annotation.PropertySource;
  5. import org.springframework.context.annotation.PropertySources;
  6. import pers.zhang.bean.Person;
  7. //加载外部配置文件保存到运行的环境变量中,参数为String[]
  8. @PropertySource(value = { "classpath:/person.properties"})
  9. //一次指定多个PropertySource
  10. //@PropertySources()
  11. @Configuration
  12. public class MainConfigOfPropertyValues {
  13. @Bean
  14. public Person person(){
  15. return new Person();
  16. }
  17. }

配置文件:

  1. person.nickName=nikie

测试:

  1. @Test
  2. public void test01(){
  3. AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(MainConfigOfPropertyValues.class);
  4. String[] names = ac.getBeanDefinitionNames();
  5. for(String name : names){
  6. System.out.println(name);
  7. }
  8. System.out.println("****************************************");
  9. Person person = (Person) ac.getBean("person");
  10. System.out.println(person);
  11. System.out.println("****************************************");
  12. //通过环境变量获取配置文件中的值
  13. String s = ac.getEnvironment().getProperty("person.nickName");
  14. System.out.println(s);
  15. }

输出:

  1. org.springframework.context.annotation.internalConfigurationAnnotationProcessor
  2. org.springframework.context.annotation.internalAutowiredAnnotationProcessor
  3. org.springframework.context.annotation.internalRequiredAnnotationProcessor
  4. org.springframework.context.annotation.internalCommonAnnotationProcessor
  5. org.springframework.context.event.internalEventListenerProcessor
  6. org.springframework.context.event.internalEventListenerFactory
  7. mainConfigOfPropertyValues
  8. person
  9. ****************************************
  10. Person{ name='张三', age=18, nickName='nikie'}
  11. ****************************************
  12. nikie

发表评论

表情:
评论列表 (有 0 条评论,534人围观)

还没有评论,来说两句吧...

相关阅读

    相关 spring注解

    (学习留存,如有侵权,请告知,立刻删除!) 一、@Override(覆盖也意“重写”) @override可以翻译为“覆盖”也可以理解为“重写”,从字面上就可以知道,它是覆

    相关 spring注解

    本文主要罗列Spring|SpringMVC相关注解的简介。 Spring部分 1.声明bean的注解 @Component 组件,没有明确的角色 @Service

    相关 Spring注解

    1、@Primary @Primary的含义是告诉Spring IoC容器,当发现多个同样类型的Bean时,请优先使用我进行注入。 @Component

    相关 spring注解

    注解是个好东西,但好东西我们也是看见过,整理过,理解过,用过才知道好。不求我们每个都记住,但求保有印象,在需要的时候能提取出来再查找相关资料,平时工作就不会显得那么被动了。