主要有几点:

一、导入依赖

springboot的包和:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>

版本在父类里统一管理了

<optional>true</optional>
</dependency>

一、配置自定义属性文件

 rabbit.config.host=192.168.135.129
rabbit.config.port=5672
rabbit.config.userName=guest
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>下面添加包含进自定义的文件,否则找不到文件报错。

项目启动类代码:

 package com.sharp.forward;

 import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication; import com.sharp.forward.config.RabbitMQProperties; @SpringBootApplication
//@ImportResource("classpath:config/application-user-service-dubbo.xml")
@MapperScan(basePackages= {"com.sharp.forward.mapper"})
@EnableAutoConfiguration
public class Application implements CommandLineRunner{ private static final Logger log = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
} /**
* @param args
* @throws Exception
*/
@Override
public void run(String... args) throws Exception {
String config = "host: " + RabbitMQProperties.getHost()
+ ", config.port:" + RabbitMQProperties.getPort()
+ ", config.userName:" + RabbitMQProperties.getUserName(); log.info("SpringBoot2.0实现自定义properties配置文件与JavaBean映射:" + config); } }

启动项目后打印如下:

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

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

 /**
*
*/
package com.sharp.forward.config; 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;
}
public static void setHost(String host) {
RabbitMQProperties.host = host;
}
public static int getPort() {
return port;
}
public static void setPort(int port) {
RabbitMQProperties.port = port;
}
public static String getUserName() {
return userName;
}
public static void setUserName(String userName) {
RabbitMQProperties.userName = userName;
}
public static String getPassword() {
return password;
}
public static void setPassword(String password) {
RabbitMQProperties.password = password;
} }

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

属性bean

 /**
*
*/
package com.sharp.forward.config; import org.springframework.boot.context.properties.ConfigurationProperties;
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 { public String getHost() {
return host;
} public void setHost(String host) {
this.host = host;
} public int getPort() {
return port;
} public void setPort(int port) {
this.port = port;
} public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} private String host; private int port; private String userName; private String password; }

启动类

 package com.sharp.forward;

 import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication; import com.sharp.forward.config.RabbitMQProperties; @SpringBootApplication
//@ImportResource("classpath:config/application-user-service-dubbo.xml")
@MapperScan(basePackages= {"com.sharp.forward.mapper"})
@EnableAutoConfiguration
public class Application implements CommandLineRunner{ private static final Logger log = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
} @Autowired
private RabbitMQProperties rabbitMQProperties;
/**
* @param args
* @throws Exception
*/
@Override
public void run(String... args) throws Exception {
String config = "host: " + rabbitMQProperties.getHost()
+ ", config.port:" + rabbitMQProperties.getPort()
+ ", config.userName:" + rabbitMQProperties.getUserName(); log.info("SpringBoot2.0实现自定义properties配置文件与JavaBean映射:" + config); } }

再次启动如下:

说明属性值已经读取。

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

 /**
*
*/
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 static void setPort(int port) {
RabbitMQProperties.port = port;
System.out.println("port----------->"+port);
}
public static String getUserName() {
return userName;
}
@Value(value="${userName}")
public static void setUserName(String userName) {
RabbitMQProperties.userName = userName;
}
public static String getPassword() {
return password;
}
public static void setPassword(String password) {
RabbitMQProperties.password = password;
} }

运行结果同样都是空或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中路径写个重复前缀的实验用了。

springboot自定义属性文件与bean映射注入属性值的更多相关文章

  1. spring练习,使用Eclipse搭建的Spring开发环境,使用set注入方式为Bean对象注入属性值并打印输出。

    相关 知识 >>> 相关 练习 >>> 实现要求: 使用Eclipse搭建的Spring开发环境,使用set注入方式为Bean对象注入属性值并打印输出.要求如下: ...

  2. SpringBoot注解把配置文件自动映射到属性和实体类实战

    SpringBoot注解把配置文件自动映射到属性和实体类实战 简介:讲解使用@value注解配置文件自动映射到属性和实体类 1.配置文件加载 方式一 1.Controller上面配置 @Propert ...

  3. SpringBoot拦截器中Bean无法注入(转)

    问题 这两天遇到SpringBoot拦截器中Bean无法注入问题.下面介绍我的思考过程和解决过程: 1.由于其他bean在service,controller层注入一点问题也没有,开始根本没意识到Be ...

  4. Spring(八):Spring配置Bean(一)BeanFactory&ApplicationContext概述、依赖注入的方式、注入属性值细节

    在Spring的IOC容器里配置Bean 配置Bean形式:基于xml文件方式.基于注解的方式 在xml文件中通过bean节点配置bean: <?xml version="1.0&qu ...

  5. spring:为javabean的集合对象注入属性值

    spring:为JavaBean的集合对象注入属性值 在 spring 中可以对List.Set.Map 等集合进行配置,不过根据集合类型的不同,需要使用不同的标签配置对应相应的集合. 1.创建 Ts ...

  6. spring读取classpath目录下的配置文件通过表达式去注入属性值.txt

    spring读取配置文件: 1. spring加载配置文件: <context:property-placeholder location="classpath:config/syst ...

  7. Spring之使用注解实例化Bean并注入属性

    1.准备工作 (1)导入jar包 除了上篇文章使用到的基本jar包外,还得加入aop的jar包,所有jar包如下 所需jar包 (2)配置xml <?xml version="1.0& ...

  8. Java反射之Bean修改更新属性值等工具类

    package com.bocean.util; import java.lang.annotation.Annotation; import java.lang.reflect.Field; imp ...

  9. Android布局文件layout.xml的一些属性值

        第一类:属性值 true或者 false android:layout_centerHrizontal 水平居中 android:layout_centerVertical 垂直居中 andr ...

随机推荐

  1. 使用Shiro实现认证和授权(基于SpringBoot)

    Apache Shiro是一个功能强大且易于使用的Java安全框架,它为开发人员提供了一种直观,全面的身份验证,授权,加密和会话管理解决方案.下面是在SpringBoot中使用Shiro进行认证和授权 ...

  2. Flask - flask-mail

    flasky中git reset --hard 8e 问题 使用flask-mail通过163邮箱的smtp服务发送token认证邮件,要关闭TLS才能发送 原理 还在研究中 结果

  3. MySQL - 设置UTF-8编码

    1. 在Windows上,安装时请选择UTF-8编码,以便正确地处理中文. 在Mac或Linux上,需要编辑MySQL的配置文件,把数据库默认的编码全部改为UTF-8.MySQL的配置文件默认存放在/ ...

  4. 单链表 C++ 实现 - 含虚拟头节点

    本文例程下载链接:ListDemo 链表 vs 数组 链表和数组的最大区别在于链表不支持随机访问,不能像数组那样对任意一个(索引)位置的元素进行访问,而需要从头节点开始,一个一个往后访问直到查找到目标 ...

  5. 深入理解 ajax系列第一篇(XHR 对象)

    1999年,微软公司发布了IE5, 第一次引入新功能:允许javascript 脚本向服务器发起 hffp 请求.这个功能方式并没有被引起注意,知道2004年 Gmail 发布和 Google Map ...

  6. hadoop3.1.1高可用集群web端口9870

  7. Java基础知识笔记第九章:组件及事件处理

    java Swing 图形用户界面(GUI : Graphics User Interface) 窗口 JFrame常用方法 JFrame()创建一个无标题的窗口. JFrame(String s)创 ...

  8. navicat12破解详细教程

    以管理员身份运行此注册机: 运行注册机 打开注册机后,1) Patch勾选Backup.Host和Navicat v12,然后点击Patch按钮: 默认勾选 找到Navicat Premium 12安 ...

  9. Oracle个人自学笔记

    SET LINESIZE 300;//设置每一行的长度 SET PAGESIZE 100;//设置每一列的长度 CONN 用户名/密码 [AS SYSDBA],如果是sys用户一定要加上SYSDBA ...

  10. 整理了一下NLP中文数据集

    个人理解: 句子相似性判断.情感分析.实体识别.智能问答,本质基本上都是分类任务. 阅读理解(抽取式.回答式.完形填空)是逐个候选项的分类问题处理. 参考 https://github.com/chi ...