运行时的值注入

运行时注入Value

Spring提供了两种方式来进行运行时值注入:

  1. Property placeholders
  2. Spring Expression Language (SpEL)
1. 注入外部的Value
1.1 使用Spring提供的Environment来加载配置文件中的配置
package com.soundsystem;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

@Configuration
@PropertySource("classpath:/com/soundsystem/app.properties")
public class ExpressiveConfig {
    @Autowired
    Environment env;

    @Bean
    Declare a property source

    public BlankDisc disc() {
        return new BlankDisc(env.getProperty("disc.title"), 
                             env.getProperty("disc.artist"));
    } 
}

@PropertySource注解指明了配置文件的位置

1.2 解析PROPERTY PLACEHOLDERS
<bean id="sgtPeppers"
      class="soundsystem.BlankDisc"
      c:_title="${disc.title}"
      c:_artist="${disc.artist}" />
@Component
public class BlankDisc {
    public BlankDisc (
        @Value("${disc.title}") String title,
        @Value("${disc.artist}") String artist) {
      this.title = title;
      this.artist = artist;
  )
}

需要配置PropertyPlaceholderConfigurer bean或者PropertySourcesPlaceholderConfigurer bean

@Bean
public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
  return new PropertySourcesPlaceholderConfigurer();
}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">
  <context:property-placeholder />
</beans>
2. 使用Spring Expression Language

注意:SpEL expressions是被#{}包在里面的哟,不是{}({}是property placeholder的)

示例:

#{1}

#{T(System).currentTimeMillis()}

#{sgtPeppers.artist}

#{systemProperties['disc.title']}
@Component
public class BlankDisc {

    public BlankDisc(
        @Value("#{systemProperties['disc.title']}") String title,
        @Value("#{systemProperties['disc.artist']}") String artist) {
      this.title = title;
      this.artist = artist;
  )
}

<bean id="sgtPeppers"
      class="soundsystem.BlankDisc"
      c:_title="#{systemProperties['disc.title']}"
      c:_artist="#{systemProperties['disc.artist']}" />

运算符:


image.png

使用正则表达式:

#{admin.email matches '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.com'}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,268评论 19 139
  • 1.1 Spring IoC容器和bean简介 本章介绍了Spring Framework实现的控制反转(IoC)...
    起名真是难阅读 2,629评论 0 8
  • 本章内容: Spring profile 条件化的bean声明 自动装配与歧义性 bean的作用域 Spring表...
    谢随安阅读 1,222评论 0 5
  • 1.1 spring IoC容器和beans的简介 Spring 框架的最核心基础的功能是IoC(控制反转)容器,...
    simoscode阅读 6,786评论 2 22
  • 夜深听蔡健雅的《停格》,又引起阵阵感慨,就像歌词所说的爱情是个轮廓不可能私有,如果爱注定是场博弈,那我已丢盔弃甲。...
    F初见阅读 680评论 2 3