java文件与bean所定义的_springboot自定义属性文件与bean映射注入属性值

主要有几点:

一、导入依赖

springboot的包和:

org.springframework.boot

spring-boot-configuration-processor

版本在父类里统一管理了

true

一、配置自定义属性文件

1 rabbit.config.host=192.168.135.1292 rabbit.config.port=56723 rabbit.config.userName=guest4 rabbit.config.password=guest

二、在属性bean上的注解后期版本1.4以后主要是如下三个,且不需要在启动类上添加额外注解

@Component

@ConfigurationProperties(prefix="rabbit.config")

@PropertySource(value="classpath:config/rabbitmq.properties",encoding="utf-8")

也不需要在项目启动类上增加@EnableConfigurationProperties这个注解。

当然在打包的时候也要将该属性文件包含进来记得在pom文件的

下面添加包含进自定义的文件,否则找不到文件报错。

项目启动类代码:

1 packagecom.sharp.forward;2

3 importorg.mybatis.spring.annotation.MapperScan;4 importorg.slf4j.Logger;5 importorg.slf4j.LoggerFactory;6 importorg.springframework.boot.CommandLineRunner;7 importorg.springframework.boot.SpringApplication;8 importorg.springframework.boot.autoconfigure.EnableAutoConfiguration;9 importorg.springframework.boot.autoconfigure.SpringBootApplication;10

11 importcom.sharp.forward.config.RabbitMQProperties;12

13 @SpringBootApplication14 //@ImportResource("classpath:config/application-user-service-dubbo.xml")

15 @MapperScan(basePackages= {"com.sharp.forward.mapper"})16 @EnableAutoConfiguration17 public class Application implementsCommandLineRunner{18

19 private static final Logger log = LoggerFactory.getLogger(Application.class);20 public static voidmain(String[] args) {21 SpringApplication.run(Application.class, args);22 }23

24 /**

25 *@paramargs26 *@throwsException27 */

28 @Override29 public void run(String... args) throwsException {30 String config = "host: " +RabbitMQProperties.getHost()31 + ", config.port:" +RabbitMQProperties.getPort()32 + ", config.userName:" +RabbitMQProperties.getUserName();33

34 log.info("SpringBoot2.0实现自定义properties配置文件与JavaBean映射:" +config);35

36 }37

38 }

启动项目后打印如下:

INFO com.sharp.forward.Application - SpringBoot2.0实现自定义properties配置文件与JavaBean映射:host: null, config.port:0, config.userName:null

说明没有注入进来,然后在看我的属性bean类

1 /**

2 *3 */

4 packagecom.sharp.forward.config;5

6 importorg.springframework.boot.context.properties.ConfigurationProperties;7 importorg.springframework.context.annotation.Configuration;8 importorg.springframework.context.annotation.PropertySource;9 importorg.springframework.stereotype.Component;10

11 /**

12 *@author醉逍遥13 *14 */

15 @Component16 @ConfigurationProperties(prefix="rabbit.config")17 @PropertySource(value="classpath:config/rabbitmq.properties",encoding="utf-8")18 public classRabbitMQProperties {19

20 private staticString host;21

22 private static intport;23

24 private staticString userName;25

26 private staticString password;27

28 public staticString getHost() {29 returnhost;30 }31 public static voidsetHost(String host) {32 RabbitMQProperties.host =host;33 }34 public static intgetPort() {35 returnport;36 }37 public static void setPort(intport) {38 RabbitMQProperties.port =port;39 }40 public staticString getUserName() {41 returnuserName;42 }43 public static voidsetUserName(String userName) {44 RabbitMQProperties.userName =userName;45 }46 public staticString getPassword() {47 returnpassword;48 }49 public static voidsetPassword(String password) {50 RabbitMQProperties.password =password;51 }52

53 }

各属性和方法都是静态的,问题就出在这里,于是将静态的均修改掉如下

属性bean

1 /**

2 *3 */

4 packagecom.sharp.forward.config;5

6 importorg.springframework.boot.context.properties.ConfigurationProperties;7 importorg.springframework.context.annotation.PropertySource;8 importorg.springframework.stereotype.Component;9

10 /**

11 *@author醉逍遥12 *13 */

14 @Component15 @ConfigurationProperties(prefix="rabbit.config")16 @PropertySource(value="classpath:config/rabbitmq.properties",encoding="utf-8")17 public classRabbitMQProperties {18

19 publicString getHost() {20 returnhost;21 }22

23 public voidsetHost(String host) {24 this.host =host;25 }26

27 public intgetPort() {28 returnport;29 }30

31 public void setPort(intport) {32 this.port =port;33 }34

35 publicString getUserName() {36 returnuserName;37 }38

39 public voidsetUserName(String userName) {40 this.userName =userName;41 }42

43 publicString getPassword() {44 returnpassword;45 }46

47 public voidsetPassword(String password) {48 this.password =password;49 }50

51 privateString host;52

53 private intport;54

55 privateString userName;56

57 privateString password;58

59

60

61 }

启动类

1 packagecom.sharp.forward;2

3 importorg.mybatis.spring.annotation.MapperScan;4 importorg.slf4j.Logger;5 importorg.slf4j.LoggerFactory;6 importorg.springframework.beans.factory.annotation.Autowired;7 importorg.springframework.boot.CommandLineRunner;8 importorg.springframework.boot.SpringApplication;9 importorg.springframework.boot.autoconfigure.EnableAutoConfiguration;10 importorg.springframework.boot.autoconfigure.SpringBootApplication;11

12 importcom.sharp.forward.config.RabbitMQProperties;13

14 @SpringBootApplication15 //@ImportResource("classpath:config/application-user-service-dubbo.xml")

16 @MapperScan(basePackages= {"com.sharp.forward.mapper"})17 @EnableAutoConfiguration18 public class Application implementsCommandLineRunner{19

20 private static final Logger log = LoggerFactory.getLogger(Application.class);21 public static voidmain(String[] args) {22 SpringApplication.run(Application.class, args);23 }24

25 @Autowired26 privateRabbitMQProperties rabbitMQProperties;27 /**

28 *@paramargs29 *@throwsException30 */

31 @Override32 public void run(String... args) throwsException {33 String config = "host: " +rabbitMQProperties.getHost()34 + ", config.port:" +rabbitMQProperties.getPort()35 + ", config.userName:" +rabbitMQProperties.getUserName();36

37 log.info("SpringBoot2.0实现自定义properties配置文件与JavaBean映射:" +config);38

39 }40

41 }

再次启动如下:

132b2f27ed81037a9c440da5c90e3eee.png

说明属性值已经读取。

同样将属性bean修改为如下也不能在在初始化中为静态变量赋值

1 /**

2 *3 */

4 packagecom.sharp.forward.config;5

6 importorg.springframework.beans.factory.annotation.Value;7 importorg.springframework.boot.context.properties.ConfigurationProperties;8 importorg.springframework.context.annotation.Configuration;9 importorg.springframework.context.annotation.PropertySource;10 importorg.springframework.stereotype.Component;11

12 /**

13 *@author醉逍遥14 *15 */

16 @Component17 @ConfigurationProperties(prefix="rabbit.config")18 @PropertySource(value="classpath:config/rabbitmq.properties",encoding="utf-8")19 public classRabbitMQProperties {20

21 private staticString host;22

23 private static intport;24

25 private staticString userName;26

27 private staticString password;28

29 public staticString getHost() {30 returnhost;31 }32 @Value(value="${host}")33 public static voidsetHost(String host) {34 RabbitMQProperties.host =host;35 System.out.println("host----------->"+host);36 }37 public static intgetPort() {38 returnport;39 }40 @Value(value="${rabbit.config.port}")41 public static void setPort(intport) {42 RabbitMQProperties.port =port;43 System.out.println("port----------->"+port);44 }45 public staticString getUserName() {46 returnuserName;47 }48 @Value(value="${userName}")49 public static voidsetUserName(String userName) {50 RabbitMQProperties.userName =userName;51 }52 public staticString getPassword() {53 returnpassword;54 }55 public static voidsetPassword(String password) {56 RabbitMQProperties.password =password;57 }58

59 }

运行结果同样都是空或0;

参考https://www.cnblogs.com/hsz-csy/p/9625950.html,可以解决为静态变量赋值的问题,set方法一定要是非静态的

修改为

/****/

packagecom.sharp.forward.config;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.boot.context.properties.ConfigurationProperties;importorg.springframework.context.annotation.Configuration;importorg.springframework.context.annotation.PropertySource;importorg.springframework.stereotype.Component;/***@author醉逍遥

**/@Component

@ConfigurationProperties(prefix="rabbit.config")

@PropertySource(value="classpath:config/rabbitmq.properties",encoding="utf-8")public classRabbitMQProperties {private staticString host;private static intport;private staticString userName;private staticString password;public staticString getHost() {returnhost;

}

@Value(value="${host}")public static voidsetHost(String host) {

RabbitMQProperties.host=host;

System.out.println("host----------->"+host);

}public static intgetPort() {returnport;

}

@Value(value="${rabbit.config.port}")public void setPort(intport) {

RabbitMQProperties.port=port;

System.out.println("port----------->"+port);

}public staticString getUserName() {returnuserName;

}

@Value(value="${userName}")public voidsetUserName(String userName) {

RabbitMQProperties.userName=userName;

}public staticString getPassword() {returnpassword;

}public static voidsetPassword(String password) {

RabbitMQProperties.password=password;

}

}

其他不变,

com.sharp.forward.Application - SpringBoot2.0实现自定义properties配置文件与JavaBean映射:host: null, config.port:5672, config.userName:guest

host没取到是因为value中路径写个重复前缀的实验用了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值