简体   繁体   English

无法使用context:property-placeholder获得属性(春季)

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

I am trying to read properties from "file" but not able to achive same. 我正在尝试从“文件”中读取属性,但无法达到相同的目的。

web.xml 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 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>

1st try 第一次尝试

Class ThreadFileImageUpload 类ThreadFileImageUpload

public class ThreadFileImageUpload extends Thread {
    private String rawImagePath;

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

Result : rawImagePath is null 结果:rawImagePath为空

2nd try 第二次尝试

Class ThreadFileImageUpload 类ThreadFileImageUpload

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

properties-config.xml 性能-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>

Result : rawImagePath is null 结果:rawImagePath为空

3rd try 第三次尝试

Class ThreadFileImageUpload 类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");
        ...

Give java.lang.NullPointerException (environment itself is null) 给出java.lang.NullPointerException(环境本身为null)

4th try 第四次尝试

Class ThreadFileImageUpload 类ThreadFileImageUpload

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

Result : rawImagePath is null 结果:rawImagePath为空

5th try 第五次尝试

ApplicationContext.xml (now placed all file in one property-placeholder) ApplicationContext.xml(现在将所有文件放在一个属性占位符中)

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

Class ThreadFileImageUpload 类ThreadFileImageUpload

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

Result : rawImagePath is null 结果:rawImagePath为空

6th try 第六次尝试

class ThreadFileImageUpload 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 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>

Still getting it null 仍然得到它null

I am sure that property file has required field 我确定属性文件具有必填字段

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

I am still stuck in same place. 我仍然停留在同一个地方。

Whevever you inject properties using @Value, the class your are injecting into needs to be a Spring bean. 无论您使用@Value注入属性,注入的类都必须是Spring bean。 To make that happen you need to either annotate ThreadFileImageUpload with @Component and make sure that the class in a package that is within the component scan, or you need to add that bean manually (in this case into ApplicationContext.xml) 为此,您需要使用@Component注释ThreadFileImageUpload并确保扫描组件中的包中的类,或者您需要手动添加该bean(在这种情况下,将其添加到ApplicationContext.xml中)。

I think you're missing the name(root) of the properties. 我认为您缺少属性的名称(根)。 See this http://forum.spring.io/forum/spring-projects/container/61645-value-and-propertyplaceholderconfigurer 看到这个http://forum.spring.io/forum/spring-projects/container/61645-value-and-propertyplaceholderconfigurer

If the file is service.properties and you have 如果文件是service.properties并且您有

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

then try @Value("#{service.rawImagePath}") 然后尝试@Value("#{service.rawImagePath}")

To make @Value work, ThreadFileImageUpload instance must be a Spring managed component. 为了使@Value工作,ThreadFileImageUpload实例必须是Spring托管的组件。 Please note that instances created using "new" are not managed by Spring. 请注意,使用“ new”创建的实例不受 Spring的管理。 The reference manual here and here may help you to understand Spring beans and component scanning. 此处此处的参考手册可以帮助您了解Spring Bean和组件扫描。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM