配置文件 src/main/resources 目录下: book.properties
yuyi.book.name=三国演义
yuyi.book.count=100
一、使用@Value 注解注入属性
package com.yuyi.bean.model;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
/**
 *  使用 @Value 注解注入属性值 
 */
@Component
public class BookValue {
    
    @Value("${yuyi.book.name}")
    private String name;
    
    @Value("${yuyi.book.count}")
    private Integer count;
 
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getCount() {
        return count;
    }
    public void setCount(Integer count) {
        this.count = count;
    }
 
}
二、使用 Environment 获取配置信息
package com.yuyi.bean.model;
 
import java.io.UnsupportedEncodingException;
 
import javax.annotation.PostConstruct;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
 
/**
 * 使用 Environment 类获取配置信息
 */
@Component
public class BookEnvironment {
    
    @Autowired
    private Environment env;
 
    private String name;
    
    private Integer count; 
    
    @PostConstruct //注解标注该方法在构造器调用之后自动执行
    public void init() throws UnsupportedEncodingException{
        String strname = env.getProperty("yuyi.book.name");
        String strcount = env.getProperty("yuyi.book.count");
        
        name = strname;
        count = Integer.parseInt(strcount);
    }
 
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getCount() {
        return count;
    }
    public void setCount(Integer count) {
        this.count = count;
    }
    
}
三、使用 @ConfigurationProperties 注解注入配置信息
package com.yuyi.bean.model;
 
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
 
/**
 * 使用 @ConfigurationProperties注入配置信息
 */
@Component
@ConfigurationProperties(prefix = "yuyi.book")
@PropertySource(value = "classpath:book.properties", encoding = "UTF-8")  //配置文件位置, 编码方式
public class BookProperties {
    
    private String name;
    
    private Integer count;
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
    public Integer getCount() {
        return count;
    }
    public void setCount(Integer count) {
        this.count = count;
    }
 
注意:
@ConfigurationProperties与静态加载配置文件
因为@ConfigurationProperties只会调用 非静态的set方法,所以我们稍微做一下改动,set方法都换成非静态的就可以了,贴下正确的代码:
@Component
@ConfigurationProperties(prefix = "spring.redis")
public class RedisProperties {
    private static String host;
    private static int port;
    public static String getHost() {
        return host;
    }
    public void setHost(String host) {
        RedisProperties.host = host;
    }
    public static int getPort() {
        return port;
    }
    public void setPort(int port) {
        RedisProperties.port = port;
    }
}
更多内容请访问:IT源点
注意:本文归作者所有,未经作者允许,不得转载