You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RefreshScope无法刷新HessianProxyFactoryBean问题求助

Problem: @RefreshScope Fails to Refresh HessianProxyFactoryBean Instances

I’ve run into similar headaches with HessianProxyFactoryBean and @RefreshScope before—let’s break down why this isn’t working for you and walk through actionable fixes:

Why This Isn’t Working

The core issue boils down to how HessianProxyFactoryBean operates combined with Spring’s refresh scope behavior:

  1. HessianProxyFactoryBean creates a static proxy instance during its afterPropertiesSet() initialization phase. Once this proxy is built, it’s permanently bound to the initial serviceUrl—updating the URL later won’t modify the existing proxy’s target.
  2. Annotating the entire @Configuration class with @RefreshScope doesn’t guarantee the initSampleBean() method will re-run to create a fresh HessianProxyFactoryBean when configs change. The scope annotation needs to be targeted directly at the bean creation logic.

Solutions

1. Move @RefreshScope to the @Bean Method

Instead of applying @RefreshScope to the whole configuration class, attach it directly to the bean definition method. This tells Spring to re-execute the bean creation logic whenever the scope is refreshed:

@Configuration
public class HessianConfiguration { 
    @Value("${sample.hessian.url}") 
    private String sampleUrl; 

    @Bean 
    @RefreshScope // Apply scope directly to the bean creation logic
    public HessianProxyFactoryBean initSampleBean() { 
        HessianProxyFactoryBean invoker = new HessianProxyFactoryBean(); 
        invoker.setServiceUrl(sampleUrl); 
        invoker.setServiceInterface(Sample.class); 
        return invoker; 
    } 
}

2. Manual Refresh via RefreshEvent Listener

If the first approach doesn’t work, you can explicitly listen for refresh events and reinitialize the Hessian proxy manually:

Create a component that reacts to RefreshEvent updates and resets the proxy’s target URL:

@Component
@RefreshScope
public class HessianProxyRefresher implements ApplicationListener<RefreshEvent> {

    @Value("${sample.hessian.url}")
    private String sampleUrl;

    @Autowired
    private HessianProxyFactoryBean sampleBean;

    @Override
    public void onApplicationEvent(RefreshEvent event) {
        // Update the URL and trigger proxy re-creation
        sampleBean.setServiceUrl(sampleUrl);
        try {
            sampleBean.afterPropertiesSet(); // Rebuilds the proxy with the new URL
        } catch (Exception e) {
            throw new RuntimeException("Failed to refresh Hessian proxy", e);
        }
    }
}

3. Bypass FactoryBean: Create Hessian Proxy Directly

For more control, skip HessianProxyFactoryBean entirely and build the proxy manually with HessianProxyFactory. This ensures a fresh proxy instance is created every time the scope refreshes:

@Configuration
public class HessianConfiguration { 
    @Value("${sample.hessian.url}") 
    private String sampleUrl; 

    @Bean 
    @RefreshScope
    public Sample sampleService() throws MalformedURLException {
        HessianProxyFactory factory = new HessianProxyFactory();
        // Optional: Add factory configs (like authentication) here
        return (Sample) factory.create(Sample.class, sampleUrl);
    } 
}

Key Notes

  • Make sure you’re triggering the refresh correctly (e.g., via the /actuator/refresh endpoint if using Spring Boot Actuator).
  • When injecting the scoped Hessian bean, rely on Spring’s default scoped proxying (enabled automatically with @RefreshScope) to ensure you always get the latest instance.

内容的提问来源于stack exchange,提问作者Arun Xavier

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:35:43