springboot自定义属性文件与bean映射注入属性值
主要有几点:
一、导入依赖
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映射注入属性值的更多相关文章
- spring练习,使用Eclipse搭建的Spring开发环境,使用set注入方式为Bean对象注入属性值并打印输出。
相关 知识 >>> 相关 练习 >>> 实现要求: 使用Eclipse搭建的Spring开发环境,使用set注入方式为Bean对象注入属性值并打印输出.要求如下: ...
- SpringBoot注解把配置文件自动映射到属性和实体类实战
SpringBoot注解把配置文件自动映射到属性和实体类实战 简介:讲解使用@value注解配置文件自动映射到属性和实体类 1.配置文件加载 方式一 1.Controller上面配置 @Propert ...
- SpringBoot拦截器中Bean无法注入(转)
问题 这两天遇到SpringBoot拦截器中Bean无法注入问题.下面介绍我的思考过程和解决过程: 1.由于其他bean在service,controller层注入一点问题也没有,开始根本没意识到Be ...
- Spring(八):Spring配置Bean(一)BeanFactory&ApplicationContext概述、依赖注入的方式、注入属性值细节
在Spring的IOC容器里配置Bean 配置Bean形式:基于xml文件方式.基于注解的方式 在xml文件中通过bean节点配置bean: <?xml version="1.0&qu ...
- spring:为javabean的集合对象注入属性值
spring:为JavaBean的集合对象注入属性值 在 spring 中可以对List.Set.Map 等集合进行配置,不过根据集合类型的不同,需要使用不同的标签配置对应相应的集合. 1.创建 Ts ...
- spring读取classpath目录下的配置文件通过表达式去注入属性值.txt
spring读取配置文件: 1. spring加载配置文件: <context:property-placeholder location="classpath:config/syst ...
- Spring之使用注解实例化Bean并注入属性
1.准备工作 (1)导入jar包 除了上篇文章使用到的基本jar包外,还得加入aop的jar包,所有jar包如下 所需jar包 (2)配置xml <?xml version="1.0& ...
- Java反射之Bean修改更新属性值等工具类
package com.bocean.util; import java.lang.annotation.Annotation; import java.lang.reflect.Field; imp ...
- Android布局文件layout.xml的一些属性值
第一类:属性值 true或者 false android:layout_centerHrizontal 水平居中 android:layout_centerVertical 垂直居中 andr ...
随机推荐
- Android Studio 配置Gradle
一, 问题:①换个新电脑安装完Android Sutdio第一次打开一个工程巨慢怎么办?② 手动配置Gradle Home为什么总是无效?③ 明明已经下载了Gradle,配置了gradle home, ...
- Centos610无桌面安装Docker-内核升级
1.查看当前操作系统和系统内核 (此处只需要注意一项centos6的docker源只有64位的,x86_64,32位的直接换系统吧) 查看当前内核版本uname -r 2.6.32-754.el6.x ...
- Python实现图片识别加翻译【高薪必学】
Python使用百度AI接口实现图片识别加翻译 另外很多人在学习Python的过程中,往往因为没有好的教程或者没人指导从而导致自己容易放弃,为此我建了个Python交流.裙 :一久武其而而流一思(数字 ...
- oracle 高级函数
原 oracle 高级函数 2017年08月17日 16:44:19 阅读数:1731 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u013278 ...
- 「题解」「美团 CodeM 资格赛」跳格子
目录 「题解」「美团 CodeM 资格赛」跳格子 题目描述 考场思路 思路分析及正解代码 「题解」「美团 CodeM 资格赛」跳格子 今天真的考自闭了... \(T1\) 花了 \(2h\) 都没有搞 ...
- Kali中文乱码问题
上面的是用网上介绍的安装组件无法安装,老是提示最后一句:Unable to locate package ...... 后来觉得应该是因为安装Kali时在最后有个选择更新系统的一个配置上,我选择了下面 ...
- C++启动和关闭外部exe
转载:https://www.cnblogs.com/Sketch-liu/p/7277130.html 1.WinExec( lpCmdLine: LPCSTR; {文件名和参数; 如没指定路径会 ...
- run jumper server
1. 生成key: $ if [ "$SECRET_KEY" = "" ]; then SECRET_KEY=`cat /dev/urandom | tr -d ...
- Systemverilog for design 笔记(七)
转载请标明出处 第一章 接口(interface) 1.1. 接口的概念 接口允许许多信号合成一组由一个端口表示. 1.2. 接口声明 //接口定义 Interface main_bus ...
- Ubuntu18.04-MySQL8.0-表名大小写敏感-远程连接
1.卸载 停止服务 sudo service mysql stop 删除mysql服务 sudo apt-get remove mysql-server 删除其他组件 sudo apt-get aut ...