Spring EL表达式

    Spring EL-Spring表达式语言,支持在xml和注解中使用表达式,类似于在jsp的EL表达式语言。

    Spring 开发中经常涉及调用各种资源的情况,包含普通文件、网址、配置文件、系统环境变量等,我们可以使用Spring的表达式语言实现资源的注入。

    Spring主要在注解@Value的参数中使用表达式语言。

  • 注入普通字符
  • 注入操作系统属性
  • 注入表达式运算结果
  • 注入其他Bean的属性
  • 注入文件内容
  • 注入网址内容
  • 注入属性文件

示例

  1. 在类路径下新建一个text.txt文件,内容随意,在类路径下新建一个test.propertes文件,内容如下:
book.author=xxx
book.name= spring boot
  1. 编写一个需要注入的Bean
	@Service
public class DemoService {
@Value("其他类中的属性") // 此处为注入普通字符串
private String another; public String getAnother(){
return another;
} public void setAnother(String anther){
this.another = anther;
}
}
  1. 配置类
	@Configuration
@ComponentScan("xxx.xx.xx")
@PropertySource("classpath:test.properties")
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
private String fromAnother; @Value("classpath:test.txt") // 5
private Resource testFile; @Value("http://www.baidu.com") // 6
private Resource testUrl; @Value("${book.name}")
private String bookName; @Bean // 7
public static PropertySourcesPlaceholderConfigurer propertyConfigure(){
return new PropertySourcesPlaceholderConfigurer();
} ...
setter and getter function
...
}
代码解释
  1. 注入普通字符串

  2. 注入操作系统属性

  3. 注入表达式结果

  4. 注入其他Bean属性

  5. 注入文件资源,文件资源可以是classpath路径下的也可以是系统文件目录下的

  6. 注入网址资源

  7. 注入配置文件中的资源,配置文件使用@PropertySource注解在类上标注

    注入配置件需要使用@PropertySource指定文件地址,若使用@Value注入,则要配置一个PropertySourcesPlaceholderConfigurer的Bean,@Value("${book.name}")使用的是"$"而不是"#"

Profile

    Profile为不同环境下使用不同的配置提供了支持(开发环境和生成环境)

  1. 通过设定Environment的ActiveProfiles类设定当前context需要使用的配置。在开发中使用@Profile注解类或者方法,达到不同情况下选择实例化不同的Bean。
  2. 通过设定JVM的spring.profiles.active参数来设计配置环境。
  3. Web项目设置在Servlet的context parameter中。
	<-- Servlet 2.5及以下 -->
<serlvet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>spring.profiles.active</param-name>
<param-value>prod</param-value>
</init-param>
</servlet>
	// servlet 3.0 以上
public class WebInit implements WebApplicationInitializer {
public void onStartup(ServletContext container) throws ServletException {
container.setInitParameter("spring.profiles.default", "dev");
}
}

示例

  1. 示例Bean
	public class DemoBean {
private String content;
public DemoBean(String content){
this.content = content;
}
public String getContent(){
return content;
}
}
  1. Profile配置
	@Configuration
public class ProfileConfig {
@Bean
@Profile("dev") // 1
public DemoBean devDemoBean(){
return new DemoBean("from development profile");
} @Bean
@Profile("prod") // 2
public DemoBean prodDemoBean() {
return new DemoBean("from production profile");
}
}
代码解释
  1. profile为dev时示例化devDemoBean

  2. profile为prod时实例化为prodDemoBean

  3. 运行

public class Main{
public static void main(String[] args){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.getEnvironment().setActiveProfiles("prod"); // 1
context.register(ProfileConfig.class); // 2
context.refresh(); // 3 DemoBean demoBean = context.getBean(DemoBean.class);
System.out.println(demoBean.getContext()); context.close();
}
}
说明
  1. 先将活动的Profile设置为prod
  2. 后置注册Bean配置类,不然会报Bean未定义的错误
  3. 刷新容器

Spring EL表达式和资源调用的更多相关文章

  1. java MVEL2/Spring EL表达式、直接调用、反射性能实测

    import java.io.Serializable; import java.lang.reflect.Field; import java.util.HashMap; import java.u ...

  2. 把功能强大的Spring EL表达式应用在.net平台

    Spring EL 表达式是什么? Spring3中引入了Spring表达式语言—SpringEL,SpEL是一种强大,简洁的装配Bean的方式,他可以通过运行期间执行的表达式将值装配到我们的属性或构 ...

  3. Spring 在 xml配置文件 或 annotation 注解中 运用Spring EL表达式

    Spring  EL 一:在Spring xml 配置文件中运用   Spring EL Spring EL 采用 #{Sp Expression  Language} 即 #{spring表达式} ...

  4. 使用spring EL表达式+自定义切面封装缓存模块

    需求是这样的,业务代码需要使用到缓存功能以减少数据库压力,使用redis来实现,并且需要生成缓存的key由方法的传参拼接而成(貌似也只能这样才能保证同样的select查询可以使用缓存),简单的方式就是 ...

  5. 解决spring el表达式不起作用

    el表达式不起作用,如下图所示 现象: 在显示页面中加入: <%@ page isELIgnored="false" %>就OK了 参考:http://bbs.csdn ...

  6. spring EL表达式,null-safe表达式

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://ww ...

  7. Spring整合Shiro并扩展使用EL表达式

    Shiro是一个轻量级的权限控制框架,应用非常广泛.本文的重点是介绍Spring整合Shiro,并通过扩展使用Spring的EL表达式,使@RequiresRoles等支持动态的参数.对Shiro的介 ...

  8. Jsp内置对象及EL表达式的使用

    一.JSP的内置对象(9个JSP内置对象) JSP的内置对象引用名称 对应的类型 request HttpServletRequest response HttpServletResponse ses ...

  9. EL表达式的语法介绍及九大隐含对象

    一. 简介 > JSP表达式 <%= %> 用于向页面中输出一个对象. > 到JSP2.0时,在我们的页面中不允许出现 JSP表达式和 脚本片段. > 使用EL表达式来代 ...

随机推荐

  1. 51nod 1572 宝岛地图 (预处理四个方向的最大步数优化时间,时间复杂度O(n*m+k))

    题目: 这题如果没有时间限制的话暴力可以解,暴力的话时间复杂度大概是O(k*n),1s的话非常悬. 所以我们需要换个思路,我们对每个点预处理四个方向最多能走的步数,这个预处理时间复杂度是O(n*m). ...

  2. 为什么同样的数据,俩人生成的obj和bin文件不一样

    http://bbs.csdn.net/topics/270055083 编译器编译的时候可能有些东西依赖时间,或许是优化的原因,如果可以,换个编译器试试,或许两次编译的时候,强制把系统时间调成一个看 ...

  3. vmware workstation中的NAT配置

    宿主机:win10: IP:192.168.1.101 GW:192.168.1.1 以太网2(VMNET8) IP:192.168.100.1 GW:nonevmware中的虚拟网络设置(NAT): ...

  4. 待解决问题 oc

    读书破万卷 Associated Object hash实现 Dynamic Method Resolution Message Forwarding forwardingTargetForSelec ...

  5. 洛谷P1136 迎接仪式 动态规划

    显然,这是一道动归题. 我们发现,每次交换时只可能交换不同的字母(交换同类字母显然是没有意义的).那么每次交换等同于将 111 个 "j""j""j& ...

  6. python学习笔记第三章

    1.列表是由一系列特定顺序排列得元素组成,在python中用([])组成,列表得索引是从0开始,不是1 xu=['a','b',''c] print(xu) 访问列表的元素第一个元素 xu=['a', ...

  7. salt 安装kubernetes集群3节点

    [root@linux-node1 k8s]# tree .├── etcd.sls├── files│   ├── cfssl-1.2│   │   ├── cfssl-certinfo_linux ...

  8. [置顶] Docker学习总结(7)——云端基于Docker的微服务与持续交付实践

    本文根据[2016 全球运维大会•深圳站]现场演讲嘉宾分享内容整理而成 讲师简介 易立 毕业于北京大学,获得学士学位和硕士学位:目前负责阿里云容器技术相关的产品的研发工作. 加入阿里之前,曾在IBM中 ...

  9. Eclipse WEB项目更改项目名

    可能很多人都碰到过 WEB 项目需要改名字的事情,不过不是每个人都能很顺畅地完成这个事情.这里简单给大家介绍两种改项目名的方法 1. 在Eclipse 中修改项目名 没错这种方法跟你预料的一样简单,当 ...

  10. Linux下安装过程中编译PHP时报错:configure: error: libjpeg.(a|so) not found

    在Linux下安装PHP过程中,编译时出现configure: error: libjpeg.(a|so) not found 错误的解决的方法: 检查之后发现已经安装libjpeg.可是/usr/l ...