zoukankan      html  css  js  c++  java
  • spring无法读取properties文件数据

    只讲述异常点,关于怎么配置文件,这里不做说明。
     

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

    controller中注入的@Value配置是从servlet-context.xml配置文件中获取的;service中注入的@Value配置可以从applicationContext.xml中获取的。所以,如果要在controller中注入属性配置,需要在相应servlet文件中添加配置,同applicationContext.xml中一样。
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                 <value>classpath:jdbc.properties</value>
                <value>classpath:config.properties</value>
             </list>
        </property>
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
    </bean>

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

    查看配置文件是否有多个。如果配置的路径是classpath:config.properties, 鼠标点击文件。如果显示”multiple implementations”, 表示有多个文件,查看其他的文件中是否有需要的配置项,没有的话,很可能就是加载了其他文件的配置项。这时,将路径改为classpath*:config.properties即可。
    <context:property-placeholder
          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-common-1.5.7.jar!/config.properties]
    [2017-01-05 16:45:02 INFO ] [main] (org.springframework.context.support.PropertySourcesPlaceholderConfigurer:?) - Loading properties file from URL [file:/home/admin/creative-task/conf/config.properties]

     加载了两个config.properties文件。

     

    3.关于诊断:

    1)首先确认是否正确加载了配置文件。查看日志:

    正常日志如下:
    [2017-01-05 16:45:02 INFO ] [main] (org.springframework.context.support.PropertySourcesPlaceholderConfigurer:?) - Loading properties file from URL [file:/home/admin/creative-task/conf/config.properties]

     异常日志如下:

    [2017-01-05 16:39:39 ERROR] [main] (Main:22) - Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [config.properties] cannot be opened because it does not exist
    org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [config.properties] cannot be opened because it does not exist 
    如果文件没有加载,则查看路径是否匹配等。
     

    2)如果文件加载ok,查看配置属性是否正确加载。

    查看tomcat启动的debug日志:
    正常日志如下:
    [2017-01-05 16:45:04 DEBUG] [main] (org.springframework.core.env.PropertySourcesPropertyResolver:?) - Searching for key 'adx.id' in [environmentProperties]
    [2017-01-05 16:45:04 DEBUG] [main] (org.springframework.core.env.PropertySourcesPropertyResolver:?) - Searching for key 'adx.id' in [systemProperties]
    [2017-01-05 16:45:04 DEBUG] [main] (org.springframework.core.env.PropertySourcesPropertyResolver:?) - Searching for key 'adx.id' in [systemEnvironment]
    [2017-01-05 16:45:04 DEBUG] [main] (org.springframework.core.env.PropertySourcesPropertyResolver:?) - Could not find key 'adx.id' in any property source. Returning [null]
    [2017-01-05 16:45:04 DEBUG] [main] (org.springframework.core.env.PropertySourcesPropertyResolver:?) - Searching for key 'adx.id' in [localProperties]
    [2017-01-05 16:45:04 DEBUG] [main] (org.springframework.core.env.PropertySourcesPropertyResolver:?) - Found key 'adx.id' in [localProperties] with type [String] and value '1'
    异常日志如下:
    [2017-01-05 16:34:01 DEBUG] [main] (org.springframework.core.env.PropertySourcesPropertyResolver:?) - Searching for key 'adx.id' in [environmentProperties]
    [2017-01-05 16:34:01 DEBUG] [main] (org.springframework.core.env.PropertySourcesPropertyResolver:?) - Searching for key 'adx.id' in [systemProperties]
    [2017-01-05 16:34:01 DEBUG] [main] (org.springframework.core.env.PropertySourcesPropertyResolver:?) - Searching for key 'adx.id' in [systemEnvironment]
    [2017-01-05 16:34:01 DEBUG] [main] (org.springframework.core.env.PropertySourcesPropertyResolver:?) - Could not find key 'adx.id' in any property source. Returning [null]
    [2017-01-05 16:34:01 DEBUG] [main] (org.springframework.core.env.PropertySourcesPropertyResolver:?) - Searching for key 'adx.id' in [localProperties]
    [2017-01-05 16:34:01 DEBUG] [main] (org.springframework.core.env.PropertySourcesPropertyResolver:?) - Could not find key 'adx.id' in any property source. Returning [null]
    如果配置属性没有正确加载,查看属性配置的key是否正确。 
  • 相关阅读:
    python基础5之装饰器
    python基础4之递归、lambda、深浅copy
    python基础3之文件操作、字符编码解码、函数介绍
    python基础1之python介绍、安装、变量和字符编码、数据类型、输入输出、数据运算、循环
    python基础2之字符串、列表、字典、集合
    Mysql主从复制的实现
    LNMP一键安装脚本
    LNMP搭建(CentOS 6.3+Nginx 1.2.0+PHP 5.3.15(fpm)+ MySQL 5.5.35)
    整合apache+tomcat+keepalived实现高可用tomcat集群
    浅谈CSRF
  • 原文地址:https://www.cnblogs.com/shoren/p/spring.html
Copyright © 2011-2022 走看看