spring boot 注入配置文件属性组装Bean的三种方式

625 阅读1分钟

1、通过注解@Value对于每个来设置,这种情况默认读取的是application-*.properties的文件内容,例如 例如设置启动来自dev模式

image.png

image.png

package com.esoon.ele.web.config;/**
 * Created by burns.
 *
 * @author <a href="http://www.esoon-soft.com/">burns</a>
 * @date 2021/06/24 11:32
 */

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * TODO
 *
 * @ClassName appModel
 * @Author Burns
 * @DAte 2021/6/24 11:32
 */
@Component
@Data
public class AppModel {

    @Value("${app.name}")
    private String appName;

    @Value("${app.version}")
    private String appVersion;

    @Value("${app.info}")
    private String appInfo;

    @Value("${app.number}")
    private String appNumber;

}

2、第二种方式是,java类的属性和application-*.properties中的除了前缀其他都一致,配置文件中的配置前会在java类注解上加以定义

image.png

package com.esoon.ele.web.config;/**
 * Created by burns.
 *
 * @author <a href="http://www.esoon-soft.com/">burns</a>
 * @date 2021/06/24 13:51
 */

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * TODO
 *
 * @ClassName HomeProperties
 * @Author Burns
 * @DAte 2021/6/24 13:51
 */
@Data
@Component
@ConfigurationProperties(prefix = "home")
public class HomeProperties {
    /**
     * 省份
     */
    private String province;
    /**
     * 城市
     */
    private String city;
    /**
     * 描述
     */
    private String desc;
}

3、第三种方式是从其他普通配置文件中读取,可以是类路径下也可以是其他普通路径下例如

image.png


/**  
 * @Title: ConfigProperties.java
 * @Package com.acconsys.ids.util
 * @Description: TODO(用一句话描述该文件做什么)
 * @author 35725
 * @date 2020年1月8日 上午9:20:40 
 * @version V1.0  
 */

package com.esoon.ids.common;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * @ClassName: ConfigProperties
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author 35725
 * @date 2020年1月8日
 *
 */
@Component
//@PropertySource(value = "classpath:config/config.properties")
@PropertySource(value="file:${cis.properties}")
public class CisProperties {
	@Value("${cis.ip}")
	private String ip;
	
	@Value("${cis.port}")
	private int port;
	
	@Value("${cis.username}")
	private String username;
	
	@Value("${cis.password}")
	private String password;
	

	

	/**
	 * 使用@value注解注入properties中的属性 1. 在类名上面使用 @PropertySource("classpath:*")
	 * 注解,*代表属性文件路径,可以指向多个配置文件路径
	 * 如果是多个配置文件,则是 @PropertySource({"classpath:*","classpath:*"....}) 2.
	 * 在字段上直接使用@value注解 3. 注解内使用${core.pool.size} core.pool.size 代表属性文件里面的key 5.
	 * 需要新增 PropertySourcesPlaceholderConfigurer 的 bean 6. 在
	 * PropertySourcesPlaceholderConfigurer 增加@bean注解,申明返回的是一个bean,否则会注入失败
	 *
	 */


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

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}