如何为 adviceChain 创建 bean?

How can I create bean for adviceChain?

我很难将此 spring xml 转换为 java 配置。我如何根据下面的 spring 文件定义一个 adviceChain bean。

这是我的 xml 文件:

<util:list id="adviceChain">
   <ref bean="retryInterceptor"/>
</util:list>    
<bean id="retryInterceptor" class="org.springframework.amqp.rabbit.config.StatelessRetryOperationsInterceptorFactoryBean">
    <property name="messageRecoverer">
        <bean class="customDiscardingMessageRecoverer" />
    </property>
    <property name="retryOperations" ref="retryTemplate" />
</bean>     
<bean id="retryTemplate" class="org.springframework.retry.support.RetryTemplate">
    <property name="backOffPolicy">
        <bean class="org.springframework.retry.backoff.FixedBackOffPolicy">
            <property name="backOffPeriod" value="3000" /> 
        </bean>     
    </property>
    <property name="retryPolicy">
        <bean class="org.springframework.retry.policy.SimpleRetryPolicy">
            <property name="maxAttempts" value="3" />
        </bean>     
    </property>
</bean>

这是我尝试过的:

@Bean
StatelessRetryOperationsInterceptorFactoryBean retryInterceptor() {
    StatelessRetryOperationsInterceptorFactoryBean retryInterceptor = new StatelessRetryOperationsInterceptorFactoryBean();
    DiscardingMessageRecoverer messageRecoverer = new DiscardingMessageRecoverer();
    retryInterceptor.setMessageRecoverer(messageRecoverer);
    retryInterceptor.setRetryOperations(retryTemplate());
    return retryInterceptor;
}

@Bean
public RetryTemplate retryTemplate() {
    RetryTemplate retryTemplate = new RetryTemplate();         
    FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
    fixedBackOffPolicy.setBackOffPeriod(3000);
    retryTemplate.setBackOffPolicy(fixedBackOffPolicy); 
    SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
    retryPolicy.setMaxAttempts(3);
    retryTemplate.setRetryPolicy(retryPolicy);         
    return retryTemplate;

@Bean 
SimpleMessageListenerContainer listenerContainer(){
    SimpleMessageListenerContainer listenerContainer = new SimpleMessageListenerContainer();
    listenerContainer.setConnectionFactory(connectionFactory());
    listenerContainer.setConcurrentConsumers(1);
    listenerContainer.setAdviceChain(new Advice[] { (Advice) retryInterceptor() });
    listenerContainer.setMessageListener(listenerAdapter());
    listenerContainer.setQueues(queue());
    return listenerContainer;
}

我收到 org.springframework.amqp.rabbit.config.StatelessRetryOperationsInterceptorFactoryBean$$EnhancerBySpringCGLIB$$e3de830c cannot be cast to org.aopalliance.aop.Advice 错误 listenerContainer.setAdviceChain(new Advice[] { (Advice) retryInterceptor() }); 我认为这是因为我没有像 spring xml 文件那样定义 AdviceChain bean,但到目前为止我不知道如何定义。

像这样编辑 StatelessRetryOperationsInterceptorFactoryBean:

@Bean
RetryOperationsInterceptor retryInterceptor() {
    StatelessRetryOperationsInterceptorFactoryBean retryInterceptor = new StatelessRetryOperationsInterceptorFactoryBean();
    DiscardingMessageRecoverer messageRecoverer = new DiscardingMessageRecoverer();
    retryInterceptor.setMessageRecoverer(messageRecoverer);
    retryInterceptor.setRetryOperations(retryTemplate());
    return retryInterceptor.getObject();
}