运行时值注入

来源:互联网 发布:fileinput.js 参数 编辑:程序博客网 时间:2024/05/20 03:42

注入外部的值

在Spring中,处理外部值的最简单方式就是声明属性源并通过Spring的Environment来检索属性。

使用@PropertySource注解和Environment

程序清单:

package com.lf.config;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;/** * Created by LF on 2017/5/3. */@Configuration@PropertySource("classpath:application.properties")public class Config {    @Autowired    private Environment environment;    @Bean    public String getTitle() {        return environment.getProperty("title") + environment.getProperty("context");    }}
title=标题context=内容

测试代码:

package com.lf;import com.lf.config.Config;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/** * Unit test for simple App. */@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes = Config.class)public class AppTest {    @Autowired    private String string;    @Test    public void get() {        System.err.println("==========title"+string);    }}

工程结构
这里写图片描述

学习Spring的Environment

getProperty()方法有四个重载的变种形式:

    String getProperty(String key);    String getProperty(String key, String defaultValue);        <T> T getProperty(String key, Class<T> targetType);    <T> T getProperty(String key, Class<T> targetType, T defaultValue);

前两种形式的getProperty()方法都会返回String类型的值。
第二种在指定属性不存在的时候,会使用一个默认值。

剩下的两种getProperty()方法与前面的两种非常类似,但是它们不会将所有的值都视为String类型。

假设你想要获取的值所代表的含义是连接池中所维持的连接数量。如果我们从属性文件中得到
的是一个String类型的值,那么在使用之前还需要将其转换为Integer类型。但是,如果使用重载形式的getProperty()的话,就能非常便利地解决这个问题。

最后,如果想将属性解析为类的话,可以使用getPropertyAsClass()方法。

除了属性相关的功能以外,Environment还提供了一些方法来检查哪些profile处于激活状态:

  1. String[] getActiveProfiles():返回激活profile名称的数组;
  2. String[] getDefaultProfiles():返回默认profile名称的数组;
  3. boolean acceptsProfiles(String… profiles):如果environment支持给定profile的话,就返回true。

@Value注解

Spring一直支持将属性定义到外部的属性的文件中,并使用占位符值将其插入到Spring bean中。在Spring装配中,占位符的形式为使用“${… }”包装的属性名称。作为样例,我们可以在XML中按照如下的方式解析BlankDisc构造器参数:
这里写图片描述

为了使用占位符,我们必须要配置一个PropertyPlaceholderConfigurer bean或PropertySourcesPlaceholderConfigurer bean。从Spring3.1开始,推荐使用PropertySourcesPlaceholderConfigurer,因为它能够基于Spring Environment及其属性源来解析占位符。

package com.lf.config;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.context.support.PropertySourcesPlaceholderConfigurer;import org.springframework.core.env.Environment;/** * Created by LF on 2017/5/3. */@Configuration@PropertySource("classpath:application.properties")public class Config {    @Bean    public PropertySourcesPlaceholderConfigurer get() {        return new PropertySourcesPlaceholderConfigurer();    }}
package com.lf;import com.lf.config.Config;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/** * Unit test for simple App. */@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes = Config.class)public class AppTest {    @Value("${title}")    private String string;    @Test    public void get() {        System.err.println("==========title" + string);    }}

如果你想使用XML配置的话,Spring context命名空间中的元素将会为你生成PropertySourcesPlaceholderConfigurer bean:

0 0
原创粉丝点击