RefreshScope无法刷新HessianProxyFactoryBean问题求助
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:
HessianProxyFactoryBeancreates a static proxy instance during itsafterPropertiesSet()initialization phase. Once this proxy is built, it’s permanently bound to the initialserviceUrl—updating the URL later won’t modify the existing proxy’s target.- Annotating the entire
@Configurationclass with@RefreshScopedoesn’t guarantee theinitSampleBean()method will re-run to create a freshHessianProxyFactoryBeanwhen 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/refreshendpoint 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

