如何为adviceChain创建bean?

问题描述 投票:0回答:1

我很难将这个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;
}

我无法在listenerContainer.setAdviceChain(new Advice [] {(Advice)retryInterceptor()});我认为这是因为我没有像Spring xml文件那样定义AdviceChain bean,但到目前为止我还不知道如何。

我很难将这个Spring xml转换为Java配置。如何在下面的spring文件中定义adviceChain bean。这是我的xml文件:

[[[<...>编辑像这样创建StatelessRetryOperationsInterceptorFactoryBean:

spring spring-boot javabeans spring-java-config
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.