根据@Value可直接 在属性名上进行赋值,赋值的内容可以分为以下8种:
①字符串:@value(“要赋值的字符串”)
②数字:@value("#{T (java.lang.Math) .random( ) *10 }")
③系统名称:@Value("#{systemProperties[‘osName’]}")
④文件:@value(“classpath:包名/xx.txt”)
注意:可以是任意文件类型
⑤网址:@value("#{http://www.baidu.com}")
⑥指定类的属性值:@Value("#{xX类.属性名}")
⑦全局定义的properties文件中的属性值:
@value("#{properties中的属性名}")
⑧ @value("${属性名}")
示例:
定义的txt:
本文件中的内容“abcdEFG"。
定义的properties:
book.author=wangyufei
book.name=spring boot
BookService:(作为其他类)
@Service
public class BookService {
@Value("author是其他类的属性")
private String author;
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
配置类:(本示例在config中执行。也可在其他类中执行打印)
@Configuration
@ComponentScan("com.demo02.el")
@PropertySource("classpath:com/demo02/el/test.properties")
public class ElConfig {
//直接赋值
@Value("normal上的注解")
private String normal;
//安装的系统名称 如:Windows 10
@Value("#{systemProperties['os.name']}")
private String osName;
//随机数*100
@Value("#{T(java.lang.Math).random() * 100 }")
private double randomNumber;
/*
*BookService类中author属性上的@value值
*注意:首字母小写
*/
@Value("#{bookService.author}")
private String fromAuthor;
//指定路径的文件 classpath:包全名/文件名.后缀
@Value("classpath:com/demo02/el/test.txt")
private Resource testFile;
//指定网址,转换为字符就是该网址的html文件内容
@Value("http://www.baidu.com")
private Resource testUrl;
/*
*指定当前类中全局定义的proprties文件中属性的值。${属性名},
*注意是美元符号 ${属性名}
* @PropertySource("classpath:com/demo02/el/test.properties")中的book.name属性的值。
*/
@Value("${book.name}")
private String bookName;
//environment.getProperty("属性名") 可获取当前类定义的properties文件中指定属性的值
@Autowired
private Environment environment;
//使用@Value(${book.name})获取properties文件的el表达式,必须创建PropertySourcesPlaceholderConfigurer实例,并返回。
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
public void printResources() {
System.out.println("normal:"+normal);
System.out.println("osName:"+osName);
System.out.println("randomNumber:"+randomNumber);
System.out.println("fromAuthor:"+fromAuthor);
System.out.println("bookName:"+bookName);
System.out.println("environment-property book.author:"+environment.getProperty("book.author"));
System.out.println("environment-property book.name:"+environment.getProperty("book.name"));
try {
System.out.println("testFile字符:"+IOUtils.toString(testFile.getInputStream()));
System.out.println("testUrl字符:"+IOUtils.toString(testUrl.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Configuration:表示当前类为配置类
@ComponentScan("com.demo02.el"):自动扫描指定包中包含@component、@service、@controller、@repository注解的类放入容器
@PropertySource("classpath:com/demo02/el/test.properties"):表示当前类中都可引用指定的properties文件
运行结果:
normal:normal上的注解
osName:Windows 10
randomNumber:13.860361485703166
fromAuthor:author是其他类的属性
bookName:spring boot
environment-property book.author:wangyufei
environment-property book.name:spring boot
testFile字符:本文件中的内容“abcdEFG"。
testUrl字符:<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必读</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a> 京ICP证030173号 <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>