cancel
Showing results for 
Search instead for 
Did you mean: 

ConfigurationService is null, even with Spring bean/annotation?

dartuso
Discoverer
0 Kudos
1,153

I have written some util functions which use ConfigurationService to fetch string values. (In src of a custom extension)

To autowire the ConfigurationSerivce outside of a controller my understanding is you need to tell spring to pick up the class through an annotation or through adding it to a bean.

Both ways I have tried have resulted in a error: Cannot invoke \"de.hybris.platform.servicelayer.config.ConfigurationService.getConfiguration()\" because \"this.configurationService\" is null

Does anyone know if my logic is off in getting the DI to work?

Accepted Solutions (0)

Answers (1)

Answers (1)

alemasetto
Participant
0 Kudos
Here are a few things you can try to resolve the issue:
  • Check that the ConfigurationService is being properly imported in your util functions. Make sure that you are importing the correct package and that there are no typos in the import statement.
  • Check that your util functions are being properly scanned by Spring. Make sure that your custom extension package is included in the scan for Spring components, you can add it to the myexampleextension-spring.xml file of your extension using this format: <context:component-scan base-package="your.package.name.of.util.class"/>
  • Make sure that the util functions are not in a static context, autowiring does not work
  • Check getter and setter methods in your util class
  • Check the bean definition
  • Check the "ref" value of your bean: <property name="configurationService" ref="configurationService" />

Example:

public class MyClass {
    
    private ConfigurationService configurationService;		
	
   public String getMyValue() {
        return getConfigurationService().getConfiguration().getString("example.value", "default value");
    }	

    public ConfigurationService getConfigurationService() {
        return configurationService;
    }
    
    /**
* Spring injects the value with this setter! public void setConfigurationService(ConfigurationService configurationService) { this.configurationService = configurationService; }
<bean id="myClass" class="com.example.MyClass">
<property name="configurationService" ref="configurationService" />
</bean>

If none of the above solutions work, please provide more details about your project setup, including the versions of SAP Commerce that you are using, and the specific packages and classes that you are importing and annotating. This will help to troubleshoot the issue more effectively.

Spring Framework in SAP Commerce:

https://help.sap.com/docs/SAP_COMMERCE/d0224eca81e249cb821f2cdf45a82ace/8c63621986691014a7e0a18695d7...