Skip to content

Adopt to AOT changes in Commons #4964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>5.0.0-SNAPSHOT</version>
<version>5.0.0-GH-3279-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand All @@ -26,7 +26,7 @@
<properties>
<project.type>multi</project.type>
<dist.id>spring-data-mongodb</dist.id>
<springdata.commons>4.0.0-SNAPSHOT</springdata.commons>
<springdata.commons>4.0.x-GH-3279-SNAPSHOT</springdata.commons>
<mongo>5.4.0</mongo>
<jmh.version>1.19</jmh.version>
</properties>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>5.0.0-SNAPSHOT</version>
<version>5.0.0-GH-3279-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
9 changes: 8 additions & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>5.0.0-SNAPSHOT</version>
<version>5.0.0-GH-3279-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down Expand Up @@ -140,6 +140,13 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>net.javacrumbs.json-unit</groupId>
<artifactId>json-unit-assertj</artifactId>
<version>4.1.0</version>
<scope>test</scope>
</dependency>

<!-- CDI -->
<!-- Dependency order required to build against CDI 1.0 and test with CDI 2.0 -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.data.config.ParsingUtils;
import org.springframework.data.mongodb.repository.aot.AotMongoRepositoryPostProcessor;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.aot.AotMongoRepositoryPostProcessor;
import org.springframework.data.mongodb.repository.support.MongoRepositoryFactoryBean;
import org.springframework.data.mongodb.repository.support.SimpleMongoRepository;
import org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource;
import org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport;
import org.springframework.data.repository.config.XmlRepositoryConfigurationSource;
Expand Down Expand Up @@ -55,6 +56,12 @@ public String getModulePrefix() {
return "mongo";
}

@Override
public String getRepositoryBaseClassName() {
return SimpleMongoRepository.class.getName();
}

@Override
public String getRepositoryFactoryBeanClassName() {
return MongoRepositoryFactoryBean.class.getName();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
import org.springframework.data.config.ParsingUtils;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import org.springframework.data.mongodb.repository.support.ReactiveMongoRepositoryFactoryBean;
import org.springframework.data.mongodb.repository.support.SimpleReactiveMongoRepository;
import org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource;
import org.springframework.data.repository.config.RepositoryConfigurationExtension;
import org.springframework.data.repository.config.XmlRepositoryConfigurationSource;
import org.springframework.data.repository.core.RepositoryMetadata;

import org.w3c.dom.Element;

/**
Expand All @@ -47,7 +49,13 @@ public String getModuleName() {
return "Reactive MongoDB";
}

public String getRepositoryFactoryClassName() {
@Override
public String getRepositoryBaseClassName() {
return SimpleReactiveMongoRepository.class.getName();
}

@Override
public String getRepositoryFactoryBeanClassName() {
return ReactiveMongoRepositoryFactoryBean.class.getName();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@
*/
package org.springframework.data.mongodb.repository.support;

import static org.springframework.data.querydsl.QuerydslUtils.*;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Optional;

import org.jspecify.annotations.Nullable;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
Expand All @@ -35,7 +33,6 @@
import org.springframework.data.mongodb.repository.query.StringBasedAggregation;
import org.springframework.data.mongodb.repository.query.StringBasedMongoQuery;
import org.springframework.data.projection.ProjectionFactory;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.repository.core.NamedQueries;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.RepositoryMetadata;
Expand All @@ -60,6 +57,7 @@ public class MongoRepositoryFactory extends RepositoryFactorySupport {
private final CrudMethodMetadataPostProcessor crudMethodMetadataPostProcessor = new CrudMethodMetadataPostProcessor();
private final MongoOperations operations;
private final MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext;
private MongoRepositoryFragmentsContributor fragmentsContributor = MongoRepositoryFragmentsContributor.DEFAULT;

/**
* Creates a new {@link MongoRepositoryFactory} with the given {@link MongoOperations}.
Expand All @@ -76,6 +74,17 @@ public MongoRepositoryFactory(MongoOperations mongoOperations) {
addRepositoryProxyPostProcessor(crudMethodMetadataPostProcessor);
}

/**
* Configures the {@link MongoRepositoryFragmentsContributor} to be used. Defaults to
* {@link MongoRepositoryFragmentsContributor#DEFAULT}.
*
* @param fragmentsContributor
* @since 5.0
*/
public void setFragmentsContributor(MongoRepositoryFragmentsContributor fragmentsContributor) {
this.fragmentsContributor = fragmentsContributor;
}

@Override
public void setBeanClassLoader(@Nullable ClassLoader classLoader) {

Expand All @@ -99,33 +108,18 @@ protected RepositoryFragments getRepositoryFragments(RepositoryMetadata metadata
}

/**
* Creates {@link RepositoryFragments} based on {@link RepositoryMetadata} to add Mongo-specific extensions. Typically
* adds a {@link QuerydslMongoPredicateExecutor} if the repository interface uses Querydsl.
* Creates {@link RepositoryFragments} based on {@link RepositoryMetadata} to add Mongo-specific extensions.
* Typically, adds a {@link QuerydslMongoPredicateExecutor} if the repository interface uses Querydsl.
* <p>
* Can be overridden by subclasses to customize {@link RepositoryFragments}.
* Built-in fragment contribution can be customized by configuring {@link MongoRepositoryFragmentsContributor}.
*
* @param metadata repository metadata.
* @param operations the MongoDB operations manager.
* @return
* @return {@link RepositoryFragments} to be added to the repository.
* @since 3.2.1
*/
protected RepositoryFragments getRepositoryFragments(RepositoryMetadata metadata, MongoOperations operations) {

boolean isQueryDslRepository = QUERY_DSL_PRESENT
&& QuerydslPredicateExecutor.class.isAssignableFrom(metadata.getRepositoryInterface());

if (isQueryDslRepository) {

if (metadata.isReactiveRepository()) {
throw new InvalidDataAccessApiUsageException(
"Cannot combine Querydsl and reactive repository support in a single interface");
}

return RepositoryFragments
.just(new QuerydslMongoPredicateExecutor<>(getEntityInformation(metadata.getDomainType()), operations));
}

return RepositoryFragments.empty();
return fragmentsContributor.contribute(metadata, getEntityInformation(metadata.getDomainType()), operations);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@
* {@link org.springframework.beans.factory.FactoryBean} to create {@link MongoRepository} instances.
*
* @author Oliver Gierke
* @author Mark Paluch
*/
@SuppressWarnings("NullAway")
public class MongoRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable>
extends RepositoryFactoryBeanSupport<T, S, ID> {

private @Nullable MongoOperations operations;
private MongoRepositoryFragmentsContributor repositoryFragmentsContributor = MongoRepositoryFragmentsContributor.DEFAULT;
private boolean createIndexesForQueryMethods = false;
private boolean mappingContextConfigured = false;

Expand All @@ -57,6 +59,22 @@ public void setMongoOperations(MongoOperations operations) {
this.operations = operations;
}

@Override
public MongoRepositoryFragmentsContributor getRepositoryFragmentsContributor() {
return repositoryFragmentsContributor;
}

/**
* Configures the {@link MongoRepositoryFragmentsContributor} to contribute built-in fragment functionality to the
* repository.
*
* @param repositoryFragmentsContributor must not be {@literal null}.
* @since 5.0
*/
public void setRepositoryFragmentsContributor(MongoRepositoryFragmentsContributor repositoryFragmentsContributor) {
this.repositoryFragmentsContributor = repositoryFragmentsContributor;
}

/**
* Configures whether to automatically create indexes for the properties referenced in a query method.
*
Expand All @@ -76,7 +94,8 @@ public void setMappingContext(MappingContext<?, ?> mappingContext) {
@Override
protected RepositoryFactorySupport createRepositoryFactory() {

RepositoryFactorySupport factory = getFactoryInstance(operations);
MongoRepositoryFactory factory = getFactoryInstance(operations);
factory.setFragmentsContributor(repositoryFragmentsContributor);

if (createIndexesForQueryMethods) {
factory.addQueryCreationListener(
Expand All @@ -92,7 +111,7 @@ protected RepositoryFactorySupport createRepositoryFactory() {
* @param operations
* @return
*/
protected RepositoryFactorySupport getFactoryInstance(MongoOperations operations) {
protected MongoRepositoryFactory getFactoryInstance(MongoOperations operations) {
return new MongoRepositoryFactory(operations);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.repository.support;

import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.core.support.RepositoryComposition;
import org.springframework.data.repository.core.support.RepositoryFragmentsContributor;
import org.springframework.util.Assert;

/**
* MongoDB-specific {@link RepositoryFragmentsContributor} contributing fragments based on the repository.
* <p>
* Implementations must define a no-args constructor.
*
* @author Mark Paluch
* @since 5.0
* @see QuerydslMongoPredicateExecutor
*/
public interface MongoRepositoryFragmentsContributor extends RepositoryFragmentsContributor {

MongoRepositoryFragmentsContributor DEFAULT = QuerydslContributor.INSTANCE;

/**
* Returns a composed {@code MongoRepositoryFragmentsContributor} that first applies this contributor to its inputs,
* and then applies the {@code after} contributor concatenating effectively both results. If evaluation of either
* contributors throws an exception, it is relayed to the caller of the composed contributor.
*
* @param after the contributor to apply after this contributor is applied.
* @return a composed contributor that first applies this contributor and then applies the {@code after} contributor.
*/
default MongoRepositoryFragmentsContributor andThen(MongoRepositoryFragmentsContributor after) {

Assert.notNull(after, "MongoRepositoryFragmentsContributor must not be null");

return new MongoRepositoryFragmentsContributor() {

@Override
public RepositoryComposition.RepositoryFragments contribute(RepositoryMetadata metadata,
MongoEntityInformation<?, ?> entityInformation, MongoOperations operations) {
return MongoRepositoryFragmentsContributor.this.contribute(metadata, entityInformation, operations)
.append(after.contribute(metadata, entityInformation, operations));
}

@Override
public RepositoryComposition.RepositoryFragments describe(RepositoryMetadata metadata) {
return MongoRepositoryFragmentsContributor.this.describe(metadata).append(after.describe(metadata));
}
};
}

/**
* Creates {@link RepositoryComposition.RepositoryFragments} based on {@link RepositoryMetadata} to add
* MongoDB-specific extensions.
*
* @param metadata repository metadata.
* @param entityInformation must not be {@literal null}.
* @param operations must not be {@literal null}.
* @return {@link RepositoryComposition.RepositoryFragments} to be added to the repository.
*/
RepositoryComposition.RepositoryFragments contribute(RepositoryMetadata metadata,
MongoEntityInformation<?, ?> entityInformation, MongoOperations operations);

}
Loading