- application.properties
yaml
spring.application.name=demo
app.config.upload-addr=baidu.com
app.config.app-key=1234
- AppConfigProperties
java
import lombok.Data;
@Data
public class AppConfigProperties {
private String uploadAddr;
private String appKey;
}
- UploadComponent
java
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
@Data
@Slf4j
public class UploadComponent {
private AppConfigProperties appConfigProperties;
public void upload(){
log.info("app.config:{}",appConfigProperties);
}
}
- AppConfiguration
java
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class AppConfiguration {
//使用 @Value 注解注入配置文件中的spring.application.name属性的值到appName字段
@Value("${spring.application.name}")
public String appName;
//定义一个Bean,RestTemplate的实例将会被Spring管理,BeanID=restTemplate
@Bean
public RestTemplate restTemplate() {return new RestTemplate();}
//定义一个Bean,AppConfigProperties的实例将会被Spring管理
//使用@ConfigurationProperties注解,将配置文件中以"app.config"为前缀的配置项自动绑定到AppConfigProperties
@Bean
@ConfigurationProperties(prefix = "app.config")
public AppConfigProperties appConfigProperties() {return new AppConfigProperties();}
@Bean
public AppConfigProperties appConfigPropertiesNon() {return new AppConfigProperties();}
//定义一个Bean UploadComponent的实例会被Spring管理
//通过@Qualifier注解指定注入的appConfigProperties Bean的名称
//确保注入的是我们刚刚定义的appConfigProperties Bean
//在UploadComponent实例被创建后
//设置其AppConfigProperties属性为我们注入的AppConfigProperties实例
@Bean
public UploadComponent uploadComponent(@Qualifier("appConfigProperties") AppConfigProperties properties) {
UploadComponent uploadComponent = new UploadComponent();
uploadComponent.setAppConfigProperties(properties);
return uploadComponent;
}
}
- UploadController
java
import com.example.demo.config.UploadComponent;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RestController
public class UploadController {
@Resource
private RestTemplate restTemplate;
@Resource
private UploadComponent uploadComponent;
@GetMapping("/uploader")
public void upload() {uploadComponent.upload();}
}