这里用到了placeholder特有的一个语言或者将表达形式:${},spring in action 描述如下:
In spring wiring ,placeholder values are property names wrapped with ${...},as an exampl,you can resolve the constructor arguments for a BlankDisc in xml like this :
<bean id="sgtPeppers"
class="soundsystem.BlankDisc"
c:_titile="${disc.title}"
c:_artist="${disc.artist}"/>
下面这个案例用${...}从配置文件读取内容,
记得一定要在配置文件写上下面的语句:
1 <!-- 加载.properties文件--> 2 <context:property-placeholder location="classpath:com/advancedWiring/ambiguityIniAutowiring2/student.properties"/>
案例的目录结构如下图所示:
案例的代码如下:
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 public class Student implements BeanNameAware{ 14 private String name; 15 private Integer age; 16 17 public String getName() { 18 return name; 19 } 20 21 public void setName(String name) { 22 this.name = name; 23 } 24 25 public Integer getAge() { 26 return age; 27 } 28 29 public void setAge(Integer age) { 30 this.age = age; 31 } 32 33 @Override 34 public void setBeanName(String name) { 35 System.out.println("the Student bean name is :"+name); 36 } 37 38 /** 39 * write this constructor mainly to inject external property value. 40 * @param name the student‘s name 41 * @param age the student‘s age 42 */ 43 public Student(String name, Integer age) { 44 this.name = name; 45 this.age = age; 46 } 47 }
student.properties文件的代码如下:
1 #用配置文件的形式,避免注入属性值的硬代码化。 2 name=AbrahamLincoln 3 age=21
ambiguity.xml的代码如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:c="http://www.springframework.org/schema/c" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xsi:schemaLocation="http://www.springframework.org/schema/aop 8 http://www.springframework.org/schema/aop/spring-aop.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context.xsd 11 http://www.springframework.org/schema/beans 12 http://www.springframework.org/schema/beans/spring-beans.xsd"> 13 <context:annotation-config/> 14 <!-- 加载.properties文件--> 15 <context:property-placeholder location="classpath:com/advancedWiring/ambiguityIniAutowiring2/student.properties"/> 16 <bean id="student" 17 class="com.advancedWiring.ambiguityIniAutowiring2.Student" 18 c:name="${name}" 19 c:age="${age}"> 20 </bean> 21 </beans>
测试类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 12 ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("com/advancedWiring/ambiguityIniAutowiring2/ambiguity.xml"); 13 Student student = ac.getBean(Student.class); 14 /** 15 * 输出结果是:the student name is: AbrahamLincoln and age is :19 16 * 可以看出他把配置文件的值给取出来了。 17 */ 18 System.out.println("the student name is: "+student.getName()+" and age is :"+student.getAge()); 19 } 20 }
时间: 2024-10-10 16:49:19