這裏記錄一種spring boot讀取配置文件的方式,不討論@Value的方式了,那個太簡單了
假設我們Yml中有配置:
##### 自定義隊列信息
user:
mq:
log:
queue: logQueue
exchange: logExchange
routingKey: logRk
mail:
queue: mailQueue
exchange: mailExchange
routingKey: mailRk
新建已給類來接收
package com.wm.contextinit.config;
import lombok.Data;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import java.util.Map;
/***
* @ClassName: UserMqProperties
* @Description:
* @Author: wm_yu
* @Create_time: 14:12 2019-12-18
*/
@ConfigurationProperties("user.mq")
@Data
@ToString
public class UserMqProperties {
private Map<String,String> log;
private Map<String,String> mail;
@Bean("logMqProperties")
public MqProperties logMqProperties(){
return new MqProperties(log.get("queue"),log.get("exchange"),log.get("routingKey"));
}
@Bean("mailMqProperties")
public MqProperties mailMqProperties(){
return new MqProperties(mail.get("queue"),mail.get("exchange"),mail.get("routingKey"));
}
}
上圖是用map接收log/mail下的屬性
在定義一個類
package com.wm.contextinit.config;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.ToString;
/***
* @ClassName: MqProperties
* @Description:
* @Author: wm_yu
* @Create_time: 14:25 2019-12-18
*/
@Data
@AllArgsConstructor
@ToString
public class MqProperties {
private String queue;
private String exchange;
private String routingKey;
}
在使用的地方使用註解:
@EnableConfigurationProperties({UserMqProperties.class})
如下:
package com.wm.contextinit.service;
import com.wm.contextinit.config.MqProperties;
import com.wm.contextinit.config.UserMqProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;
/***
* @ClassName: TestService
* @Description:
* @Author: wm_yu
* @Create_time: 14:19 2019-12-18
*/
@Service
@Slf4j
@EnableConfigurationProperties({UserMqProperties.class})
public class TestService {
@Autowired
private UserMqProperties userMqProperties;
@Autowired
private MqProperties logMqProperties;
@Autowired
private MqProperties mailMqProperties;
public void test(){
log.info("userMqProperties:[{}],logMqProperties:[{}],mailMqProperties:[{}]",userMqProperties.toString(),logMqProperties.toString(),mailMqProperties.toString());
}
}
測試如下:
其實還可以更簡單,如下:
直接在UserMqProperties上添加註解@Compment 註冊到ioc
package com.wm.contextinit.config;
import lombok.Data;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import java.util.Map;
/***
* @ClassName: UserMqProperties
* @Description:
* @Author: wm_yu
* @Create_time: 14:12 2019-12-18
*/
@ConfigurationProperties("user.mq")
@Data
@ToString
@Component
public class UserMqProperties {
private Map<String,String> log;
private Map<String,String> mail;
@Bean("logMqProperties")
public MqProperties logMqProperties(){
return new MqProperties(log.get("queue"),log.get("exchange"),log.get("routingKey"));
}
@Bean("mailMqProperties")
public MqProperties mailMqProperties(){
return new MqProperties(mail.get("queue"),mail.get("exchange"),mail.get("routingKey"));
}
}
在使用的類上面,去掉註解
@EnableConfigurationProperties({UserMqProperties.class})
如下:
package com.wm.contextinit.service;
import com.wm.contextinit.config.MqProperties;
import com.wm.contextinit.config.UserMqProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/***
* @ClassName: TestService
* @Description:
* @Author: wm_yu
* @Create_time: 14:19 2019-12-18
*/
@Service
@Slf4j
public class TestService {
@Autowired
private UserMqProperties userMqProperties;
@Autowired
private MqProperties logMqProperties;
@Autowired
private MqProperties mailMqProperties;
public void test(){
log.info("userMqProperties:[{}],logMqProperties:[{}],mailMqProperties:[{}]",userMqProperties.toString(),logMqProperties.toString(),mailMqProperties.toString());
}
}
再次測試,結果一樣的,關鍵點就是使用Map來接收這個屬性
引用別人的接收數據的幾種情況