From 4c6279e4ea4515cd7ad3314d1ba4f4b7acf675f1 Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Tue, 14 Jan 2020 08:14:00 +0100 Subject: [PATCH] DATAJPA-1497 - Polishing. Moved JpaQueryMethodFactory creation into a separate class. Removed http URL in copyright statement. Simplified bean injection of JpaQueryMethodFactory.build Originial pull request: #305. Signed-off-by: Jens Schauder --- .../query/DefaultJpaQueryMethodFactory.java | 46 +++++++++++++++++++ .../jpa/repository/query/JpaQueryMethod.java | 14 ------ .../query/JpaQueryMethodFactory.java | 10 ++-- .../support/JpaRepositoryFactory.java | 3 +- .../support/JpaRepositoryFactoryBean.java | 16 ++++--- .../JpaQueryLookupStrategyUnitTests.java | 8 ++-- 6 files changed, 65 insertions(+), 32 deletions(-) create mode 100644 src/main/java/org/springframework/data/jpa/repository/query/DefaultJpaQueryMethodFactory.java diff --git a/src/main/java/org/springframework/data/jpa/repository/query/DefaultJpaQueryMethodFactory.java b/src/main/java/org/springframework/data/jpa/repository/query/DefaultJpaQueryMethodFactory.java new file mode 100644 index 000000000..379eea7d0 --- /dev/null +++ b/src/main/java/org/springframework/data/jpa/repository/query/DefaultJpaQueryMethodFactory.java @@ -0,0 +1,46 @@ +/* + * Copyright 2019 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.jpa.repository.query; + +import java.lang.reflect.Method; + +import org.springframework.data.jpa.provider.QueryExtractor; +import org.springframework.data.projection.ProjectionFactory; +import org.springframework.data.repository.core.RepositoryMetadata; +import org.springframework.util.Assert; + +/** + * A factory for creating {@link JpaQueryMethod} instances. + * + * @author Jens Schauder + * @since 2.3 + */ +public class DefaultJpaQueryMethodFactory implements JpaQueryMethodFactory { + + private final QueryExtractor extractor; + + public DefaultJpaQueryMethodFactory(QueryExtractor extractor) { + + Assert.notNull(extractor, "QueryExtractor must not be null"); + + this.extractor = extractor; + } + + @Override + public JpaQueryMethod build(Method method, RepositoryMetadata metadata, ProjectionFactory factory) { + return new JpaQueryMethod(method, metadata, factory, extractor); + } +} diff --git a/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryMethod.java b/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryMethod.java index aec4c1df8..d26c7759f 100644 --- a/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryMethod.java +++ b/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryMethod.java @@ -90,20 +90,6 @@ public class JpaQueryMethod extends QueryMethod { private final Lazy isProcedureQuery; private final Lazy> entityMetadata; - /** - * Creates a {@link JpaQueryMethodFactory} which will create instances of this class. - * - * @param extractor must not be {@literal null}. - * @return a {@link JpaQueryMethodFactory} guaranteed to be not {@literal null}. - * @since 2.3 - */ - public static JpaQueryMethodFactory createMethodFactory(QueryExtractor extractor) { - - Assert.notNull(extractor, "QueryExtractor must not be null"); - - return (method, metadata, factory) -> new JpaQueryMethod(method, metadata, factory, extractor); - } - /** * Creates a {@link JpaQueryMethod}. * diff --git a/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryMethodFactory.java b/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryMethodFactory.java index 049e1b1d4..ac3be7646 100644 --- a/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryMethodFactory.java +++ b/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryMethodFactory.java @@ -1,11 +1,11 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * 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, @@ -17,14 +17,12 @@ package org.springframework.data.jpa.repository.query; import java.lang.reflect.Method; -import org.springframework.data.jpa.provider.QueryExtractor; import org.springframework.data.projection.ProjectionFactory; import org.springframework.data.repository.core.RepositoryMetadata; /** - * A factory interface for creating {@link JpaQueryMethodFactory} instances. - * - * This may be implemented by extensions to Spring Data JPA in order create instances of custom subclasses. + * A factory interface for creating {@link JpaQueryMethodFactory} instances. This may be implemented by extensions to + * Spring Data JPA in order create instances of custom subclasses. * * @author Réda Housni Alaoui * @since 2.3 diff --git a/src/main/java/org/springframework/data/jpa/repository/support/JpaRepositoryFactory.java b/src/main/java/org/springframework/data/jpa/repository/support/JpaRepositoryFactory.java index aeaaba19a..12debcf37 100644 --- a/src/main/java/org/springframework/data/jpa/repository/support/JpaRepositoryFactory.java +++ b/src/main/java/org/springframework/data/jpa/repository/support/JpaRepositoryFactory.java @@ -34,6 +34,7 @@ import org.springframework.data.jpa.provider.PersistenceProvider; import org.springframework.data.jpa.provider.QueryExtractor; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.query.AbstractJpaQuery; +import org.springframework.data.jpa.repository.query.DefaultJpaQueryMethodFactory; import org.springframework.data.jpa.repository.query.EscapeCharacter; import org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy; import org.springframework.data.jpa.repository.query.JpaQueryMethod; @@ -91,7 +92,7 @@ public class JpaRepositoryFactory extends RepositoryFactorySupport { this.extractor = PersistenceProvider.fromEntityManager(entityManager); this.crudMethodMetadataPostProcessor = new CrudMethodMetadataPostProcessor(); this.entityPathResolver = SimpleEntityPathResolver.INSTANCE; - this.queryMethodFactory = JpaQueryMethod.createMethodFactory(extractor); + this.queryMethodFactory = new DefaultJpaQueryMethodFactory(extractor); addRepositoryProxyPostProcessor(crudMethodMetadataPostProcessor); addRepositoryProxyPostProcessor((factory, repositoryInformation) -> { diff --git a/src/main/java/org/springframework/data/jpa/repository/support/JpaRepositoryFactoryBean.java b/src/main/java/org/springframework/data/jpa/repository/support/JpaRepositoryFactoryBean.java index 8c71b9768..d0dbc11ca 100644 --- a/src/main/java/org/springframework/data/jpa/repository/support/JpaRepositoryFactoryBean.java +++ b/src/main/java/org/springframework/data/jpa/repository/support/JpaRepositoryFactoryBean.java @@ -20,9 +20,7 @@ import javax.persistence.PersistenceContext; import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.jpa.provider.QueryExtractor; import org.springframework.data.jpa.repository.query.EscapeCharacter; -import org.springframework.data.jpa.repository.query.JpaQueryMethod; import org.springframework.data.jpa.repository.query.JpaQueryMethodFactory; import org.springframework.data.mapping.context.MappingContext; import org.springframework.data.querydsl.EntityPathResolver; @@ -92,14 +90,18 @@ public class JpaRepositoryFactoryBean, S, ID> } /** - * Configures the {@link JpaQueryMethodFactory} to be used. Will expect a canonical bean to be present but will fallback to - * {@link JpaQueryMethod#createMethodFactory(QueryExtractor)} in case none is available. + * Configures the {@link JpaQueryMethodFactory} to be used. Will expect a canonical bean to be present but will + * fallback to {@link org.springframework.data.jpa.repository.query.DefaultJpaQueryMethodFactory} in case none is + * available. * - * @param resolver must not be {@literal null}. + * @param factory may be {@literal null}. */ @Autowired - public void setQueryMethodFactory(ObjectProvider resolver) { - this.queryMethodFactory = resolver.getIfAvailable(() -> null); + public void setQueryMethodFactory(@Nullable JpaQueryMethodFactory factory) { + + if (factory != null) { + this.queryMethodFactory = factory; + } } /* diff --git a/src/test/java/org/springframework/data/jpa/repository/query/JpaQueryLookupStrategyUnitTests.java b/src/test/java/org/springframework/data/jpa/repository/query/JpaQueryLookupStrategyUnitTests.java index 8f6f317ec..49562261a 100644 --- a/src/test/java/org/springframework/data/jpa/repository/query/JpaQueryLookupStrategyUnitTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/query/JpaQueryLookupStrategyUnitTests.java @@ -15,9 +15,9 @@ */ package org.springframework.data.jpa.repository.query; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.when; +import static org.assertj.core.api.Assertions.*; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.*; import java.lang.reflect.Method; import java.util.List; @@ -73,7 +73,7 @@ public class JpaQueryLookupStrategyUnitTests { when(em.getEntityManagerFactory()).thenReturn(emf); when(emf.createEntityManager()).thenReturn(em); when(em.getDelegate()).thenReturn(em); - queryMethodFactory = JpaQueryMethod.createMethodFactory(extractor); + queryMethodFactory = new DefaultJpaQueryMethodFactory(extractor); } @Test // DATAJPA-226