记录一次spring boot 读取yml文件中的数据一直为null

springboot有时获取配置文件的数据一直为null,前今天才遇到这个问题。,总结一下原因可能如下:

1.类上未加@Component,或@Configuration 或 @Service 或者 @RestController 注解
2 语法错误 如下 错误的写法
@Value(" b d . s e c r e t K e y " ) p r i v a t e s t a t i c S t r i n g s e c r e t K e y ; @ V a l u e ( " {bd.secretKey}") private static String secretKey; @Value(" bd.secretKey")privatestaticStringsecretKey;@Value("{bd.secretKey}")
private final String secretKey;
正确的写法:
@Value(" b d . s e c r e t K e y " ) p r i v a t e S t r i n g s e c r e t K e y ; @ V a l u e ( " {bd.secretKey}") private String secretKey; @Value(" bd.secretKey")privateStringsecretKey;@Value("{bd.secretKey}")
String secretKey;

3 有时候为啦测试,直接写啦一个main 方法 测试,这种情况下是肯定获取不到的。

4 对象必须使用@Autowired注入,才能正常使用@Value注解,(而不是new 的对象)

当然还有其他的情况,这里就不记录啦,希望这次踩坑记录的对你有所帮助

### 回答1: 要让Spring Boot读取相对路径下的文件,我们可以使用Resource类提供的方法进行操作。 首先,我们需要在Spring Boot的配置文件(比如application.properties或application.yml配置文件路径。我们可以设置一个属性,比如"file.path",并指定文件的相对路径。例如,如果我们想读取相对于项目根目录下的一个名为"data.txt"的文件,我们可以在配置文件添加以下内容: file.path=data.txt 接下来,在我们的代码,我们可以使用ResourceLoader类来获取这个文件。 首先,我们需要在我们的类注入一个ResourceLoader对象: @Autowired private ResourceLoader resourceLoader; 然后,我们可以使用resourceLoader的getResource方法来获取文件的URL: String filePath = "classpath:" + file.path; Resource resource = resourceLoader.getResource(filePath); URL url = resource.getURL(); 最后,我们可以使用url对象来读取文件的内容。例如,我们可以使用Files类来读取文本文件的内容: Path path = Paths.get(url.toURI()); List<String> lines = Files.readAllLines(path); 现在,我们就可以通过lines变量来访问文件的内容了。 总结起来,如果想要Spring Boot读取相对路径下的文件,我们只需要在配置文件指定文件的相对路径,并使用ResourceLoader来获取文件的URL,在代码读取文件的内容即可。 ### 回答2: 在Spring Boot,我们可以使用`ResourceLoader`类来读取相对路径下的文件。 首先,我们需要注入`ResourceLoader`对象: ```java @Autowired private ResourceLoader resourceLoader; ``` 然后,可以使用`resourceLoader.getResource()`方法获取文件的`Resource`对象。我们可以使用`classpath:`前缀来指定相对于classpath的相对路径,或者使用`file:`前缀来指定相对于文件系统的路径。 ```java Resource resource = resourceLoader.getResource("classpath:example.txt"); ``` 接下来,我们可以通过`resource.getInputStream()`方法获取文件的输入流,从而读取文件的内容。 ```java try (InputStream inputStream = resource.getInputStream()) { // 读取文件内容 } ``` 另外,如果我们想指定文件的绝对路径,可以使用`file:`前缀: ```java Resource resource = resourceLoader.getResource("file:/path/to/file/example.txt"); ``` 需要注意的是,尽管Spring Boot的classpath默认是`classpath:`,但是我们仍然可以使用相对于classpath的表达式,例如: ```java Resource resource = resourceLoader.getResource("classpath:/files/example.txt"); ``` 总之,使用`ResourceLoader`对象和相应的前缀,我们可以方便地读取Spring Boot应用程序相对路径下的文件。 ### 回答3: 在Spring Boot,可以使用ResourceLoader来读取相对路径下的文件。 首先需要注入一个ResourceLoader对象到你的代码,你可以通过@Autowired或者构造函数等方式来实现注入。 例如,你可以在一个Service类注入一个ResourceLoader对象: ```java @Service public class MyService { @Autowired private ResourceLoader resourceLoader; // ... } ``` 然后,你可以使用ResourceLoader的getResource方法来获取相对路径下的文件,该方法需要传入一个相对路径字符串作为参数。 例如,假设你有一个名为"data.txt"的文件在相对路径"/resources"下,你可以通过如下代码获取该文件的Resource对象: ```java Resource resource = resourceLoader.getResource("classpath:data.txt"); ``` 其,"classpath:"是用来指示文件位于classpath下的前缀。如果文件不位于classpath下,你可以使用其他适当的前缀,例如"file:"来指示文件位于文件系统下的绝对路径。 接下来,你可以使用Resource对象提供的方法来读取文件的内容,例如使用getInputStream方法来获取文件的输入流,然后使用IO流操作来读取文件内容。 例如,你可以通过如下代码来读取文件内容并输出: ```java try (InputStream inputStream = resource.getInputStream()) { BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } } catch (IOException e) { // 处理异常 } ``` 这样,你就可以使用Spring Boot读取相对路径下的文件了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值