Skip to content

Commit e9367e1

Browse files
committed
Revise Repositories initialization.
We now no longer declare cacheRepositoryFactory as synchronized to avoid locking. Additionally, simplify the flow and reuse computed values as much as possible. Closes #3126
1 parent 3aee0a2 commit e9367e1

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

src/main/java/org/springframework/data/repository/support/DomainClassConverter.java

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ private Optional<? extends ConditionalGenericConverter> getConverter(TypeDescrip
9191
return repositories.get().hasRepositoryFor(targetType.getType()) ? toEntityConverter : toIdConverter;
9292
}
9393

94+
@Override
9495
public void setApplicationContext(ApplicationContext context) {
9596

9697
this.repositories = Lazy.of(() -> {

src/main/java/org/springframework/data/repository/support/Repositories.java

+22-19
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,14 @@ private void populateRepositoryFactoryInformation(ListableBeanFactory factory) {
9595

9696
for (String name : BeanFactoryUtils.beanNamesForTypeIncludingAncestors(factory, RepositoryFactoryInformation.class,
9797
false, false)) {
98-
cacheRepositoryFactory(name);
98+
cacheRepositoryFactory(factory, name);
9999
}
100100
}
101101

102102
@SuppressWarnings("rawtypes")
103-
private synchronized void cacheRepositoryFactory(String name) {
103+
private void cacheRepositoryFactory(ListableBeanFactory factory, String name) {
104104

105-
RepositoryFactoryInformation repositoryFactoryInformation = beanFactory.get().getBean(name,
105+
RepositoryFactoryInformation repositoryFactoryInformation = factory.getBean(name,
106106
RepositoryFactoryInformation.class);
107107
RepositoryInformation information = repositoryFactoryInformation.getRepositoryInformation();
108108
Class<?> domainType = ClassUtils.getUserClass(information.getDomainType());
@@ -113,8 +113,21 @@ private synchronized void cacheRepositoryFactory(String name) {
113113
typesToRegister.add(domainType);
114114
typesToRegister.addAll(alternativeDomainTypes);
115115

116+
Optional<ConfigurableListableBeanFactory> beanFactory = Optional.of(factory).map(it -> {
117+
118+
if (it instanceof ConfigurableListableBeanFactory) {
119+
return (ConfigurableListableBeanFactory) it;
120+
}
121+
122+
if (it instanceof ConfigurableApplicationContext) {
123+
return ((ConfigurableApplicationContext) it).getBeanFactory();
124+
}
125+
126+
return null;
127+
});
128+
116129
for (Class<?> type : typesToRegister) {
117-
cacheFirstOrPrimary(type, repositoryFactoryInformation, BeanFactoryUtils.transformedBeanName(name));
130+
cacheFirstOrPrimary(beanFactory, type, repositoryFactoryInformation, BeanFactoryUtils.transformedBeanName(name));
118131
}
119132
}
120133

@@ -273,6 +286,7 @@ public List<QueryMethod> getQueryMethodsFor(Class<?> domainClass) {
273286
return getRepositoryFactoryInfoFor(domainClass).getQueryMethods();
274287
}
275288

289+
@Override
276290
public Iterator<Class<?>> iterator() {
277291
return repositoryFactoryInfos.keySet().iterator();
278292
}
@@ -281,29 +295,18 @@ public Iterator<Class<?>> iterator() {
281295
* Caches the repository information for the given domain type or overrides existing information in case the bean name
282296
* points to a primary bean definition.
283297
*
298+
* @param beanFactory must not be {@literal null}.
284299
* @param type must not be {@literal null}.
285300
* @param information must not be {@literal null}.
286301
* @param name must not be {@literal null}.
287302
*/
288303
@SuppressWarnings({ "rawtypes", "unchecked" })
289-
private void cacheFirstOrPrimary(Class<?> type, RepositoryFactoryInformation information, String name) {
304+
private void cacheFirstOrPrimary(Optional<ConfigurableListableBeanFactory> beanFactory, Class<?> type,
305+
RepositoryFactoryInformation information, String name) {
290306

291307
if (repositoryBeanNames.containsKey(type)) {
292308

293-
Optional<ConfigurableListableBeanFactory> factoryToUse = this.beanFactory.map(it -> {
294-
295-
if (it instanceof ConfigurableListableBeanFactory) {
296-
return (ConfigurableListableBeanFactory) it;
297-
}
298-
299-
if (it instanceof ConfigurableApplicationContext) {
300-
return ((ConfigurableApplicationContext) it).getBeanFactory();
301-
}
302-
303-
return null;
304-
});
305-
306-
Boolean presentAndPrimary = factoryToUse.map(it -> it.getMergedBeanDefinition(name)) //
309+
Boolean presentAndPrimary = beanFactory.map(it -> it.getMergedBeanDefinition(name)) //
307310
.map(BeanDefinition::isPrimary) //
308311
.orElse(false);
309312

0 commit comments

Comments
 (0)