In using annotations Spring development only, it can be annotated using @Value and @PropertySource be assigned to the properties of the object.
1, create a simple entity class
public class the Person { / ** * values @Value annotation may have the following * 1, the value is written directly in the properties among Value (String, int, boolean .....), for example, the name attribute * 2, write } # {i.e., expression spEL form, e.g. age property * 3, in the main configuration file by loading the configuration class @PropertySource annotation, then take the value in the configuration file in the form of $ {} * / @Value ( "John Doe " ) Private String name; @Value ( " # {31-9} " ) Private int Age; @Value ( " person.nikname $ {} " ) Private String nikname; public String getNikname () { return nikname; } public void setNikname(String nikname) { this.nikname = nikname; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Person(String name, int age) { super(); this.name = name; this.age = age; } public Person() { super(); } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", nikname=" + nikname + "]"; } }
2, add a profile person.properties in the class directory
Zhang person.nikname =
3, create a test class
@Test public void test6() { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainPropertiesConfig.class); Object object = applicationContext.getBean("person") ; System.out.println(object); }
operation result:
Person [name = Joe Smith, age = 22, nikname = Zhang]