Skip to content

Commit 4c6279e

Browse files
committed
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 <[email protected]>
1 parent 85e4ffa commit 4c6279e

File tree

6 files changed

+65
-32
lines changed

6 files changed

+65
-32
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jpa.repository.query;
17+
18+
import java.lang.reflect.Method;
19+
20+
import org.springframework.data.jpa.provider.QueryExtractor;
21+
import org.springframework.data.projection.ProjectionFactory;
22+
import org.springframework.data.repository.core.RepositoryMetadata;
23+
import org.springframework.util.Assert;
24+
25+
/**
26+
* A factory for creating {@link JpaQueryMethod} instances.
27+
*
28+
* @author Jens Schauder
29+
* @since 2.3
30+
*/
31+
public class DefaultJpaQueryMethodFactory implements JpaQueryMethodFactory {
32+
33+
private final QueryExtractor extractor;
34+
35+
public DefaultJpaQueryMethodFactory(QueryExtractor extractor) {
36+
37+
Assert.notNull(extractor, "QueryExtractor must not be null");
38+
39+
this.extractor = extractor;
40+
}
41+
42+
@Override
43+
public JpaQueryMethod build(Method method, RepositoryMetadata metadata, ProjectionFactory factory) {
44+
return new JpaQueryMethod(method, metadata, factory, extractor);
45+
}
46+
}

src/main/java/org/springframework/data/jpa/repository/query/JpaQueryMethod.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,6 @@ public class JpaQueryMethod extends QueryMethod {
9090
private final Lazy<Boolean> isProcedureQuery;
9191
private final Lazy<JpaEntityMetadata<?>> entityMetadata;
9292

93-
/**
94-
* Creates a {@link JpaQueryMethodFactory} which will create instances of this class.
95-
*
96-
* @param extractor must not be {@literal null}.
97-
* @return a {@link JpaQueryMethodFactory} guaranteed to be not {@literal null}.
98-
* @since 2.3
99-
*/
100-
public static JpaQueryMethodFactory createMethodFactory(QueryExtractor extractor) {
101-
102-
Assert.notNull(extractor, "QueryExtractor must not be null");
103-
104-
return (method, metadata, factory) -> new JpaQueryMethod(method, metadata, factory, extractor);
105-
}
106-
10793
/**
10894
* Creates a {@link JpaQueryMethod}.
10995
*

src/main/java/org/springframework/data/jpa/repository/query/JpaQueryMethodFactory.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* https://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -17,14 +17,12 @@
1717

1818
import java.lang.reflect.Method;
1919

20-
import org.springframework.data.jpa.provider.QueryExtractor;
2120
import org.springframework.data.projection.ProjectionFactory;
2221
import org.springframework.data.repository.core.RepositoryMetadata;
2322

2423
/**
25-
* A factory interface for creating {@link JpaQueryMethodFactory} instances.
26-
*
27-
* This may be implemented by extensions to Spring Data JPA in order create instances of custom subclasses.
24+
* A factory interface for creating {@link JpaQueryMethodFactory} instances. This may be implemented by extensions to
25+
* Spring Data JPA in order create instances of custom subclasses.
2826
*
2927
* @author Réda Housni Alaoui
3028
* @since 2.3

src/main/java/org/springframework/data/jpa/repository/support/JpaRepositoryFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.springframework.data.jpa.provider.QueryExtractor;
3535
import org.springframework.data.jpa.repository.JpaRepository;
3636
import org.springframework.data.jpa.repository.query.AbstractJpaQuery;
37+
import org.springframework.data.jpa.repository.query.DefaultJpaQueryMethodFactory;
3738
import org.springframework.data.jpa.repository.query.EscapeCharacter;
3839
import org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy;
3940
import org.springframework.data.jpa.repository.query.JpaQueryMethod;
@@ -91,7 +92,7 @@ public JpaRepositoryFactory(EntityManager entityManager) {
9192
this.extractor = PersistenceProvider.fromEntityManager(entityManager);
9293
this.crudMethodMetadataPostProcessor = new CrudMethodMetadataPostProcessor();
9394
this.entityPathResolver = SimpleEntityPathResolver.INSTANCE;
94-
this.queryMethodFactory = JpaQueryMethod.createMethodFactory(extractor);
95+
this.queryMethodFactory = new DefaultJpaQueryMethodFactory(extractor);
9596

9697
addRepositoryProxyPostProcessor(crudMethodMetadataPostProcessor);
9798
addRepositoryProxyPostProcessor((factory, repositoryInformation) -> {

src/main/java/org/springframework/data/jpa/repository/support/JpaRepositoryFactoryBean.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020

2121
import org.springframework.beans.factory.ObjectProvider;
2222
import org.springframework.beans.factory.annotation.Autowired;
23-
import org.springframework.data.jpa.provider.QueryExtractor;
2423
import org.springframework.data.jpa.repository.query.EscapeCharacter;
25-
import org.springframework.data.jpa.repository.query.JpaQueryMethod;
2624
import org.springframework.data.jpa.repository.query.JpaQueryMethodFactory;
2725
import org.springframework.data.mapping.context.MappingContext;
2826
import org.springframework.data.querydsl.EntityPathResolver;
@@ -92,14 +90,18 @@ public void setEntityPathResolver(ObjectProvider<EntityPathResolver> resolver) {
9290
}
9391

9492
/**
95-
* Configures the {@link JpaQueryMethodFactory} to be used. Will expect a canonical bean to be present but will fallback to
96-
* {@link JpaQueryMethod#createMethodFactory(QueryExtractor)} in case none is available.
93+
* Configures the {@link JpaQueryMethodFactory} to be used. Will expect a canonical bean to be present but will
94+
* fallback to {@link org.springframework.data.jpa.repository.query.DefaultJpaQueryMethodFactory} in case none is
95+
* available.
9796
*
98-
* @param resolver must not be {@literal null}.
97+
* @param factory may be {@literal null}.
9998
*/
10099
@Autowired
101-
public void setQueryMethodFactory(ObjectProvider<JpaQueryMethodFactory> resolver) {
102-
this.queryMethodFactory = resolver.getIfAvailable(() -> null);
100+
public void setQueryMethodFactory(@Nullable JpaQueryMethodFactory factory) {
101+
102+
if (factory != null) {
103+
this.queryMethodFactory = factory;
104+
}
103105
}
104106

105107
/*

src/test/java/org/springframework/data/jpa/repository/query/JpaQueryLookupStrategyUnitTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
*/
1616
package org.springframework.data.jpa.repository.query;
1717

18-
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
19-
import static org.mockito.ArgumentMatchers.anyString;
20-
import static org.mockito.Mockito.when;
18+
import static org.assertj.core.api.Assertions.*;
19+
import static org.mockito.ArgumentMatchers.*;
20+
import static org.mockito.Mockito.*;
2121

2222
import java.lang.reflect.Method;
2323
import java.util.List;
@@ -73,7 +73,7 @@ public void setUp() {
7373
when(em.getEntityManagerFactory()).thenReturn(emf);
7474
when(emf.createEntityManager()).thenReturn(em);
7575
when(em.getDelegate()).thenReturn(em);
76-
queryMethodFactory = JpaQueryMethod.createMethodFactory(extractor);
76+
queryMethodFactory = new DefaultJpaQueryMethodFactory(extractor);
7777
}
7878

7979
@Test // DATAJPA-226

0 commit comments

Comments
 (0)