@ConfigurationProperties注解的使用与@Value的使用
-
读取的配置文件
application.yml
:jianshu: id: futaosmile
1. 使用@ConfigurationProperties注解注入
@RunWith(SpringRunner.class) @SpringBootTest @ConfigurationProperties(prefix = "jianshu") public class SpringmvcdemoApplicationTests { private String id; @Test public void test10() { System.out.println(id); } }
- 输出
null
,注入失败 - 原因:使用
@ConfigurationProperties
注解的方式注入为每个注入的字段添加setter方法
package com.futao.springmvcdemo; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest @ConfigurationProperties(prefix = "jianshu") public class SpringmvcdemoApplicationTests { private String id; @Test public void test10() { System.out.println(id); } public void setId(String id) { this.id = id; } }
- 输出:
2. 使用@Value注解
package com.futao.springmvcdemo; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class SpringmvcdemoApplicationTests { @Value("${jianshu.id}") private String id; @Test public void test10() { System.out.println(id); } }
- 输出:
OK

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
消息中间件系列五、rabbit消息的确认机制
前言:这是中间件一个系列的文章之一,有需要的朋友可以看看这个系列的其他文章:消息中间件系列一、消息中间件的基本了解消息中间件系列二、Windows下的activeMQ和rabbitMQ的安装消息中间件系列三、JMS和activeMQ的简单使用消息中间件系列四、认识AMQP和RabbiyMq的简单使用消息中间件系列五、rabbit消息的确认机制消息中间件系列六,rabbit与spring集成实战 一、消息的确认机制 1、消费者收到的每一条消息都必须进行确认。(分为自动确认和消费者自行确认) 消费者在声明队列时,指定autoAck参数,true自动确认,false时rabbitmq会等到消费者显示的发回一个ack信号才会删除消息。autoAck=false,有足够时间让消费者处理消息,直到消费者显示调用basicAck为止。Rabbitmq中消息分为了两部分:1、等待投递的消息;2、已经投递,但是还没有收到ack信号的。如果消费者断连了,服务器会把消息重新入队,投递给下一个消费者。补充:未ack的消息是没有超时时间的,没有处理会一直在队列中,知道内存溢出。 2、如何明确拒绝消息 a、消费者...
- 下一篇
springboot整合druid连接池
依赖 //mysql 驱动 compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.21' //druid compile group: 'com.alibaba', name: 'druid', version: '1.1.10' 1. 新建druid配置信息类DruidConfiguration.java package com.futao.springmvcdemo.foundation.configuration; import com.alibaba.druid.pool.DruidDataSource; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; import javax.sql.DataS...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS关闭SELinux安全模块
- CentOS7,8上快速安装Gitea,搭建Git服务器
- CentOS7,CentOS8安装Elasticsearch6.8.6
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- CentOS6,CentOS7官方镜像安装Oracle11G
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- CentOS8编译安装MySQL8.0.19
- Windows10,CentOS7,CentOS8安装Nodejs环境
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- Hadoop3单机部署,实现最简伪集群