gpt4 book ai didi

java - 无法使用上下文 :property-placeholder (Spring) 获取属性

转载 作者:行者123 更新时间:2023-12-01 18:33:19 27 4
gpt4 key购买 nike

我正在尝试从"file"读取属性,但无法实现相同的效果。

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(环境本身为空)

第四次尝试

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>

仍然为空

我确信属性文件有必填字段

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

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

最佳答案

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

关于java - 无法使用上下文 :property-placeholder (Spring) 获取属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23168891/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com