开发中经常涉及调用各种资源的情况,包含普通文件、网址、配置文件、系统环境变量等,我们可以使用Spring的表达式语言实现资源的注入,Spring主要在@Value注解中使用表达式。
一:添加依赖。增加commons-io可简化文件相关操作。将文件可转化为字符串
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.3</version>
</dependency>
项目结构已经资源情况如下图:
二:直接注入普通字符串如下类:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class DemoService {
@Value("其他类的属性") //注入普通字符串
private String another;
public String getAnother() {
return another;
}
public void setAnother(String another) {
this.another = another;
}
}
三:配置类(操作各种资源)
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;
@Configuration
@ComponentScan("com.flysun.el")
@PropertySource(value="classpath:com/flysun/el/test.properties")//7:注入配置文件需要@PropertySource注解指定文件位置,若还使用@Value
//注入文件中的内容,还要配置一个PropertySourcesPlaceholderConfigurer的bean.注意此时@Value("${book.name}")使用的是$而不是#。
//注入的properties还可以从Environment中获得
public class ElConfig {
@Value("I Love You!") //1注入普通字符串
private String normal;
@Value("#{systemProperties['os.name']}") //2注入操作系统属性
private String osName;
@Value("#{ T(java.lang.Math).random() * 100.0 }") //3注入表达式结果
private double randomNumber;
@Value("#{demoService.another}") //4注入其他bean的属性
private String fromAnother;
@Value("classpath:com/flysun/el/test.txt") //5注入文件资源
private Resource testFile;
@Value("http://www.baidu.com") //6注入网址资源
private Resource testUrl;
@Value("${book.name}") //7读取配置文件中的属性
private String bookName;
@Autowired
private Environment environment; //7
@Bean //7
public static PropertySourcesPlaceholderConfigurer propertyConfigure() {
return new PropertySourcesPlaceholderConfigurer();
}
public void outputResource() {
try {
System.out.println(normal);
System.out.println(osName);
System.out.println(randomNumber);
System.out.println(fromAnother);
System.out.println(IOUtils.toString(testFile.getInputStream()));
System.out.println(IOUtils.toString(testUrl.getInputStream()));
System.out.println(bookName);
System.out.println(environment.getProperty("book.author"));
System.out.println(demoService.getAnother());
} catch (Exception e) {
e.printStackTrace();
}
}
}
四:运行
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(ElConfig.class);
ElConfig resourceService = context.getBean(ElConfig.class);
resourceService.outputResource();
context.close();
}
}
也可以参考这个:https://www.cnblogs.com/sxdcgaq8080/p/7659074.html。在SpringBoot中还时常用