【SpringBoot基础】Spring El 表达式读取配置
内容概要
使用 spring 支持的注解方式,读取系统文件中的配置,注入到容器中,方便调用。
环境jar包
jdk: jdk1.8.0_121(32位)
pom:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.10.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.0.1</version>
</dependency>
文件结构
test.properties 内容
test.txt内容
EL配置类 BeanSpringEl
package SpringEl;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
@Configuration
//扫描目录下配置 从java开始如目录为 com.springEl.此处填写 com.springEl
@ComponentScan("SpringEl")
//读取配置 默认起始地址为 resources
@PropertySource("test.properties")
public class BeanSpringEl {
@Value("Bean") //name = "bean"
private String name;
@Value("#{systemProperties['os.name']}") //取操作系统版本
private String systemName;
@Value("#{T(java.lang.Math).random() * 100.0}") //随机数计算值
private Double randomDouble;
@Value("#{anotherBean.anotherBean}") //取AnotherBean类中,anotherBean元素。
private String bean;
@Value("https://www.baidu.com") //访问 百度 地址 获取Html
private Resource testUrl;
@Value("test.txt")
private Resource testFile;//访问 resources地址下的 test.txt;
@Value("${system.name}")
private String sysName;//读取 resources地址下 test.properties内的system.name属性
@Autowired
private Environment environment; //获得容器中的环境变量,也可取出test.properties下的属性
@Bean //必须配置 使用该方式 取得test.properties下的数据
public static PropertySourcesPlaceholderConfigurer propertyConfigure(){
return new PropertySourcesPlaceholderConfigurer();
}
public void say(){
System.out.println(name);
System.out.println(systemName);
System.out.println(randomDouble);
System.out.println(bean);
try {
System.out.println(IOUtils.toString(testUrl.getInputStream()));
System.out.println(IOUtils.toString(testFile.getInputStream()));
}catch (Exception e){
System.out.println(e.getMessage());
}
System.out.println(environment.getProperty("system.boot"));
System.out.println(sysName);
}
}
另一个实例,测试取值使用
package SpringEl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service //声明
public class AnotherBean {
@Value("AnotherBean") // anotherBean = "AnotherBean"
private String anotherBean;
public String getAnotherBean() {
return anotherBean;
}
public void setAnotherBean(String anotherBean) {
this.anotherBean = anotherBean;
}
}
测试类Main
package SpringEl;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String [] args){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BeanSpringEl.class);
BeanSpringEl bean = context.getBean(BeanSpringEl.class);
bean.say();
context.close();
}
}