Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spring Boot Starter : override bean cause cyclic error #517

Closed
zsobolsky opened this issue May 28, 2019 · 2 comments
Closed

Spring Boot Starter : override bean cause cyclic error #517

zsobolsky opened this issue May 28, 2019 · 2 comments

Comments

@zsobolsky
Copy link

following code causes cyclic error in spring-boot.
Please some help to fix this.

code:

	@Autowired
	public Logbook logbook;

	@Bean
	public RestTemplate restapi() throws Exception {

		CloseableHttpClient httpclient = HttpClientBuilder.create()
			.addInterceptorFirst(new LogbookHttpRequestInterceptor(logbook))
			.addInterceptorFirst(new LogbookHttpResponseInterceptor())
			.build();
		RestTemplate resttemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory(httpclient));
		return resttemplate;
	} 
	
	@Bean
	public BodyFilter bodyFilter() {
		return BodyFilter.merge(new PrettyPrintingJsonBodyFilter(), BodyFilters.defaultValue());
	}
***************************
APPLICATION FAILED TO START
***************************

Description:

The dependencies of some of the beans in the application context form a cycle:

   secureLogbookFilter defined in class path resource [org/zalando/logbook/autoconfigure/LogbookAutoConfiguration$SecurityServletFilterConfiguration.class]
+-----+
|  logbook defined in class path resource [org/zalando/logbook/autoconfigure/LogbookAutoConfiguration.class]
?     ?
|  restAPIConfig (field public org.zalando.logbook.Logbook com.restapi.jconfig.RestAPIConfig.logbook)
+-----+

When commenting
@Autowired public Logbook logbook
or
@bean public BodyFilter bodyFilter() {},
the problem issue is gone.

@whiskeysierra
Copy link
Collaborator

whiskeysierra commented May 28, 2019

Your configuration both depends on Logbook but also provides a part of its configuration. Two options:

  1. Split up your configuration into two different ones.
  2. Make logbook a parameter of the restapi method, rather than an instance field, like so:
@Bean
public RestTemplate restapi(final Logbook logbook) throws Exception {
	CloseableHttpClient httpclient = HttpClientBuilder.create()
		.addInterceptorFirst(new LogbookHttpRequestInterceptor(logbook))
		.addInterceptorFirst(new LogbookHttpResponseInterceptor())
		.build();
	return new RestTemplate(new HttpComponentsClientHttpRequestFactory(httpclient));
} 

@zsobolsky
Copy link
Author

issue is fixed with 2nd option

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants