diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java index 6e3ad642ede..e30989ad96c 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java @@ -911,6 +911,11 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac // static factory method signature or from class inheritance hierarchy... return getTypeForFactoryBeanFromMethod(mbd.getBeanClass(), factoryMethodName); } + + result = getFactoryBeanGeneric(mbd.targetType); + if (result.resolve() != null) { + return result; + } result = getFactoryBeanGeneric(beanType); if (result.resolve() != null) { return result; diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java index 7f7ab566852..5397168264b 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java @@ -2032,6 +2032,16 @@ class DefaultListableBeanFactoryTests { assertBeanNamesForType(FactoryBean.class, false, false); } + @Test // gh-30987 + void getBeanNamesForTypeWithFactoryBeanDefinedAsTargetType() { + RootBeanDefinition beanDefinition = new RootBeanDefinition(TestRepositoryFactoryBean.class); + beanDefinition.setTargetType(ResolvableType.forClassWithGenerics(TestRepositoryFactoryBean.class, + CityRepository.class, Object.class, Object.class)); + lbf.registerBeanDefinition("factoryBean", beanDefinition); + assertBeanNamesForType(TestRepositoryFactoryBean.class, true, false, "&factoryBean"); + assertBeanNamesForType(CityRepository.class, true, false, "factoryBean"); + } + /** * Verifies that a dependency on a {@link FactoryBean} can not * be autowired by name, as & is an illegal character in @@ -3120,6 +3130,25 @@ class DefaultListableBeanFactoryTests { } + public static class TestRepositoryFactoryBean, S, ID extends Serializable> + extends RepositoryFactoryBeanSupport { + + @Override + public T getObject() throws Exception { + throw new IllegalArgumentException("Should not be called"); + } + + @Override + public Class getObjectType() { + throw new IllegalArgumentException("Should not be called"); + } + } + + public record City(String name) {} + + public static class CityRepository implements Repository {} + + public static class LazyInitFactory implements FactoryBean { public boolean initialized = false; diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/client/AbstractSockJsIntegrationTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/client/AbstractSockJsIntegrationTests.java index daf4e5f8a08..a6bae1556d4 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/client/AbstractSockJsIntegrationTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/client/AbstractSockJsIntegrationTests.java @@ -113,8 +113,9 @@ abstract class AbstractSockJsIntegrationTests { this.baseUrl = "http://localhost:" + this.server.getPort(); } + @AfterEach - void teardown() throws Exception { + void teardown() { try { this.sockJsClient.stop(); } @@ -141,6 +142,7 @@ abstract class AbstractSockJsIntegrationTests { } } + protected abstract Class upgradeStrategyConfigClass(); protected abstract WebSocketTestServer createWebSocketTestServer(); @@ -154,6 +156,7 @@ abstract class AbstractSockJsIntegrationTests { this.sockJsClient.start(); } + @Test void echoWebSocket() throws Exception { testEcho(100, createWebSocketTransport(), null); @@ -305,8 +308,8 @@ abstract class AbstractSockJsIntegrationTests { try { Thread.sleep(timeToSleep); } - catch (InterruptedException e) { - throw new IllegalStateException("Interrupted while waiting for " + description, e); + catch (InterruptedException ex) { + throw new IllegalStateException("Interrupted while waiting for " + description, ex); } } throw new IllegalStateException("Timed out waiting for " + description); @@ -333,6 +336,7 @@ abstract class AbstractSockJsIntegrationTests { } } + private static class TestClientHandler extends TextWebSocketHandler { private final BlockingQueue receivedMessages = new LinkedBlockingQueue<>(); @@ -341,7 +345,6 @@ abstract class AbstractSockJsIntegrationTests { private volatile Throwable transportError; - @Override public void afterConnectionEstablished(WebSocketSession session) throws Exception { this.session = session; @@ -376,6 +379,7 @@ abstract class AbstractSockJsIntegrationTests { } } + private static class EchoHandler extends TextWebSocketHandler { @Override @@ -384,21 +388,23 @@ abstract class AbstractSockJsIntegrationTests { } } + private static class TestServerHandler extends TextWebSocketHandler { private WebSocketSession session; @Override - public void afterConnectionEstablished(WebSocketSession session) throws Exception { + public void afterConnectionEstablished(WebSocketSession session) { this.session = session; } - public WebSocketSession awaitSession(long timeToWait) throws InterruptedException { + public WebSocketSession awaitSession(long timeToWait) { awaitEvent(() -> this.session != null, timeToWait, " session"); return this.session; } } + private static class TestFilter implements Filter { private final Map requests = new HashMap<>(); @@ -407,7 +413,6 @@ abstract class AbstractSockJsIntegrationTests { private final Map sendErrorMap = new HashMap<>(); - @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { @@ -418,18 +423,18 @@ abstract class AbstractSockJsIntegrationTests { this.requests.put(uri, headers); for (String suffix : this.sleepDelayMap.keySet()) { - if ((httpRequest).getRequestURI().endsWith(suffix)) { + if (httpRequest.getRequestURI().endsWith(suffix)) { try { Thread.sleep(this.sleepDelayMap.get(suffix)); break; } - catch (InterruptedException e) { - e.printStackTrace(); + catch (InterruptedException ex) { + ex.printStackTrace(); } } } for (String suffix : this.sendErrorMap.keySet()) { - if ((httpRequest).getRequestURI().endsWith(suffix)) { + if (httpRequest.getRequestURI().endsWith(suffix)) { ((HttpServletResponse) response).sendError(this.sendErrorMap.get(suffix)); return; }