@Value注入

来源:互联网 发布:手机电脑控制软件 编辑:程序博客网 时间:2024/05/29 03:51

通过@Value加载资源文件的示例

@PropertySource("classpath:com/example/test.properties")//加载指定文件资源 public class ElConfig {    @Value("I Love You!") //直接指定字符串        private String normal;    @Value("#{systemProperties['os.name']}") //通过spring el获取系统值    private String osName;    @Value("#{ T(java.lang.Math).random() * 100.0 }") //表达式结果        private double randomNumber;    @Value("#{demoService.another}") //Spring el获取其它类的值    private String fromAnother;    @Value("classpath:com/example/test.txt") //加载指定文件资源    private Resource testFile;    @Value("http://www.baidu.com") //将网址字符串转换成资源     private Resource testUrl;    @Value("${book.name}") //获取配置文件里key为book.name的值    private String bookName;}

针对@Value(“#{demoService.another}”)的情况

@Servicepublic class DemoService {    @Value("其他类调用的属性")         private String another;    public String getAnother() {//private时必须存在get方法        return another;    }}
0 0