我春天啓動應用程序,目前已經是一個bean創建一個自動configuraton類從一個依賴的jar像波紋管:如何在彈簧引導應用程序中替換現有的bean?
@Bean
@Order(100)
public StaticRouteLocator staticRouteLocator(AdminServerProperties admin) {
Collection<ZuulRoute> routes = Collections
.singleton(new ZuulRoute(admin.getContextPath() + "/api/turbine/stream/**",
properties.getUrl().toString()));
return new StaticRouteLocator(routes, server.getServletPrefix(), zuulProperties);
}
現在我要替換這個bean,但我仍然需要這個jar它有這種不必要的豆創建。所以我在我的主要自動配置類中添加了另一個bean創建方法,如下所示:
@Bean(name="patchedStaticRouteLocator")
@Order(10)
@Primary
@ConditionalOnMissingBean
public StaticRouteLocator patchedStaticRouteLocator(AdminServerProperties admin) {
Collection<ZuulProperties.ZuulRoute> routes = Collections
.singleton(new ZuulProperties.ZuulRoute(admin.getContextPath(),
properties.getUrl().toString()));
return new StaticRouteLocator(routes, server.getServletPrefix(), zuulProperties);
}
但是,這無法取代目標bean。錯誤信息清晰易懂:
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.cloud.netflix.zuul.filters.RouteLocator] is defined: more than one 'primary' bean found among candidates: [routeLocator, patchedStaticRouteLocator, staticRouteLocator, compositeRouteLocator, applicationRouteLocator]
我的問題什麼是正確的方式來取代這種現有的bean在春季啓動?提前致謝。
感謝它清理我的一些混亂的,雖然如預期這個解決方案沒有工作上面的指示。不過,我通過實現一個BeanPostProcessor組件來完成它。 – leo
不客氣。不過很高興你找到了一個可行的解決方案 –