覆盖 hybris commonI18NService roundCurrency 方法
Posted
技术标签:
【中文标题】覆盖 hybris commonI18NService roundCurrency 方法【英文标题】:Override hybris commonI18NService roundCurrency method 【发布时间】:2017-11-03 20:50:06 【问题描述】:尝试使用Alias
覆盖spring bean
。
我想覆盖commonI18NService
的roundCurrency
方法
OOTB 定义
<alias alias="commonI18NService" name="defaultCommonI18NService"/>
<bean id="defaultCommonI18NService" class="de.hybris.platform.servicelayer.i18n.impl.DefaultCommonI18NService" parent="abstractBusinessService">
<property name="languageDao" ref="languageDao"/>
<property name="currencyDao" ref="currencyDao"/>
<property name="countryDao" ref="countryDao"/>
<property name="regionDao" ref="regionDao"/>
<property name="conversionStrategy" ref="conversionStrategy"/>
</bean>
我们的自定义代码:-
public class DefaultCustomCommonI18NService extends DefaultCommonI18NService
@Override
public double roundCurrency(double value, int digits)
// custom logic
return value;
注入自定义bean:-
<alias alias="commonI18NService" name="defaultCustomCommonI18NService"/>
<bean id="defaultCustomCommonI18NService" class="com.extended.service.impl.DefaultCustomCommonI18NService" parent="defaultCommonI18NService"/>
但它在服务器启动时抛出异常
INFO [localhost-startStop-1] [HybrisContextFactory] Loading <<application>> spring config <master> from extension (saporderexchangeb2b) located in (saporderexchangeb2b-spring.xml) took: (121.4 ms)
WARN [localhost-startStop-1] [CloseAwareApplicationContext] Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'listMergeBeanPostProcessor': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'commercePlaceOrderMethodHooksListMergeDirective' defined in class path resource [b2bapprovalprocess-spring.xml]: Cannot resolve reference to bean 'b2bApprovalBusinessProcessCreationPlaceOrderMethodHook' while setting bean property 'add'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'b2bApprovalBusinessProcessCreationPlaceOrderMethodHook' defined in class path resource [b2bapprovalprocess-spring.xml]: Cannot resolve reference to bean 'defaultB2BCreateOrderFromCartStrategy' while setting bean property 'businessProcessCreationStrategy'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultB2BCreateOrderFromCartStrategy' defined in class path resource [b2bapprovalprocess-spring.xml]: Cannot resolve reference to bean 'cloneAbstractOrderStrategy' while setting bean property 'cloneAbstractOrderStrategy'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultCloneAbstractOrderStrategy' defined in class path resource [order-spring.xml]: Cannot resolve reference to bean 'typeService' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultTypeService' defined in class path resource [servicelayer-spring.xml]: Cannot resolve reference to bean 'converterRegistry' while setting bean property 'converterRegistry'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'defaultConverterRegistry' defined in class path resource [servicelayer-spring.xml]: Unsatisfied dependency expressed through bean property 'commonI18NService': : No qualifying bean of type [de.hybris.platform.servicelayer.i18n.CommonI18NService] is defined: expected single matching bean but found 2: defaultCommonI18NService,defaultcustomCommonI18NService; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [de.hybris.platform.servicelayer.i18n.CommonI18NService] is defined: expected single matching bean but found 2: defaultCommonI18NService,defaultcustomCommonI18NService
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:136) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:408) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1566) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
【问题讨论】:
【参考方案1】:这是因为defaultConverterRegistry
的自动装配策略是byType
:
<bean id="defaultConverterRegistry" ... autowire="byType" >
这意味着 Spring 找到了两个候选 commonI18NService
bean defaultCommonI18NService
和 defaultcustomCommonI18NService
因此不知道要注入哪个。
我建议使用primary="true"
、see 将您的defaultcustomCommonI18NService
设为自动装配的主bean
<bean id="defaultCustomCommonI18NService" class="com.extended.service.impl.DefaultCustomCommonI18NService" parent="defaultCommonI18NService" primary="true" />
【讨论】:
您如何发现defaultConverterRegistry
导致了问题?我无法看到该课程的来源。假设“defaultConverterRegistry”有 commonI18NService 作为@Autowired
它在这里声明:...\hybris\bin\platform\ext\core\resources\servicelayer-spring.xml 并且它存在于您提供的堆栈跟踪中
如果你想查看 DefaultModelConverterRegistry.class 的来源,你需要使用 java 反编译器。
我没有找到任何与DefaultModelConverterRegistry
相关的堆栈跟踪,我还尝试使用反编译器检查DefaultModelConverterRegistry
的来源,但没有运气。我假设这个类有自动连线commonI18NService
这是导致问题。以上是关于覆盖 hybris commonI18NService roundCurrency 方法的主要内容,如果未能解决你的问题,请参考以下文章
Internationalization(i18n) support in SAP CRM,UI5 and Hybris
MacOS Monterey 和 SAP Commerce / Hybris 1905:无法启动 hybrisserver.sh