运行时值注入

注入外部的值

在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:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值