java 无法加载配置文件_spring无法读取properties文件数据问题详解

本文详细解析了Spring在加载配置文件时遇到的问题,包括controller无法读取`config.properties`,service中存在多个配置文件导致的问题,以及诊断加载错误的方法。通过查看日志、检查配置文件路径和配置属性,可以定位并解决问题。建议使用`classpath*:config.properties`来解决多个配置文件的问题。
摘要由CSDN通过智能技术生成

1. controller中无法读取config.properties文件

controller中注入的@Value配置是从servlet-context.xml配置文件中获取的;service中注入的@Value配置可以从applicationContext.xml中获取的。所以,如果要在controller中注入属性配置,需要在相应servlet文件中添加配置,同applicationContext.xml中一样。

classpath:jdbc.properties

classpath:config.properties

2.service中无法读取config.properties文件

查看配置文件是否有多个。如果配置的路径是classpath:config.properties, 鼠标点击文件。如果显示”multiple implementations”, 表示有多个文件,查看其他的文件中是否有需要的配置项,没有的话,很可能就是加载了其他文件的配置项。这时,将路径改为classpath*:config.properties即可。

ignore-unresolvable="true" location="classpath:/jdbc.properties, classpath*:/config.properties"/>

查看日志,发现:

[2017-01-05 16:45:02 INFO ] [main] (org.springframework.context.support.PropertySourcesPlaceholderConfigurer:?) - Loading properties file from URL [jar:file:/home/admin/creative-task/lib/xxxx-com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值