springboot自定义属性文件与bean映射注入属性值
主要有几点:
一、导入依赖
springboot的包和:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
版本在父类里统一管理了
<optional>true</optional>
</dependency>
一、配置自定义属性文件
1 rabbit.config.host=192.168.135.129 2 rabbit.config.port=5672 3 rabbit.config.userName=guest 4 rabbit.config.password=guest
二、在属性bean上的注解后期版本1.4以后主要是如下三个,且不需要在启动类上添加额外注解
@Component
@ConfigurationProperties(prefix="rabbit.config")
@PropertySource(value="classpath:config/rabbitmq.properties",encoding="utf-8")
也不需要在项目启动类上增加@EnableConfigurationProperties这个注解。
当然在打包的时候也要将该属性文件包含进来记得在pom文件的
<resources>
<resource>下面添加包含进自定义的文件,否则找不到文件报错。
项目启动类代码:
1 package com.sharp.forward; 2 3 import org.mybatis.spring.annotation.MapperScan; 4 import org.slf4j.Logger; 5 import org.slf4j.LoggerFactory; 6 import org.springframework.boot.CommandLineRunner; 7 import org.springframework.boot.SpringApplication; 8 import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 9 import org.springframework.boot.autoconfigure.SpringBootApplication; 10 11 import com.sharp.forward.config.RabbitMQProperties; 12 13 @SpringBootApplication 14 //@ImportResource("classpath:config/application-user-service-dubbo.xml") 15 @MapperScan(basePackages= {"com.sharp.forward.mapper"}) 16 @EnableAutoConfiguration 17 public class Application implements CommandLineRunner{ 18 19 private static final Logger log = LoggerFactory.getLogger(Application.class); 20 public static void main(String[] args) { 21 SpringApplication.run(Application.class, args); 22 } 23 24 /** 25 * @param args 26 * @throws Exception 27 */ 28 @Override 29 public void run(String... args) throws Exception { 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 package com.sharp.forward.config; 5 6 import org.springframework.boot.context.properties.ConfigurationProperties; 7 import org.springframework.context.annotation.Configuration; 8 import org.springframework.context.annotation.PropertySource; 9 import org.springframework.stereotype.Component; 10 11 /** 12 * @author 醉逍遥 13 * 14 */ 15 @Component 16 @ConfigurationProperties(prefix="rabbit.config") 17 @PropertySource(value="classpath:config/rabbitmq.properties",encoding="utf-8") 18 public class RabbitMQProperties { 19 20 private static String host; 21 22 private static int port; 23 24 private static String userName; 25 26 private static String password; 27 28 public static String getHost() { 29 return host; 30 } 31 public static void setHost(String host) { 32 RabbitMQProperties.host = host; 33 } 34 public static int getPort() { 35 return port; 36 } 37 public static void setPort(int port) { 38 RabbitMQProperties.port = port; 39 } 40 public static String getUserName() { 41 return userName; 42 } 43 public static void setUserName(String userName) { 44 RabbitMQProperties.userName = userName; 45 } 46 public static String getPassword() { 47 return password; 48 } 49 public static void setPassword(String password) { 50 RabbitMQProperties.password = password; 51 } 52 53 }
各属性和方法都是静态的,问题就出在这里,于是将静态的均修改掉如下
属性bean
1 /** 2 * 3 */ 4 package com.sharp.forward.config; 5 6 import org.springframework.boot.context.properties.ConfigurationProperties; 7 import org.springframework.context.annotation.PropertySource; 8 import org.springframework.stereotype.Component; 9 10 /** 11 * @author 醉逍遥 12 * 13 */ 14 @Component 15 @ConfigurationProperties(prefix="rabbit.config") 16 @PropertySource(value="classpath:config/rabbitmq.properties",encoding="utf-8") 17 public class RabbitMQProperties { 18 19 public String getHost() { 20 return host; 21 } 22 23 public void setHost(String host) { 24 this.host = host; 25 } 26 27 public int getPort() { 28 return port; 29 } 30 31 public void setPort(int port) { 32 this.port = port; 33 } 34 35 public String getUserName() { 36 return userName; 37 } 38 39 public void setUserName(String userName) { 40 this.userName = userName; 41 } 42 43 public String getPassword() { 44 return password; 45 } 46 47 public void setPassword(String password) { 48 this.password = password; 49 } 50 51 private String host; 52 53 private int port; 54 55 private String userName; 56 57 private String password; 58 59 60 61 }
启动类
1 package com.sharp.forward; 2 3 import org.mybatis.spring.annotation.MapperScan; 4 import org.slf4j.Logger; 5 import org.slf4j.LoggerFactory; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.boot.CommandLineRunner; 8 import org.springframework.boot.SpringApplication; 9 import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 10 import org.springframework.boot.autoconfigure.SpringBootApplication; 11 12 import com.sharp.forward.config.RabbitMQProperties; 13 14 @SpringBootApplication 15 //@ImportResource("classpath:config/application-user-service-dubbo.xml") 16 @MapperScan(basePackages= {"com.sharp.forward.mapper"}) 17 @EnableAutoConfiguration 18 public class Application implements CommandLineRunner{ 19 20 private static final Logger log = LoggerFactory.getLogger(Application.class); 21 public static void main(String[] args) { 22 SpringApplication.run(Application.class, args); 23 } 24 25 @Autowired 26 private RabbitMQProperties rabbitMQProperties; 27 /** 28 * @param args 29 * @throws Exception 30 */ 31 @Override 32 public void run(String... args) throws Exception { 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 }
再次启动如下:
说明属性值已经读取。
同样将属性bean修改为如下也不能在在初始化中为静态变量赋值
1 /** 2 * 3 */ 4 package com.sharp.forward.config; 5 6 import org.springframework.beans.factory.annotation.Value; 7 import org.springframework.boot.context.properties.ConfigurationProperties; 8 import org.springframework.context.annotation.Configuration; 9 import org.springframework.context.annotation.PropertySource; 10 import org.springframework.stereotype.Component; 11 12 /** 13 * @author 醉逍遥 14 * 15 */ 16 @Component 17 @ConfigurationProperties(prefix="rabbit.config") 18 @PropertySource(value="classpath:config/rabbitmq.properties",encoding="utf-8") 19 public class RabbitMQProperties { 20 21 private static String host; 22 23 private static int port; 24 25 private static String userName; 26 27 private static String password; 28 29 public static String getHost() { 30 return host; 31 } 32 @Value(value="${host}") 33 public static void setHost(String host) { 34 RabbitMQProperties.host = host; 35 System.out.println("host----------->"+host); 36 } 37 public static int getPort() { 38 return port; 39 } 40 @Value(value="${rabbit.config.port}") 41 public static void setPort(int port) { 42 RabbitMQProperties.port = port; 43 System.out.println("port----------->"+port); 44 } 45 public static String getUserName() { 46 return userName; 47 } 48 @Value(value="${userName}") 49 public static void setUserName(String userName) { 50 RabbitMQProperties.userName = userName; 51 } 52 public static String getPassword() { 53 return password; 54 } 55 public static void setPassword(String password) { 56 RabbitMQProperties.password = password; 57 } 58 59 }
运行结果同样都是空或0;
参考https://www.cnblogs.com/hsz-csy/p/9625950.html,可以解决为静态变量赋值的问题,set方法一定要是非静态的
修改为
/** * */ package com.sharp.forward.config; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; /** * @author 醉逍遥 * */ @Component @ConfigurationProperties(prefix="rabbit.config") @PropertySource(value="classpath:config/rabbitmq.properties",encoding="utf-8") public class RabbitMQProperties { private static String host; private static int port; private static String userName; private static String password; public static String getHost() { return host; } @Value(value="${host}") public static void setHost(String host) { RabbitMQProperties.host = host; System.out.println("host----------->"+host); } public static int getPort() { return port; } @Value(value="${rabbit.config.port}") public void setPort(int port) { RabbitMQProperties.port = port; System.out.println("port----------->"+port); } public static String getUserName() { return userName; } @Value(value="${userName}") public void setUserName(String userName) { RabbitMQProperties.userName = userName; } public static String getPassword() { return password; } public static void setPassword(String password) { RabbitMQProperties.password = password; } }
其他不变,
com.sharp.forward.Application - SpringBoot2.0实现自定义properties配置文件与JavaBean映射:host: null, config.port:5672, config.userName:guest
host没取到是因为value中路径写个重复前缀的实验用了。