Spring开发中经常设计调用各种资源的情况,包括普通文件,网址、配置文件、系统环境变量等,Spring的提供了很多种犯法来完成这个步骤,这里仅介绍@Value注解来时实现。
1.@Value("")
@Value("张三")
private String name;
@Value("classpath:/person.properties")
private Resource file;
@Value("http://www.baidu.com")
private Resource testUrl;
2.@Value("#{}")
@Value("#{}") 表示SpEl表达式通常用来获取bean的属性,或者调用bean的某个方法,也可以表示常量。
当bean通过@Value(“#{}”) 获取其他bean的属性,或者调用其他bean的方法时,只要该bean (Beab_A)能够访问到被调用的bean(Beab_B),即要么Beab_A 和Beab_B在同一个容器中,或者Beab_B所在容器是Beab_A所在容器的父容器。
@Value("#{T(java.lang.Math).random() * 100}")
private Integer age;
@Value("#{systemProperties['os.name']}")
private String osName;
@Value("#{1}")
private int number; //获取数字 1
3.@Value("${}")
通过@Value("${}") 可以获取对应属性文件中定义的属性值。
@Value("${nick.name}")
private String nickName;
4.测试
创建一个实体类
package com.yanling.bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
/**
*
* @author 16437
*
*/
public class Person {
@Value("张三")
private String name;
@Value("#{T(java.lang.Math).random() * 100}")
private Integer age;
@Value("#{systemProperties['os.name']}")
private String osName;
@Value("classpath:/person.properties")
private Resource file;
@Value("http://www.baidu.com")
private Resource testUrl;
@Value("${nick.name}")
private String nickName;
public Resource getFile() {
return file;
}
public void setFile(Resource file) {
this.file = file;
}
public Resource getTestUrl() {
return testUrl;
}
public void setTestUrl(Resource testUrl) {
this.testUrl = testUrl;
}
public Person() {
super();
}
public Person(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getOsName() {
return osName;
}
public void setOsName(String osName) {
this.osName = osName;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", osName=" + osName + ", file=" + file + ", testUrl="
+ testUrl + ", nickName=" + nickName + "]";
}
}
创建一个属性文件person.properties
nick.name=小三
创建一个配置类,这里不写xml配置,直接通过注解来实现spring的各种配置。
package com.yanling.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import com.yanling.bean.Person;
/**
*
* @author 16437
*
*/
//使用@PropertySource读取外部配置文件中K-V保存到运行环境中;之后再使用${}来读取出配置文件的值
@PropertySource(value="classpath:/person.properties")
@Configuration//声明这是一个配置类
public class MainConfigOfPropertyValue {
//创建Bean
@Bean
public Person person() {
return new Person();
}
}
测试
@Test
public void test1() {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfPropertyValue.class);
Person person = (Person) applicationContext.getBean("person");
System.out.println(person.toString());
ConfigurableEnvironment environment = applicationContext.getEnvironment();
String property = environment.getProperty("nick.name");
System.out.println(property);
applicationContext.close();
}