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 2002-2015 the original author or authors.
* Copyright 2002-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.
@@ -26,7 +26,6 @@ import org.jxmpp.util.XmppStringUtils;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.config.AbstractFactoryBean;
import org.springframework.context.SmartLifecycle;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
@@ -44,10 +43,10 @@ import org.springframework.util.StringUtils;
*/
public class XmppConnectionFactoryBean extends AbstractFactoryBean<XMPPConnection> implements SmartLifecycle {
private final XMPPTCPConnectionConfiguration connectionConfiguration;
private final Object lifecycleMonitor = new Object();
private XMPPTCPConnectionConfiguration connectionConfiguration;
private volatile String resource; // server will generate resource if not provided
private volatile String user;
@@ -72,11 +71,24 @@ public class XmppConnectionFactoryBean extends AbstractFactoryBean<XMPPConnectio
public XmppConnectionFactoryBean() {
this.connectionConfiguration = null;
}
/**
* @param connectionConfiguration the {@link XMPPTCPConnectionConfiguration} to use.
* @deprecated since {@literal 4.2.5} in favor of {@link #setConnectionConfiguration(XMPPTCPConnectionConfiguration)}
* to avoid {@code BeanCurrentlyInCreationException}
* during {@code AbstractAutowireCapableBeanFactory.getSingletonFactoryBeanForTypeCheck()}
*/
@Deprecated
public XmppConnectionFactoryBean(XMPPTCPConnectionConfiguration connectionConfiguration) {
Assert.notNull(connectionConfiguration, "'connectionConfiguration' must not be null");
this.connectionConfiguration = connectionConfiguration;
}
/**
* @param connectionConfiguration the {@link XMPPTCPConnectionConfiguration} to use.
* @since 4.2.5
*/
public void setConnectionConfiguration(XMPPTCPConnectionConfiguration connectionConfiguration) {
this.connectionConfiguration = connectionConfiguration;
}
@@ -144,6 +156,7 @@ public class XmppConnectionFactoryBean extends AbstractFactoryBean<XMPPConnectio
return this.connection;
}
@Override
public void start() {
synchronized (this.lifecycleMonitor) {
if (this.running) {
@@ -165,6 +178,7 @@ public class XmppConnectionFactoryBean extends AbstractFactoryBean<XMPPConnectio
}
}
@Override
public void stop() {
synchronized (this.lifecycleMonitor) {
if (this.isRunning()) {
@@ -174,19 +188,23 @@ public class XmppConnectionFactoryBean extends AbstractFactoryBean<XMPPConnectio
}
}
@Override
public void stop(Runnable callback) {
stop();
callback.run();
}
@Override
public boolean isRunning() {
return this.running;
}
@Override
public int getPhase() {
return this.phase;
}
@Override
public boolean isAutoStartup() {
return this.autoStartup;
}
@@ -194,22 +212,27 @@ public class XmppConnectionFactoryBean extends AbstractFactoryBean<XMPPConnectio
private class LoggingConnectionListener implements ConnectionListener {
@Override
public void reconnectionSuccessful() {
logger.debug("Reconnection successful");
}
@Override
public void reconnectionFailed(Exception e) {
logger.debug("Reconnection failed", e);
}
@Override
public void reconnectingIn(int seconds) {
logger.debug("Reconnecting in " + seconds + " seconds");
}
@Override
public void connectionClosedOnError(Exception e) {
logger.debug("Connection closed on error", e);
}
@Override
public void connectionClosed() {
logger.debug("Connection closed");
}