java - 重写 hybris commonI18NService roundCurrency 方法

标签 java spring dependency-injection sap-commerce-cloud

尝试使用 Alias 覆盖 spring bean

我想覆盖 commonI18NServiceroundCurrency 方法

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;
    }
} 

注入(inject)自定义 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]

最佳答案

这是因为 defaultConverterRegistry 的 Autowiring 策略是 byType:

<bean id="defaultConverterRegistry" ... autowire="byType" >

这意味着 Spring 找到了两个候选 commonI18NService bean defaultCommonI18NServicedefaultcustomCommonI18NService 因此不知道是哪个注入(inject)。

我建议使用 primary="true", see 使您的 defaultcustomCommonI18NService 成为 Autowiring 的主要 bean

<bean id="defaultCustomCommonI18NService" class="com.extended.service.impl.DefaultCustomCommonI18NService"  parent="defaultCommonI18NService" primary="true" />

关于java - 重写 hybris commonI18NService roundCurrency 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44328980/

相关文章:

java - 谷歌吉斯。使用 Java EE5 时注入(inject) EJB

design-patterns - 如果已知具体类,为什么要使用 IoC/DI?

java - 多数据库应用SpringBoot、Hibernate和JPA

java - 安卓耳机插孔

java - HelloWorld SpringMVC - HTTP 状态 404

dependency-injection - 在 dotnetcore DI 世界中与助手一起工作

java - 返回 LinkedHashMap 的第一个键

java - 键入 Cmd-W 时,Mac OS X 上的 SWT 应用程序不会关闭

java - Spring AOP生成的代理类所在位置

java - Rest Template - 如何在发送之前检索原始 POST 请求字符串