Python 官方文档:入门教程 => 点击学习
使用@Value获取配置文件的值 1、创建配置文件(application.properties) spring.activeMQ.broker-url=tcp://localh
spring.activeMQ.broker-url=tcp://localhost:61616
spring.activemq.user=admin
spring.activemq.passWord=admin
spring.activemq.in-memory=true
spring.activemq.pool.enabled=false
package com.jeff.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.WEB.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
@Value("${spring.activemq.user}")
private String user;
@RequestMapping("myTest")
public String myTest() {
System.out.println("user的值:" + user);
return "success";
}
}
@Slf4j
@Component
public class FileUtils {
@Value("${document.path.output}")
private String outPath;
}
document:
path:
output: E:/workspace/doc_convert/tmp
新创建了一个SpringBoot项目,在使用@Value获取配置文件中的值时,一直为null。
通过排查发现,虽然值为null,说明配置的参地址是正确的,只是在Spring进行依赖注入的时候没有把这个值注入到变量中来。
通过检查发现,是由于使用此类(注解所在的类)时候是直接new的,并没有将此类的创建交由Spring容器进行管理。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: springboot如何使用@Value获取配置文件的值
本文链接: https://www.lsjlt.com/news/132301.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0