自定义配置的变量名:
在 *.properties 里面定义特定的变量
server.port=9000
amazon.associateId=habuma-20
建立Properties文件制定特定的变量
@Component //指定配置文件的地址 @PropertySource("classpath:application.properties") //指定配置文件的前缀 @ConfigurationProperties(prefix = "amazon") public class AmazonProperties { public String getAssociateId() { return associateId; } //直接根据properties里面的变量名赋值 //等值于 @Value public void setAssociateId(String associateId) { this.associateId = associateId; } private String associateId; }
Demo文件直接调用获取即可或得到AssociatedId的值
@SpringBootApplication @EnableConfigurationProperties public class DemoApplication { private static AmazonProperties amazonProperties; @Autowired private void setAmazonProperties(AmazonProperties amazonProperties){ this.amazonProperties = amazonProperties; } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); System.out.println(amazonProperties.getAssociateId()); } }