INT-3916: Don't Use CTOR Injection in FactoryBean

JIRA: https://jira.spring.io/browse/INT-3916

The `JpaOutboundGatewayFactoryBean` used CTOR injection for the `JpaExecutor`.
That one, in turn, uses CTOR injection for the `EntityManagerFactory`.

Such a dependency may cause the `early bean instantiating` in case of `AbstractAutowireCapableBeanFactory.getSingletonFactoryBeanForTypeCheck()`.
And we end up with the `BeanCurrentlyInCreationException`.

Therefore no one `FactoryBean` should use CTOR injection if there is a potential hierarchical dependency.

NOTE: there is no tests on the matter, since we don't change the components behavior.
The `JPA` sample application will be changed to the Boot to track this fix.

**Cherry-pick to 4.2.x**

Address PR comments and fix other `FactoryBean`s for the same issue, when it is reasonable

Polishing

Address PR comments

Make setter `setSockJsTaskScheduler` as `public`
This commit is contained in:
Artem Bilan
2015-12-17 18:05:31 -05:00
committed by Gary Russell
parent 2be12160e9
commit fec2a36f42
15 changed files with 310 additions and 149 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -33,17 +33,18 @@ import org.springframework.integration.zookeeper.leader.LeaderInitiator;
* Creates a {@link LeaderInitiator}.
*
* @author Gary Russell
* @author Artem Bilan
* @since 4.2
*
*/
public class LeaderInitiatorFactoryBean
implements FactoryBean<LeaderInitiator>, SmartLifecycle, InitializingBean, ApplicationEventPublisherAware {
private final CuratorFramework client;
private CuratorFramework client;
private final Candidate candidate;
private Candidate candidate;
private final String path;
private String path;
private LeaderInitiator leaderInitiator;
@@ -53,16 +54,38 @@ public class LeaderInitiatorFactoryBean
private ApplicationEventPublisher applicationEventPublisher;
public LeaderInitiatorFactoryBean() {
}
/**
* Construct the instance.
* @param client the {@link CuratorFramework}.
* @param path the path in zookeeper.
* @param role the role of the leader.
* @deprecated since {@literal 4.2.5} in favor of appropriate setters
* to avoid {@code BeanCurrentlyInCreationException}
* during {@code AbstractAutowireCapableBeanFactory.getSingletonFactoryBeanForTypeCheck()}
*/
@Deprecated
public LeaderInitiatorFactoryBean(CuratorFramework client, String path, String role) {
this.client = client;
this.candidate = new DefaultCandidate(UUID.randomUUID().toString(), role);
this.path = path;
this.candidate = new DefaultCandidate(UUID.randomUUID().toString(), role);
}
public LeaderInitiatorFactoryBean setClient(CuratorFramework client) {
this.client = client;
return this;
}
public LeaderInitiatorFactoryBean setPath(String path) {
this.path = path;
return this;
}
public LeaderInitiatorFactoryBean setRole(String role) {
this.candidate = new DefaultCandidate(UUID.randomUUID().toString(), role);
return this;
}
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -44,6 +44,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Gary Russell
* @author Artem Bilan
* @since 4.2
*
*/
@@ -85,7 +86,10 @@ public class LeaderInitiatorFactoryBeanTests extends ZookeeperTestSupport {
@Bean
public LeaderInitiatorFactoryBean leaderInitiator(CuratorFramework client) {
return new LeaderInitiatorFactoryBean(client, "/siTest/", "foo");
return new LeaderInitiatorFactoryBean()
.setClient(client)
.setPath("/siTest/")
.setRole("foo");
}
@Bean