|
18 | 18 | import io.r2dbc.spi.ConnectionFactory; |
19 | 19 |
|
20 | 20 | import java.util.ArrayList; |
| 21 | +import java.util.Collection; |
21 | 22 | import java.util.Collections; |
| 23 | +import java.util.HashSet; |
22 | 24 | import java.util.List; |
23 | 25 | import java.util.Optional; |
| 26 | +import java.util.Set; |
24 | 27 |
|
25 | 28 | import org.springframework.beans.BeansException; |
| 29 | +import org.springframework.beans.factory.config.BeanDefinition; |
26 | 30 | import org.springframework.context.ApplicationContext; |
27 | 31 | import org.springframework.context.ApplicationContextAware; |
28 | 32 | import org.springframework.context.annotation.Bean; |
| 33 | +import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; |
29 | 34 | import org.springframework.context.annotation.Configuration; |
30 | 35 | import org.springframework.core.convert.converter.Converter; |
| 36 | +import org.springframework.core.type.filter.AnnotationTypeFilter; |
31 | 37 | import org.springframework.data.convert.CustomConversions; |
32 | 38 | import org.springframework.data.convert.CustomConversions.StoreConversions; |
33 | 39 | import org.springframework.data.r2dbc.convert.MappingR2dbcConverter; |
|
39 | 45 | import org.springframework.data.r2dbc.dialect.DialectResolver; |
40 | 46 | import org.springframework.data.r2dbc.dialect.R2dbcDialect; |
41 | 47 | import org.springframework.data.r2dbc.mapping.R2dbcMappingContext; |
| 48 | +import org.springframework.data.relational.RelationalManagedTypes; |
42 | 49 | import org.springframework.data.relational.core.conversion.BasicRelationalConverter; |
43 | 50 | import org.springframework.data.relational.core.mapping.NamingStrategy; |
| 51 | +import org.springframework.data.relational.core.mapping.Table; |
44 | 52 | import org.springframework.lang.Nullable; |
45 | 53 | import org.springframework.r2dbc.core.DatabaseClient; |
46 | 54 | import org.springframework.util.Assert; |
| 55 | +import org.springframework.util.ClassUtils; |
| 56 | +import org.springframework.util.StringUtils; |
47 | 57 |
|
48 | 58 | /** |
49 | 59 | * Base class for Spring Data R2DBC configuration containing bean declarations that must be registered for Spring Data |
@@ -78,14 +88,43 @@ public void setApplicationContext(ApplicationContext applicationContext) throws |
78 | 88 | */ |
79 | 89 | public abstract ConnectionFactory connectionFactory(); |
80 | 90 |
|
| 91 | + /** |
| 92 | + * Returns the base packages to scan for R2DBC mapped entities at startup. Returns the package name of the |
| 93 | + * configuration class' (the concrete class, not this one here) by default. So if you have a |
| 94 | + * {@code com.acme.AppConfig} extending {@link AbstractR2dbcConfiguration} the base package will be considered |
| 95 | + * {@code com.acme} unless the method is overridden to implement alternate behavior. |
| 96 | + * |
| 97 | + * @return the base packages to scan for mapped {@link Table} classes or an empty collection to not enable scanning |
| 98 | + * for entities. |
| 99 | + * @since 3.0 |
| 100 | + */ |
| 101 | + protected Collection<String> getMappingBasePackages() { |
| 102 | + |
| 103 | + Package mappingBasePackage = getClass().getPackage(); |
| 104 | + return Collections.singleton(mappingBasePackage == null ? null : mappingBasePackage.getName()); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Returns the a {@link RelationalManagedTypes} object holding the initial entity set. |
| 109 | + * |
| 110 | + * @return new instance of {@link RelationalManagedTypes}. |
| 111 | + * @throws ClassNotFoundException |
| 112 | + * @since 3.0 |
| 113 | + */ |
| 114 | + @Bean |
| 115 | + public RelationalManagedTypes r2dbcManagedTypes() throws ClassNotFoundException { |
| 116 | + return RelationalManagedTypes.fromIterable(getInitialEntitySet()); |
| 117 | + } |
| 118 | + |
81 | 119 | /** |
82 | 120 | * Return a {@link R2dbcDialect} for the given {@link ConnectionFactory}. This method attempts to resolve a |
83 | 121 | * {@link R2dbcDialect} from {@link io.r2dbc.spi.ConnectionFactoryMetadata}. Override this method to specify a dialect |
84 | 122 | * instead of attempting to resolve one. |
85 | 123 | * |
86 | 124 | * @param connectionFactory the configured {@link ConnectionFactory}. |
87 | 125 | * @return the resolved {@link R2dbcDialect}. |
88 | | - * @throws org.springframework.data.r2dbc.dialect.DialectResolver.NoDialectException if the {@link R2dbcDialect} cannot be determined. |
| 126 | + * @throws org.springframework.data.r2dbc.dialect.DialectResolver.NoDialectException if the {@link R2dbcDialect} |
| 127 | + * cannot be determined. |
89 | 128 | */ |
90 | 129 | public R2dbcDialect getDialect(ConnectionFactory connectionFactory) { |
91 | 130 | return DialectResolver.getDialect(connectionFactory); |
@@ -131,17 +170,20 @@ public R2dbcEntityTemplate r2dbcEntityTemplate(DatabaseClient databaseClient, |
131 | 170 | * |
132 | 171 | * @param namingStrategy optional {@link NamingStrategy}. Use {@link NamingStrategy#INSTANCE} as fallback. |
133 | 172 | * @param r2dbcCustomConversions customized R2DBC conversions. |
| 173 | + * @param r2dbcManagedTypes R2DBC managed types, typically discovered through {@link #r2dbcManagedTypes() an entity |
| 174 | + * scan}. |
134 | 175 | * @return must not be {@literal null}. |
135 | 176 | * @throws IllegalArgumentException if any of the required args is {@literal null}. |
136 | 177 | */ |
137 | 178 | @Bean |
138 | 179 | public R2dbcMappingContext r2dbcMappingContext(Optional<NamingStrategy> namingStrategy, |
139 | | - R2dbcCustomConversions r2dbcCustomConversions) { |
| 180 | + R2dbcCustomConversions r2dbcCustomConversions, RelationalManagedTypes r2dbcManagedTypes) { |
140 | 181 |
|
141 | 182 | Assert.notNull(namingStrategy, "NamingStrategy must not be null"); |
142 | 183 |
|
143 | 184 | R2dbcMappingContext context = new R2dbcMappingContext(namingStrategy.orElse(NamingStrategy.INSTANCE)); |
144 | 185 | context.setSimpleTypeHolder(r2dbcCustomConversions.getSimpleTypeHolder()); |
| 186 | + context.setManagedTypes(r2dbcManagedTypes); |
145 | 187 |
|
146 | 188 | return context; |
147 | 189 | } |
@@ -239,4 +281,56 @@ ConnectionFactory lookupConnectionFactory() { |
239 | 281 |
|
240 | 282 | return connectionFactory(); |
241 | 283 | } |
| 284 | + |
| 285 | + /** |
| 286 | + * Scans the mapping base package for classes annotated with {@link Table}. By default, it scans for entities in all |
| 287 | + * packages returned by {@link #getMappingBasePackages()}. |
| 288 | + * |
| 289 | + * @see #getMappingBasePackages() |
| 290 | + * @return |
| 291 | + * @throws ClassNotFoundException |
| 292 | + * @since 3.0 |
| 293 | + */ |
| 294 | + protected Set<Class<?>> getInitialEntitySet() throws ClassNotFoundException { |
| 295 | + |
| 296 | + Set<Class<?>> initialEntitySet = new HashSet<>(); |
| 297 | + |
| 298 | + for (String basePackage : getMappingBasePackages()) { |
| 299 | + initialEntitySet.addAll(scanForEntities(basePackage)); |
| 300 | + } |
| 301 | + |
| 302 | + return initialEntitySet; |
| 303 | + } |
| 304 | + |
| 305 | + /** |
| 306 | + * Scans the given base package for entities, i.e. R2DBC-specific types annotated with {@link Table}. |
| 307 | + * |
| 308 | + * @param basePackage must not be {@literal null}. |
| 309 | + * @return |
| 310 | + * @throws ClassNotFoundException |
| 311 | + * @since 3.0 |
| 312 | + */ |
| 313 | + protected Set<Class<?>> scanForEntities(String basePackage) throws ClassNotFoundException { |
| 314 | + |
| 315 | + if (!StringUtils.hasText(basePackage)) { |
| 316 | + return Collections.emptySet(); |
| 317 | + } |
| 318 | + |
| 319 | + Set<Class<?>> initialEntitySet = new HashSet<>(); |
| 320 | + |
| 321 | + if (StringUtils.hasText(basePackage)) { |
| 322 | + |
| 323 | + ClassPathScanningCandidateComponentProvider componentProvider = new ClassPathScanningCandidateComponentProvider( |
| 324 | + false); |
| 325 | + componentProvider.addIncludeFilter(new AnnotationTypeFilter(Table.class)); |
| 326 | + |
| 327 | + for (BeanDefinition candidate : componentProvider.findCandidateComponents(basePackage)) { |
| 328 | + |
| 329 | + initialEntitySet |
| 330 | + .add(ClassUtils.forName(candidate.getBeanClassName(), AbstractR2dbcConfiguration.class.getClassLoader())); |
| 331 | + } |
| 332 | + } |
| 333 | + |
| 334 | + return initialEntitySet; |
| 335 | + } |
242 | 336 | } |
0 commit comments