SpringBoot-屬性直接注入
上面我們說到,如果公共的屬性,我們可以使用Java類加載Properties文件,來達到復用的目的,在SpringBoot中,我們提供了更為簡單的方法,即直接使用配置文件注入。
-
Properties文件中的key(如果有前綴則是前綴后的值 )必須和屬性名一致
-
在需要使用到Properties屬性文件中屬性的方法上加上@ConfigurationProperties(“prefix=xxx”)
-
一定要提供要注入類的getter、setter方法
packagecn.rayfoo.config;
importcom.alibaba.druid.pool.DruidDataSource;
importlombok.Data;
importlombok.ToString;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.beans.factory.annotation.Value;
importorg.springframework.boot.context.properties.ConfigurationProperties;
importorg.springframework.boot.context.properties.EnableConfigurationProperties;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.context.annotation.PropertySource;
importjavax.sql.DataSource;
/**
* @author 張瑞豐
* @description
* @date 2019/11/4
*/