Skip to content

Commit e42c5ca

Browse files
committed
Merge branch '6.1.x'
2 parents f96d0a6 + 0e0397a commit e42c5ca

File tree

8 files changed

+71
-49
lines changed

8 files changed

+71
-49
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -823,11 +823,11 @@ protected Class<?> getTypeForFactoryMethod(String beanName, RootBeanDefinition m
823823
/**
824824
* This implementation attempts to query the FactoryBean's generic parameter metadata
825825
* if present to determine the object type. If not present, i.e. the FactoryBean is
826-
* declared as a raw type, checks the FactoryBean's {@code getObjectType} method
826+
* declared as a raw type, it checks the FactoryBean's {@code getObjectType} method
827827
* on a plain instance of the FactoryBean, without bean properties applied yet.
828-
* If this doesn't return a type yet, and {@code allowInit} is {@code true} a
829-
* full creation of the FactoryBean is used as fallback (through delegation to the
830-
* superclass's implementation).
828+
* If this doesn't return a type yet and {@code allowInit} is {@code true}, full
829+
* creation of the FactoryBean is attempted as fallback (through delegation to the
830+
* superclass implementation).
831831
* <p>The shortcut check for a FactoryBean is only applied in case of a singleton
832832
* FactoryBean. If the FactoryBean instance itself is not kept as singleton,
833833
* it will be fully created to check the type of its exposed object.

spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1682,7 +1682,7 @@ protected boolean isFactoryBean(String beanName, RootBeanDefinition mbd) {
16821682
* already. The implementation is allowed to instantiate the target factory bean if
16831683
* {@code allowInit} is {@code true} and the type cannot be determined another way;
16841684
* otherwise it is restricted to introspecting signatures and related metadata.
1685-
* <p>If no {@link FactoryBean#OBJECT_TYPE_ATTRIBUTE} if set on the bean definition
1685+
* <p>If no {@link FactoryBean#OBJECT_TYPE_ATTRIBUTE} is set on the bean definition
16861686
* and {@code allowInit} is {@code true}, the default implementation will create
16871687
* the FactoryBean via {@code getBean} to call its {@code getObjectType} method.
16881688
* Subclasses are encouraged to optimize this, typically by inspecting the generic
Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
3030
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
3131
import org.springframework.beans.factory.support.GenericBeanDefinition;
32+
import org.springframework.beans.factory.support.RootBeanDefinition;
3233
import org.springframework.context.support.GenericApplicationContext;
34+
import org.springframework.core.ResolvableType;
3335
import org.springframework.core.type.AnnotationMetadata;
3436

3537
import static org.assertj.core.api.Assertions.assertThat;
@@ -39,8 +41,9 @@
3941
* {@link FactoryBean FactoryBeans} defined in the configuration.
4042
*
4143
* @author Phillip Webb
44+
* @author Juergen Hoeller
4245
*/
43-
class ConfigurationWithFactoryBeanBeanEarlyDeductionTests {
46+
class ConfigurationWithFactoryBeanEarlyDeductionTests {
4447

4548
@Test
4649
void preFreezeDirect() {
@@ -82,6 +85,16 @@ void postFreezeAttribute() {
8285
assertPostFreeze(AttributeClassConfiguration.class);
8386
}
8487

88+
@Test
89+
void preFreezeTargetType() {
90+
assertPreFreeze(TargetTypeConfiguration.class);
91+
}
92+
93+
@Test
94+
void postFreezeTargetType() {
95+
assertPostFreeze(TargetTypeConfiguration.class);
96+
}
97+
8598
@Test
8699
void preFreezeUnresolvedGenericFactoryBean() {
87100
// Covers the case where a @Configuration is picked up via component scanning
@@ -105,14 +118,13 @@ void preFreezeUnresolvedGenericFactoryBean() {
105118
}
106119
}
107120

121+
108122
private void assertPostFreeze(Class<?> configurationClass) {
109-
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
110-
configurationClass);
123+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(configurationClass);
111124
assertContainsMyBeanName(context);
112125
}
113126

114-
private void assertPreFreeze(Class<?> configurationClass,
115-
BeanFactoryPostProcessor... postProcessors) {
127+
private void assertPreFreeze(Class<?> configurationClass, BeanFactoryPostProcessor... postProcessors) {
116128
NameCollectingBeanFactoryPostProcessor postProcessor = new NameCollectingBeanFactoryPostProcessor();
117129
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
118130
try (context) {
@@ -132,41 +144,38 @@ private void assertContainsMyBeanName(String[] names) {
132144
assertThat(names).containsExactly("myBean");
133145
}
134146

135-
private static class NameCollectingBeanFactoryPostProcessor
136-
implements BeanFactoryPostProcessor {
147+
148+
private static class NameCollectingBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
137149

138150
private String[] names;
139151

140152
@Override
141-
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
142-
throws BeansException {
143-
this.names = beanFactory.getBeanNamesForType(MyBean.class, true, false);
153+
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
154+
ResolvableType typeToMatch = ResolvableType.forClassWithGenerics(MyBean.class, String.class);
155+
this.names = beanFactory.getBeanNamesForType(typeToMatch, true, false);
144156
}
145157

146158
public String[] getNames() {
147159
return this.names;
148160
}
149-
150161
}
151162

152163
@Configuration
153164
static class DirectConfiguration {
154165

155166
@Bean
156-
MyBean myBean() {
157-
return new MyBean();
167+
MyBean<String> myBean() {
168+
return new MyBean<>();
158169
}
159-
160170
}
161171

162172
@Configuration
163173
static class GenericMethodConfiguration {
164174

165175
@Bean
166-
FactoryBean<MyBean> myBean() {
167-
return new TestFactoryBean<>(new MyBean());
176+
FactoryBean<MyBean<String>> myBean() {
177+
return new TestFactoryBean<>(new MyBean<>());
168178
}
169-
170179
}
171180

172181
@Configuration
@@ -176,13 +185,11 @@ static class GenericClassConfiguration {
176185
MyFactoryBean myBean() {
177186
return new MyFactoryBean();
178187
}
179-
180188
}
181189

182190
@Configuration
183191
@Import(AttributeClassRegistrar.class)
184192
static class AttributeClassConfiguration {
185-
186193
}
187194

188195
static class AttributeClassRegistrar implements ImportBeanDefinitionRegistrar {
@@ -191,16 +198,32 @@ static class AttributeClassRegistrar implements ImportBeanDefinitionRegistrar {
191198
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
192199
BeanDefinition definition = BeanDefinitionBuilder.genericBeanDefinition(
193200
RawWithAbstractObjectTypeFactoryBean.class).getBeanDefinition();
194-
definition.setAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE, MyBean.class);
201+
definition.setAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE,
202+
ResolvableType.forClassWithGenerics(MyBean.class, String.class));
195203
registry.registerBeanDefinition("myBean", definition);
196204
}
205+
}
197206

207+
@Configuration
208+
@Import(TargetTypeRegistrar.class)
209+
static class TargetTypeConfiguration {
210+
}
211+
212+
static class TargetTypeRegistrar implements ImportBeanDefinitionRegistrar {
213+
214+
@Override
215+
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
216+
RootBeanDefinition definition = new RootBeanDefinition(RawWithAbstractObjectTypeFactoryBean.class);
217+
definition.setTargetType(ResolvableType.forClassWithGenerics(FactoryBean.class,
218+
ResolvableType.forClassWithGenerics(MyBean.class, String.class)));
219+
registry.registerBeanDefinition("myBean", definition);
220+
}
198221
}
199222

200223
abstract static class AbstractMyBean {
201224
}
202225

203-
static class MyBean extends AbstractMyBean {
226+
static class MyBean<T> extends AbstractMyBean {
204227
}
205228

206229
static class TestFactoryBean<T> implements FactoryBean<T> {
@@ -220,31 +243,26 @@ public T getObject() {
220243
public Class<?> getObjectType() {
221244
return this.instance.getClass();
222245
}
223-
224246
}
225247

226-
static class MyFactoryBean extends TestFactoryBean<MyBean> {
248+
static class MyFactoryBean extends TestFactoryBean<MyBean<String>> {
227249

228250
public MyFactoryBean() {
229-
super(new MyBean());
251+
super(new MyBean<>());
230252
}
231-
232253
}
233254

234255
static class RawWithAbstractObjectTypeFactoryBean implements FactoryBean<Object> {
235256

236-
private final Object object = new MyBean();
237-
238257
@Override
239-
public Object getObject() {
240-
return object;
258+
public Object getObject() throws Exception {
259+
throw new IllegalStateException();
241260
}
242261

243262
@Override
244263
public Class<?> getObjectType() {
245264
return MyBean.class;
246265
}
247-
248266
}
249267

250268
}

spring-jdbc/src/test/java/org/springframework/jdbc/support/rowset/ResultSetWrappingRowSetTests.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@
3939
*/
4040
class ResultSetWrappingRowSetTests {
4141

42-
private ResultSet resultSet = mock();
42+
private final ResultSet resultSet = mock();
4343

44-
private ResultSetWrappingSqlRowSet rowSet = new ResultSetWrappingSqlRowSet(resultSet);
44+
private final ResultSetWrappingSqlRowSet rowSet = new ResultSetWrappingSqlRowSet(resultSet);
4545

4646

4747
@Test
@@ -198,6 +198,7 @@ void testGetBooleanString() throws Exception {
198198
doTest(rset, rowset, "test", true);
199199
}
200200

201+
201202
private void doTest(Method rsetMethod, Method rowsetMethod, Object arg, Object ret) throws Exception {
202203
if (arg instanceof String) {
203204
given(resultSet.findColumn((String) arg)).willReturn(1);
@@ -207,9 +208,9 @@ private void doTest(Method rsetMethod, Method rowsetMethod, Object arg, Object r
207208
given(rsetMethod.invoke(resultSet, arg)).willReturn(ret).willThrow(new SQLException("test"));
208209
}
209210
rowsetMethod.invoke(rowSet, arg);
210-
assertThatExceptionOfType(InvocationTargetException.class).isThrownBy(() ->
211-
rowsetMethod.invoke(rowSet, arg)).
212-
satisfies(ex -> assertThat(ex.getTargetException()).isExactlyInstanceOf(InvalidResultSetAccessException.class));
211+
assertThatExceptionOfType(InvocationTargetException.class)
212+
.isThrownBy(() -> rowsetMethod.invoke(rowSet, arg))
213+
.satisfies(ex -> assertThat(ex.getTargetException()).isExactlyInstanceOf(InvalidResultSetAccessException.class));
213214
}
214215

215216
}

spring-tx/src/main/java/org/springframework/transaction/interceptor/DefaultTransactionAttribute.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class DefaultTransactionAttribute extends DefaultTransactionDefinition im
5151

5252

5353
/**
54-
* Create a new DefaultTransactionAttribute, with default settings.
54+
* Create a new {@code DefaultTransactionAttribute} with default settings.
5555
* Can be modified through bean property setters.
5656
* @see #setPropagationBehavior
5757
* @see #setIsolationLevel
@@ -75,7 +75,7 @@ public DefaultTransactionAttribute(TransactionAttribute other) {
7575
}
7676

7777
/**
78-
* Create a new DefaultTransactionAttribute with the given
78+
* Create a new {@code DefaultTransactionAttribute} with the given
7979
* propagation behavior. Can be modified through bean property setters.
8080
* @param propagationBehavior one of the propagation constants in the
8181
* TransactionDefinition interface

spring-tx/src/main/java/org/springframework/transaction/reactive/AbstractReactiveTransactionManager.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -234,11 +234,13 @@ private Mono<ReactiveTransaction> handleExistingTransaction(TransactionSynchroni
234234
prepareSynchronization(synchronizationManager, status, definition)).thenReturn(status);
235235
}
236236

237-
// Assumably PROPAGATION_SUPPORTS or PROPAGATION_REQUIRED.
237+
// PROPAGATION_REQUIRED, PROPAGATION_SUPPORTS, PROPAGATION_MANDATORY:
238+
// regular participation in existing transaction.
238239
if (debugEnabled) {
239240
logger.debug("Participating in existing transaction");
240241
}
241-
return Mono.just(prepareReactiveTransaction(synchronizationManager, definition, transaction, false, debugEnabled, null));
242+
return Mono.just(prepareReactiveTransaction(
243+
synchronizationManager, definition, transaction, false, debugEnabled, null));
242244
}
243245

244246
/**

spring-tx/src/main/java/org/springframework/transaction/support/AbstractPlatformTransactionManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,8 @@ private TransactionStatus handleExistingTransaction(
491491
}
492492
}
493493

494-
// Assumably PROPAGATION_SUPPORTS or PROPAGATION_REQUIRED.
494+
// PROPAGATION_REQUIRED, PROPAGATION_SUPPORTS, PROPAGATION_MANDATORY:
495+
// regular participation in existing transaction.
495496
if (debugEnabled) {
496497
logger.debug("Participating in existing transaction");
497498
}

spring-tx/src/main/java/org/springframework/transaction/support/DefaultTransactionDefinition.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -90,7 +90,7 @@ public class DefaultTransactionDefinition implements TransactionDefinition, Seri
9090

9191

9292
/**
93-
* Create a new DefaultTransactionDefinition, with default settings.
93+
* Create a new {@code DefaultTransactionDefinition} with default settings.
9494
* Can be modified through bean property setters.
9595
* @see #setPropagationBehavior
9696
* @see #setIsolationLevel
@@ -118,7 +118,7 @@ public DefaultTransactionDefinition(TransactionDefinition other) {
118118
}
119119

120120
/**
121-
* Create a new DefaultTransactionDefinition with the given
121+
* Create a new {@code DefaultTransactionDefinition} with the given
122122
* propagation behavior. Can be modified through bean property setters.
123123
* @param propagationBehavior one of the propagation constants in the
124124
* TransactionDefinition interface

0 commit comments

Comments
 (0)