对alfresco-global.properties的动态访问

问题描述 投票:0回答:3

尽管我阅读了这篇文章,但我无法动态访问alfresco-global.properties值:Accessing values from Alfresco's alfresco-global.properties file

这是我的朋友:

service-context.xml

<bean id="AccesGlobalPropertiesService" class="com.package.ksc.services.AccesGlobalPropertiesService">
     <property name="properties">
        <ref bean="global-properties"/>
    </property>
</bean>

AccesGlobalPropertiesService.java

import org.springframework.stereotype.Service;
import java.util.Properties;

@Service
public class AccesGlobalPropertiesService {

    public Properties properties;

    public void setProperties(Properties properties) {
        this.properties = properties;
    }
    public Properties getProperties() {
        return properties;
    }
}

Worker.java

public abstract class ClassifierServiceCommon {

   private AccesGlobalProperties accesGlobalProperties;
   private Properties properties;    

   /* Constructor */
   protected Worker(accesGlobalProperties) {
     this.accesGlobalProperties= accesGlobalProperties;
   }

   ...

   protected Boolean propAcces() {
      accesGlobalProperties.properties.getProperty("myPropKey");
      ...
   }
}

当我调用getProperty(“ myPropKey”)时得到NullPointerException ...

请怎么了?谢谢

java spring alfresco spring-annotations
3个回答
1
投票

似乎您在定义两个具有不同ID的bean时犯了错误:

  1. [一个bean来自XML,其ID被明确指定为“ AccesGlobalPropertiesService”。其properties字段已正确设置。
  2. 另一个bean来自Spring组件扫描(由于[C​​0]批注),其ID隐式设置为“ accesGlobalPropertiesService”。未设置其@Service字段,因为该字段缺少properties@Resource注释(有关如何使用它们的信息,请参见例如@Autowired)。

然后您很可能使用this question类中的第二个(不完整)。 (您未指定如何在此处获取bean。)


0
投票

您必须初始化变量(您似乎没有这样做)。您需要先设置setProperty(),然后再获取getProperty()。只要您不初始化AccesGlobalPropertiesService类中的属性,您的getProperty()都将返回null。

我不知道那是您要在这里做的事情吗:

ClassifierServiceCommon

如果是这种情况,则应将getProperty更改为setProperty。

希望对您有帮助:)


0
投票

您可以在下面的链接中查看详细信息。

protected Boolean propAcces(){ accesGlobalProperties.properties.getProperty("myPropKey"); ... }

© www.soinside.com 2019 - 2024. All rights reserved.