spring in action 学习十二:property placeholder 注解的方式实现避免注入外部属性硬代码化

 这里的注解是指@PropertySource这个注解。用@PropertySource这个注解加载.properties文件。

案例的目录结构如下:

student.properties的代码如下:

1 #用配置文件的形式,避免注入属性值的硬代码化。
2 name=AbrahamLincoln
3 age=21

Student的代码如下:

 1 package com.advancedWiring.ambiguityIniAutowiring2;
 2 
 3 import org.springframework.beans.factory.BeanNameAware;
 4 import org.springframework.beans.factory.annotation.Value;
 5 import org.springframework.beans.factory.config.ConfigurableBeanFactory;
 6 import org.springframework.context.annotation.Scope;
 7 import org.springframework.context.annotation.ScopedProxyMode;
 8 import org.springframework.stereotype.Component;
 9 import org.springframework.web.context.WebApplicationContext;
10 
11 /**
12  * Created by ${秦林森} on 2017/6/9.
13  */
14 @Component
15 public class Student implements BeanNameAware{
16     private  String name;
17     private Integer age;
18 
19     public String getName() {
20         return name;
21     }
22     @Value("${name}")//注入值
23     public void setName(String name) {
24         this.name = name;
25     }
26 
27     public Integer getAge() {
28         return age;
29     }
30     @Value("${age}")//注入值
31     public void setAge(Integer age) {
32         this.age = age;
33     }
34 
35     @Override
36     public void setBeanName(String name) {
37         System.out.println("the Student bean name is :"+name);
38     }
39 
40     /**
41      * write this constructor mainly to inject external property value.
42      * @param name the student's name
43      * @param age  the student's age
44      */
45     public Student(  String name,   Integer age) {
46         this.name = name;
47         this.age = age;
48     }
49 
50     public Student() {
51     }
52 }

StudentConfig的代码如下:

 1 package com.advancedWiring.ambiguityIniAutowiring2;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.beans.factory.config.ConfigurableBeanFactory;
 5 import org.springframework.context.annotation.*;
 6 import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
 7 import org.springframework.core.env.Environment;
 8 
 9 /**
10  * Created by ${秦林森} on 2017/6/9.
11  */
12 @Configuration
13 /**
14  * @PropertySource主要是加载.properties文件。
15  */
16 @ComponentScan
17 @PropertySource("classpath:com/advancedWiring/ambiguityIniAutowiring2/student.properties")
18 public class StudentConfig {
19 
20 }

测试类的代码如下:

 1 package com.advancedWiring.ambiguityIniAutowiring2;
 2 
 3 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
 6 
 7 /**
 8  * Created by ${秦林森} on 2017/6/9.
 9  */
10 public class Test {
11     public static void main(String[] args) {
12 
13         AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(StudentConfig.class);
14         Student student = ac.getBean(Student.class);
15         /**
16          * 输出结果是:the student name is: AbrahamLincoln and age is :21
17          * 可以看出他把配置文件的值给取出来了。
18          */
19         System.out.println("the student name is: "+student.getName()+" and age is :"+student.getAge());
20     }
21 }

 

posted @ 2017-06-10 08:45  技术让世界更精彩  阅读(366)  评论(0编辑  收藏  举报