Spring EL

在第一节说Spring的配置的时候提到过,有的配置是可以使用注解或者Java的配置的,有些配置像数据库配置是需要放到配置文件中的。那么使用Java配置的情况下,又要对数据库进行配置该怎么办呢?我们一般的做法就是创建一个properties配置文件,使用JDK提供的Properties类可以方便的读取文件内容。如果使用SpringEL,你会发现,读取配置就更简单了。当然了,SpringEL并不仅仅是用来读取文件的,他主要的作用是给属性注入值。只是在有些情况下,属性的值是通过配置文件读取出来的,使用SpringEL也可以方便的注入。

Spring EL是Spring的表达式语言,类似于我们JSP中的EL表达式。使用Spring EL可以方便的进行取值操作。Spring EL可以用在xml配置中或者注解中。我们以@Value注解为例来使用Spring EL给属性注入值。这里因为要用到@Value这个注解。这个注解是做什么用的呢,其实就是给属性注入值的。

我们先来看一下@Value的源码,@Value不仅仅可以放到属性上,还可以放到方法上,放到方法的参数上,还可以放到注解上,有一个value属性可以使用我们的Spring EL表达式。

@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Value {

     /**
      * The actual value expression: e.g. "#{systemProperties.myProp}".
      */
     String value();

}

举个简单的例子,看一下使用@Value来注入一个字符串常量值

@Service
public class DemoService {

     @Value("注入的属性值")
     private String another;

     public String getAnother() {
          return another;
     }

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

}

通过@Value就可以把指定的值注入到another属性中。这就类似与定义了一个字符串类型的变量,并赋予初始值为“注入的属性值”。这样对我们来说似乎意义不大,完全可以通过指定默认值来实现,那么还有哪些强大的功能呢,下面结合Spring EL来实现下面七种类型的注入

  1. 普通字符串
  2. 操作系统属性或者jvm的属性
  3. 运算结果
  4. 其他的bean的属性
  5. 文件
  6. url
  7. 属性文件

下面我们来定义一个bean,并定义一些属性,通过@Value来注入以上类型的值

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

// 这里要使用@Value,需要配置 PropertySourcesPlaceholderConfigurer 这个bean,在后面的ElConfig中进行配置
@Component
public class DemoBean {

     @Value("hello")
     private String text; // 注入一个普通字符串

     @Value("#{systemProperties['os.name']}")
     private String osName; // 注入操作系统属性

     @Value("#{demoService.another}")
     private String fromAnother; // 注入其他的bean的属性

     @Value("#{T(java.lang.System).currentTimeMillis()}")
     private Long date; // 注入计算的值

     @Value("classpath:com/hy/spring/test4/xx.txt")  // 这里依赖一个文件
     private Resource testFile; // 注入文件

     @Value("http://www.baidu.com")
     private Resource testUrl; // 注入url

     @Value("${test.app}")  // 这里也依赖一个文件,依赖的文件需要在Java配置中进行配置
     private String appName; // 注入配置文件中test.app的值

     @Autowired
     private Environment env; // 绑定环境量

     public void outputResource() {
          System.out.println(text);

          System.out.println(osName);

          System.out.println(randomNumber);

          System.out.println(fromAnother);

          System.out.println(testFile);

          System.out.println(date);

          System.out.println(testUrl);

          System.out.println(appName);

          System.out.println(env.getProperty("test.name")); // 从环境变量中获取配置文件的值

     }
}

再来配置

package com.hy.spring.test4;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

@Configuration
@ComponentScan("com.hy.spring.test4")
@PropertySource("classpath:com/hy/spring/test4/test.properties") // 这里就是指定所使用的配置文件
public class ElConfig {

     @Bean
     public static PropertySourcesPlaceholderConfigurer propertyConfigure() {// 这里配置的是@Value所依赖的bean
          return new PropertySourcesPlaceholderConfigurer();
     }

}

测试代码

package com.hy.spring.test4;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {

     public static void main(String[] args) {
          AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ElConfig.class);

          DemoBean config = context.getBean(DemoBean.class);

          config.outputResource();

          context.close();
     }

}

运行结果

七月 18, 2017 6:13:31 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@6e2c634b: startup date [Tue Jul 18 18:13:31 CST 2017]; root of context hierarchy
hello
Windows 10
89.36539160311565
注入的属性值
class path resource [com/hy/spring/test4/xx.txt]
1500372812527
URL [http://www.baidu.com]
hello
mytest
七月 18, 2017 6:13:32 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@6e2c634b: startup date [Tue Jul 18 18:13:31 CST 2017]; root of context hierarchy
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值