【问题标题】:not able to get property using context:property-placeholder (Spring)无法使用上下文获取属性:属性占位符(Spring)
【发布时间】:2014-04-19 10:31:59
【问题描述】:

我正在尝试从“文件”中读取属性,但无法获得相同的结果。

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;

properties-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/

我仍然被困在同一个地方。

【问题讨论】:

  • 在您的第 5 次尝试中,ThreadFileImageUpload 是 Spring Bean 吗?
  • ThreadFileImageUpload类的包名是什么?
  • @Shinichi package com.jodo.image.util;
  • @geoand不,它不是spring bean
  • 第一次尝试时,ThreadFileImageUpload 是否在类级别使用@Component 或其他原型注释进行了注释?要使@Value 工作,ThreadFileImageUpload 实例必须是 Spring 托管组件。

标签: java spring spring-mvc spring-3


【解决方案1】:

无论您使用@Value 注入属性,您要注入的类都必须是一个Spring bean。要做到这一点,您需要使用 @Component 注释 ThreadFileImageUpload 并确保组件扫描中的包中的类,或者您需要手动添加该 bean(在本例中为 ApplicationContext.xml)

【讨论】:

  • 将“@Value”注释属性移动到服务类(因为它不是通过“new”和注释“@Service”创建的)解决了这个问题。感谢 geoand 提供注释建议。
  • @user811602 很高兴为您提供帮助!
【解决方案2】:

要使@Value 工作,ThreadFileImageUpload 实例必须是 Spring 托管组件。请注意,使用“new”创建的实例由 Spring 管理。参考手册herehere可以帮助你理解Spring bean和组件扫描。

【讨论】:

  • 将“@Value”注释属性移动到服务类(因为它不是通过“new”和注释“@Service”创建的)解决了这个问题。谢谢
【解决方案3】:

我认为您缺少属性的名称(根)。 看到这个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}")

【讨论】:

  • 嗨 Evgeni,在更改为 @Value("${service.rawImagePath}") 后,我仍然得到它为空。加上问题是其他情况,好像是这种情况,然后尝试第一次会成功。 (在第一次尝试中,我从 bean“systemPropertiesHolder”设置它)
  • 您是否尝试过在
  • 未解决 Evgeni。即使在粘贴的链接中,属性文件也是“myprops.properties”,id 是“myProps”。文件名和 bean id 未链接
猜你喜欢
  • 2012-08-03
  • 1970-01-01
  • 1970-01-01
  • 2015-06-02
  • 1970-01-01
  • 2013-12-06
  • 1970-01-01
  • 1970-01-01
  • 2011-09-22
相关资源
最近更新 更多