这篇文章主要讲解了“spring EL怎么使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Spring EL怎么使用”吧!一:说明Spring EL-Spring表达式语言,支持在xml和
这篇文章主要讲解了“spring EL怎么使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Spring EL怎么使用”吧!
一:说明
Spring EL-Spring表达式语言,支持在xml和注解中使用表达式,类似于jsP的EL表达式语言。
Spring 开发中经常涉及调用各种资源的情况,包含普通文件,网址,配置文件,系统环境变量等。 我们可以使用Spring的表达式语言实现资源的注入。
二:代码实例
@Service
public class DemoService {
@Value("其他类的属性")
private String another;
public String getAnother() {
return another;
}
public void setAnother(String another) {
this.another = another;
}
}
点击(此处)折叠或打开
@Configuration
@ComponentScan("com.gemdale")
@PropertySource("classpath:com/gemdale/gmap/spring/boot/demo/el/test.properties")
public class ElConfig {
@Value("I love youe!")
private String nORMal;
@Value("#{systemProperties['os.name']}")
private String osName;
@Value("#{ T(java.lang.Math).random() * 100.0 }")
private double randomNumber;
@Value("#{demoService.another}")
private String fromAnother;
@Value("classpath:com/gemdale/gmap/spring/boot/demo/el/test.txt")
private Resource testFile;
private Resource testUrl;
@Value("${book.name}")
private String bookName;
@Autowired
private Environment environment;
@Bean
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"));
}
catch (Exception e) {
e.printStackTrace();
}
}
}
点击(此处)折叠或打开
public class Start {
public static void main(String[] args) throws Exception {
AnnotationConfigApplicationContext configApplicationContext = new AnnotationConfigApplicationContext(
ElConfig.class);
ElConfig resourceService = configApplicationContext.getBean(ElConfig.class);
resourceService.outputResource();
configApplicationContext.close();
}
}
book.author=GH
book.name=Spring Boot
感谢各位的阅读,以上就是“Spring EL怎么使用”的内容了,经过本文的学习后,相信大家对Spring EL怎么使用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!
--结束END--
本文标题: Spring EL怎么使用
本文链接: https://www.lsjlt.com/news/238209.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0