spring in action 学习笔记十:用@PropertySource避免注入外部属性的值硬代码化
@PropertySource的写法为:@PropertySource("classpath:某个.properties文件的类路径")
首先来看一下这个案例的目录结构,重点看带红色圆圈的。如下图所示:
从上图中可以看出student.properties文件的类路径是:
com/advancedWiring/ambiguityIniAutowiring2/student.properties
student.properties的代码如下:
1 #用配置文件的形式,避免注入属性值的硬代码化。 2 name=AbrahamLincoln 3 age=19
Student类的代码如下:
1 package com.advancedWiring.ambiguityIniAutowiring2; 2 3 import org.springframework.beans.factory.BeanNameAware; 4 import org.springframework.beans.factory.config.ConfigurableBeanFactory; 5 import org.springframework.context.annotation.Scope; 6 import org.springframework.context.annotation.ScopedProxyMode; 7 import org.springframework.stereotype.Component; 8 import org.springframework.web.context.WebApplicationContext; 9 10 /** 11 * Created by ${秦林森} on 2017/6/9. 12 */ 13 @Component 14 @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON) 15 public class Student implements BeanNameAware{ 16 private String name; 17 private Integer age; 18 19 public String getName() { 20 return name; 21 } 22 23 public void setName(String name) { 24 this.name = name; 25 } 26 27 public Integer getAge() { 28 return age; 29 } 30 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 }
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.core.env.Environment; 7 8 /** 9 * Created by ${秦林森} on 2017/6/9. 10 */ 11 @Configuration 12 /** 13 * @PropertySource主要是加载.properties文件。 14 */ 15 @PropertySource("classpath:com/advancedWiring/ambiguityIniAutowiring2/student.properties") 16 public class StudentConfig { 17 @Autowired 18 /** 19 * 加载的配置文件在Environment这个接口中。 20 */ 21 Environment evn; 22 @Bean 23 public Student student(){ 24 return new Student(evn.getProperty("name"),Integer.parseInt(evn.getProperty("age"))); 25 } 26 }
测试类Test的代码如下:
1 package com.advancedWiring.ambiguityIniAutowiring2; 2 3 import org.springframework.context.annotation.AnnotationConfigApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 /** 7 * Created by ${秦林森} on 2017/6/9. 8 */ 9 public class Test { 10 public static void main(String[] args) { 11 AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(StudentConfig.class); 12 Student student = ac.getBean(Student.class); 13 /** 14 * 输出结果是:the student name is: AbrahamLincoln and age is :19 15 * 可以看出他把配置文件的值给取出来了。 16 */ 17 System.out.println("the student name is: "+student.getName()+" and age is :"+student.getAge()); 18 } 19 }