簡體   English   中英

無法使用context:property-placeholder獲得屬性(春季)

[英]not able to get property using context:property-placeholder (Spring)

我正在嘗試從“文件”中讀取屬性,但無法達到相同的目的。

web.xml中

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:ApplicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

applicationContext.xml中

<context:annotation-config />
<context:component-scan base-package="com.jodo.image.*"></context:component-scan>

<context:property-placeholder location="file:/usr/local/jodo/opt/cms-image/db.properties"/>
<context:property-placeholder location="file:/usr/local/jodo/opt/cms-image/service.properties"/>

<util:properties id="systemPropertiesHolder" location="file:/usr/local/jodo/opt/cms-image/service.properties">
</util:properties>

第一次嘗試

類ThreadFileImageUpload

public class ThreadFileImageUpload extends Thread {
    private String rawImagePath;

    @Value("#{systemPropertiesHolder.rawImagePath}")
    public void setRawImagePath(String property){
        rawImagePath = property;
    }...

結果:rawImagePath為空

第二次嘗試

類ThreadFileImageUpload

@Configuration
@ImportResource("classpath:properties-config.xml")
public class ThreadFileImageUpload extends Thread {
    private @Value("${rawImagePath}") String rawImagePath;

性能-config.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context-2.5.xsd
                http://www.springframework.org/schema/util 
                http://www.springframework.org/schema/util/spring-util-3.0.xsd">
  <context:property-placeholder location="file:/usr/local/jodo/opt/cms-image/service.properties"/>
</beans>

結果:rawImagePath為空

第三次嘗試

類ThreadFileImageUpload

@Configuration
@PropertySource("file:/usr/local/jodo/opt/cms-image/service.properties")
public class ThreadFileImageUpload extends Thread {
    @Autowired 
    Environment environment;
    private String rawImagePath;
    public void run() {
        if(environment == null){
                logger.info("environment is NULL");
            }
            rawImagePath = this.environment.getProperty("rawImagePath");
        ...

給出java.lang.NullPointerException(環境本身為null)

第四次嘗試

類ThreadFileImageUpload

public class ThreadFileImageUpload extends Thread { 
    @Value("${rawImagePath}")
    private String rawImagePath;

結果:rawImagePath為空

第五次嘗試

ApplicationContext.xml(現在將所有文件放在一個屬性占位符中)

<context:property-placeholder location="file:/usr/local/jodo/opt/cms-image/db.properties, file:/usr/local/jodo/opt/cms-image/service.properties"/>

類ThreadFileImageUpload

public class ThreadFileImageUpload extends Thread { 
    @Value("${rawImagePath}")
    private String rawImagePath;

結果:rawImagePath為空

第六次嘗試

ThreadFileImageUpload類

package com.jodo.image.util;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class ThreadFileImageUpload extends Thread { 
    @Value("#{systemPropertiesHolder.rawImagePath}")
    private String rawImagePath;

AppliactionContext.xml

<context:annotation-config />
<context:component-scan base-package="com.jodo.image.*"></context:component-scan>
<util:properties id="systemPropertiesHolder" location="file:/usr/local/jodo/opt/cms-image/service.properties">
</util:properties>

仍然得到它null

我確定屬性文件具有必填字段

[root@localhost /]# cat /usr/local/jodo/opt/cms-image/service.properties 
rawImagePath=/var/jodo-images/raw/

我仍然停留在同一個地方。

無論您使用@Value注入屬性,注入的類都必須是Spring bean。 為此,您需要使用@Component注釋ThreadFileImageUpload並確保掃描組件中的包中的類,或者您需要手動添加該bean(在這種情況下,將其添加到ApplicationContext.xml中)。

我認為您缺少屬性的名稱(根)。 看到這個http://forum.spring.io/forum/spring-projects/container/61645-value-and-propertyplaceholderconfigurer

如果文件是service.properties並且您有

`<util:properties id="service" location="classpath:com/acme/app/service.properties"/>`

然后嘗試@Value("#{service.rawImagePath}")

為了使@Value工作,ThreadFileImageUpload實例必須是Spring托管的組件。 請注意,使用“ new”創建的實例不受 Spring的管理。 此處此處的參考手冊可以幫助您了解Spring Bean和組件掃描。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM