问题
I try to migrate the version of my application from Hybris 4.8 to Hybris 5.6.0.5.
When the server starts I have this exception:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'csTicketFacade' defined in class path resource [CoopAcceleratorFrontEndfacades-spring.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.accenture.hybris.facades.customerservice.impl.CoopCSTicketFacade]: Constructor threw exception; nested exception is java.lang.NullPointerException
INFO | jvm 1 | main | 2016/01/20 11:41:54.777 | Caused by: java.lang.NullPointerException
INFO | jvm 1 | main | 2016/01/20 11:41:54.790 | at com.accenture.facades.common.FlexSelector.getPropertiesReader(FlexSelector.java:27)
INFO | jvm 1 | main | 2016/01/20 11:41:54.797 | at com.accenture.hybris.facades.customerservice.impl.CoopCSTicketFacade$TicketFlexSelector.getCsAgentGroupUID(CoopCSTicketFacade.java:68)
INFO | jvm 1 | main | 2016/01/20 11:41:54.805 | at com.accenture.hybris.facades.customerservice.impl.CoopCSTicketFacade$TicketFlexSelector.getCsAgentGroupModel(CoopCSTicketFacade.java:31)
INFO | jvm 1 | main | 2016/01/20 11:41:54.809 | at com.accenture.hybris.facades.customerservice.impl.CoopCSTicketFacade.<init>(CoopCSTicketFacade.java:80)
INFO | jvm 1 | main | 2016/01/20 11:41:54.820 | at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
INFO | jvm 1 | main | 2016/01/20 11:41:54.823 | at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
INFO | jvm 1 | main | 2016/01/20 11:41:54.828 | at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
INFO | jvm 1 | main | 2016/01/20 11:41:54.834 | at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
INFO | jvm 1 | main | 2016/01/20 11:41:54.838 | at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:148)
INFO | jvm 1 | main | 2016/01/20 11:41:54.843 | ... 35 more
Because in the class FlexSelector:
protected static final ApplicationContext ctx = Registry.getApplicationContext();
The application context of spring is null. If someone has any idea please tell me.
回答1:
It's not a good idea to have a static reference to the application context.
Instead, as @thijsraets suggested you should make the class for the flexSelector
bean implement ApplicationContextAware
.
E.g:
public class FlexSelector implements ApplicationContextAware {
ApplicationContext context;
public ApplicationContext getContext() {
return context;
}
@Override
public void setApplicationContext(ApplicationContext context)
throws BeansException {
this.context=context;
}
[...]
}
This will make sure that the application context gets set when the bean is wired up by Spring.
In case you have static methods in FlexSelector
and require the application context in there, you should always dynamically look it up in your static methods via the Registry.getApplicationContext()
call instead of storing it in a static variable.
回答2:
Do you really need just the applicationContext object ? If you just need to retrieve the bean you could do it like below:
final ManagerFactoryBean managerFactoryBean = (ManagerFactoryBean) Registry.getApplicationContext().getBean("managerFactoryBean");
I too work in hybris and this is how I get my bean references.
来源:https://stackoverflow.com/questions/34922023/hybriscontextfactory-error-initializing-global-application-context