13点睛Spring4.1-Spring EL

13.1 Spring EL

  • Spring EL-Spring表达式语言,支持在xml和注解中使用表达式,类似jsp的EL表达式语言;
  • 本教程关注于在注解中使用Spring EL;
  • Spring EL包含很多类型的表达式,本教程关注常用的注入
    • 获得系统属性
    • 注入表达式
    • 注入文件
    • 注入其他bean或者其属性
    • 注入properties文件属性
    • 注入普通字符

13.2 示例

13.2.1 编写Spring EL演示类

@Configuration
@PropertySource("classpath:com/wisely/springel/test.properties")
public class DemoService {
    @Value("#{systemProperties}")
    private Properties systemProperties;

    @Value("#{systemProperties['os.name']}")
    private String osName;

    @Value("#{ T(java.lang.Math).random() * 100.0 }")
    private double randomNumber;

    @Value("classpath:com/wisely/springel/info.txt")
    private Resource info;

    @Value("#{demoBean.another}")
    private String fromAnother;
    //注意注入properties使用$而不是#,且需要声明propertyConfigure,在下面的@Bean
    @Value("${wisely.name}")
    private String myName;

    @Value("I love iteye and github")
    private String normal;
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyConfigure() {
        return new PropertySourcesPlaceholderConfigurer();
    }

//getter

}

13.2.2 编写java类测试注入其他bean的属性

package com.wisely.springel;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class DemoBean {
    @Value("其他类的属性")
    private String another;

    public String getAnother() {
        return another;
    }

    public void setAnother(String another) {
        this.another = another;
    }

}

13.2.3 新建测试txt和properties

1234
wisely.name = wyf

13.2.3 测试

package com.wisely.springel;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext("com.wisely.springel");
        DemoService ds = context.getBean(DemoService.class);
        System.out.println("注入操作系统属性:"+ds.getSystemProperties());
        System.out.println("注入操作系统名称: "+ds.getOsName());
        System.out.println("注入随机数: "+ds.getRandomNumber());
        System.out.println("注入文件 "+ds.getInfo().getFilename());
        System.out.println("注入其他bean属性: "+ds.getFromAnother());
        System.out.println("注入properties文件的属性: "+ds.getMyName());
        System.out.println("注入普通字符 "+ds.getNormal());
        context.close();

    }

}

输出结果:

注入操作系统属性:{java.runtime.name=Java(TM) SE Runtime Environment......}
注入操作系统名称: Windows 8.1
注入随机数: 15.342237263367908
注入文件 info.txt
注入其他bean属性: 其他类的属性
注入properties文件的属性: wyf
注入普通字符 I love iteye and github

新书推荐《JavaEE开发的颠覆者: Spring Boot实战》,涵盖Spring 4.x、Spring MVC 4.x、Spring Boot企业开发实战。

 

京东地址:http://item.jd.com/11894632.html

当当地址:http://product.dangdang.com/23926195.html

亚马逊地址:http://www.amazon.cn/图书/dp/B01D5ZBFUK/ref=zg_bsnr_663834051_6 

淘宝地址:https://item.taobao.com/item.htm?id=528426235744&ns=1&abbucket=8#detail

 

 

 

或自己在京东、淘宝、亚马逊、当当、互动出版社搜索自选。

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值