[Spring] annotation-driven development - property assignment - @ Value & @ PropertySource

This blog demo source address
https://github.com/suchahaerkang/spring-annotation.git

1 to create the object by no-argument constructor

First, we write a class configuration, will be registered component to a container to Person

/**
 * @description:
 * @author: sukang
 * @date: 2020-03-07 11:00
 */
@Configuration
public class MainConfigOfPropertyValues {

    @Bean
    public Person person(){
        return new Person();
    }
}

Person assembly

/**
 * @description:
 * @author: sukang
 * @date: 2020-03-04 12:06
 */
@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Person {

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    //姓名
    private String name;

    //年龄
    private int age;

    //昵称
    private String nikeName;
}

Write test cases

 @Test
 public void test01(){
      //创建容器
      ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfPropertyValues.class);
      //从容器中获取id为person的Person组件
      Person person = (Person) applicationContext.getBean("person");
      System.out.println(person);
  }

operation result

! [Insert Picture description here] (https://img-blog.csdnimg.cn/20200307112252424.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3N1Y2hhaGFlcmthbmc=,size_16,color_FFFFFF,t_70

2 component property assignment

@Value annotation assigns them except when the assembly created by the constructor has reference assignment, but also may be provided by spring

2.1 @Value

@Value给属性赋值有三种方式
1) @Value("xxx"),直接加上想要赋的值
2) @Value("#{}") ,也可以通过spring提供的SPEL表达式来赋值
3) @Value("${}"),也可以通过注解@PrepertySource的方式将配置文件注入到运行环境中,然后通过${}的方式去获取
Let's test modes 1 and 2

/**
 * @description:
 * @author: sukang
 * @date: 2020-03-04 12:06
 */
@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Person {

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    //姓名
    @Value("张三")
    private String name;

    //年龄
    @Value("#{20-2}")
    private int age;

    //昵称
    private String nikeName;
}

Operating results
Here Insert Picture Description
and then we test a third way
First we add a person.properties configuration file in your classpath
Here Insert Picture Description
and then add @PropertySource comment on a profile

/**
 * @description:
 * @author: sukang
 * @date: 2020-03-07 11:00
 */
@PropertySource(value={"classpath:/person.properties"},encoding = "utf-8")
@Configuration
public class MainConfigOfPropertyValues {

    @Bean
    public Person person(){
        return new Person();
    }
}

Finally nickName property in the Person above components marked notes

//昵称
@Value("${person.nickName}")
private String nickName;

operation result
Here Insert Picture Description

2.2 @PropertySource

This annotation is actually injected into the specified configuration file inside the key-value to the value of the operating environment
Here we test the environment variable in the value of the acquisition person.nickName
write test cases

@Test
public void test02(){
	  //创建容器
	  ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfPropertyValues.class);
	  //从容器中获取环境变量组件
	  Environment environment = applicationContext.getEnvironment();
	  //从环境变量中获取person.nickName值
	  String nickName = environment.getProperty("person.nickName");
	  System.out.println("从环境变量中获取的nickName为:" + nickName);
}

operation result
Here Insert Picture Description

Published 78 original articles · won praise 32 · views 90000 +

Guess you like

Origin blog.csdn.net/suchahaerkang/article/details/104711072