Skip to content

Commit 59b74fe

Browse files
committed
DATAJPA-1487 - Fixed bean definition registration for JpaMetamodelCacheCleanup.
Previously, the bean definition was registered as lazy bean which caused it not to be instantiated in the first place and thus – more importantly – the disposal method not being triggered when the ApplicationContext shuts down. We now properly register it as eager bean. Related tickets: DATAJPA-1446.
1 parent 7fd992d commit 59b74fe

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/main/java/org/springframework/data/jpa/repository/config/JpaRepositoryConfigExtension.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public void registerBeansForRoot(BeanDefinitionRegistry registry, RepositoryConf
194194

195195
}, registry, JPA_CONTEXT_BEAN_NAME, source);
196196

197-
registerLazyIfNotAlreadyRegistered(() -> new RootBeanDefinition(JPA_METAMODEL_CACHE_CLEANUP_CLASSNAME), registry,
197+
registerIfNotAlreadyRegistered(() -> new RootBeanDefinition(JPA_METAMODEL_CACHE_CLEANUP_CLASSNAME), registry,
198198
JPA_METAMODEL_CACHE_CLEANUP_CLASSNAME, source);
199199
}
200200

src/test/java/org/springframework/data/jpa/util/JpaMetamodelCacheCleanupIntegrationTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,19 @@
1616
package org.springframework.data.jpa.util;
1717

1818
import static org.assertj.core.api.Assertions.*;
19+
import static org.mockito.Mockito.*;
1920

2021
import javax.persistence.metamodel.Metamodel;
2122

2223
import org.junit.Test;
2324
import org.junit.runner.RunWith;
2425
import org.mockito.Mock;
2526
import org.mockito.junit.MockitoJUnitRunner;
27+
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
2628
import org.springframework.context.support.GenericApplicationContext;
29+
import org.springframework.data.jpa.repository.config.JpaRepositoryConfigExtension;
30+
import org.springframework.data.repository.config.RepositoryConfigurationExtension;
31+
import org.springframework.data.repository.config.RepositoryConfigurationSource;
2732

2833
/**
2934
* Integration tests for {@link JpaMetamodelCacheCleanup}.
@@ -50,4 +55,19 @@ public void wipesJpaMetamodelCacheOnApplicationContextClose() {
5055

5156
assertThat(model).isNotSameAs(JpaMetamodel.of(metamodel));
5257
}
58+
59+
@Test // DATAJPA-1487, DATAJPA-1446
60+
public void registersCleanupBeanAsNonLazy() {
61+
62+
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
63+
RepositoryConfigurationSource configurationSource = mock(RepositoryConfigurationSource.class);
64+
65+
RepositoryConfigurationExtension extension = new JpaRepositoryConfigExtension();
66+
extension.registerBeansForRoot(beanFactory, configurationSource);
67+
68+
String[] cleanupBeanNames = beanFactory.getBeanNamesForType(JpaMetamodelCacheCleanup.class);
69+
70+
assertThat(cleanupBeanNames.length).isEqualTo(1);
71+
assertThat(beanFactory.getBeanDefinition(cleanupBeanNames[0]).isLazyInit()).isFalse();
72+
}
5373
}

0 commit comments

Comments
 (0)