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 <jschauder@pivotal.io>
This commit is contained in:
Jens Schauder
2020-01-14 08:14:00 +01:00
parent 85e4ffaa46
commit 4c6279e4ea
6 changed files with 65 additions and 32 deletions

View File

@@ -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);
}
}

View File

@@ -90,20 +90,6 @@ public class JpaQueryMethod extends QueryMethod {
private final Lazy<Boolean> isProcedureQuery;
private final Lazy<JpaEntityMetadata<?>> 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}.
*

View File

@@ -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

View File

@@ -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) -> {

View File

@@ -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<T extends Repository<S, ID>, 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<JpaQueryMethodFactory> resolver) {
this.queryMethodFactory = resolver.getIfAvailable(() -> null);
public void setQueryMethodFactory(@Nullable JpaQueryMethodFactory factory) {
if (factory != null) {
this.queryMethodFactory = factory;
}
}
/*

View File

@@ -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