Skip to content

Commit 712477b

Browse files
mp911dechristophstrobl
authored andcommitted
Introduce PropertiesBasedNamedQueriesFactoryBean to reduce bean indirections.
We now provide PropertiesBasedNamedQueriesFactoryBean to create PropertiesBasedNamedQueries directly bypassing indirections through PropertiesFactoryBean and PropertiesBasedNamedQueries. Resolves: #2584 Original Pull Request: #2596
1 parent 0f85f1c commit 712477b

File tree

3 files changed

+97
-24
lines changed

3 files changed

+97
-24
lines changed

src/main/java/org/springframework/data/repository/config/NamedQueriesBeanDefinitionBuilder.java

+6-13
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,18 @@
1616
package org.springframework.data.repository.config;
1717

1818
import org.springframework.beans.factory.config.BeanDefinition;
19-
import org.springframework.beans.factory.config.PropertiesFactoryBean;
2019
import org.springframework.beans.factory.support.AbstractBeanDefinition;
2120
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
2221
import org.springframework.data.repository.core.NamedQueries;
23-
import org.springframework.data.repository.core.support.PropertiesBasedNamedQueries;
2422
import org.springframework.lang.Nullable;
2523
import org.springframework.util.Assert;
2624
import org.springframework.util.StringUtils;
2725

2826
/**
29-
* Builder to create a {@link BeanDefinition} for a {@link NamedQueries} instance.
27+
* Builder to create a {@link BeanDefinition} for a {@link NamedQueries} instance using properties.
3028
*
3129
* @author Oliver Gierke
30+
* @author Mark Paluch
3231
*/
3332
public class NamedQueriesBeanDefinitionBuilder {
3433

@@ -67,21 +66,15 @@ public void setLocations(String locations) {
6766
*/
6867
public BeanDefinition build(@Nullable Object source) {
6968

70-
BeanDefinitionBuilder properties = BeanDefinitionBuilder.rootBeanDefinition(PropertiesFactoryBean.class);
71-
69+
BeanDefinitionBuilder namedQueries = BeanDefinitionBuilder
70+
.rootBeanDefinition(PropertiesBasedNamedQueriesFactoryBean.class);
7271
String locationsToUse = StringUtils.hasText(locations) ? locations : defaultLocation;
73-
properties.addPropertyValue("locations", locationsToUse);
72+
namedQueries.addPropertyValue("locations", locationsToUse);
7473

7574
if (!StringUtils.hasText(locations)) {
76-
properties.addPropertyValue("ignoreResourceNotFound", true);
75+
namedQueries.addPropertyValue("ignoreResourceNotFound", true);
7776
}
7877

79-
AbstractBeanDefinition propertiesDefinition = properties.getBeanDefinition();
80-
propertiesDefinition.setSource(source);
81-
82-
BeanDefinitionBuilder namedQueries = BeanDefinitionBuilder.rootBeanDefinition(PropertiesBasedNamedQueries.class);
83-
namedQueries.addConstructorArgValue(propertiesDefinition);
84-
8578
AbstractBeanDefinition namedQueriesDefinition = namedQueries.getBeanDefinition();
8679
namedQueriesDefinition.setSource(source);
8780

src/main/java/org/springframework/data/repository/config/NamedQueriesBeanDefinitionParser.java

+5-11
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@
1818
import java.util.Properties;
1919

2020
import org.springframework.beans.factory.config.BeanDefinition;
21-
import org.springframework.beans.factory.config.PropertiesFactoryBean;
2221
import org.springframework.beans.factory.support.AbstractBeanDefinition;
2322
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
2423
import org.springframework.beans.factory.xml.BeanDefinitionParser;
2524
import org.springframework.beans.factory.xml.ParserContext;
2625
import org.springframework.data.repository.core.NamedQueries;
27-
import org.springframework.data.repository.core.support.PropertiesBasedNamedQueries;
2826
import org.springframework.lang.NonNull;
2927
import org.springframework.util.Assert;
3028
import org.springframework.util.StringUtils;
@@ -36,6 +34,7 @@
3634
* {@link Properties} file fom the given location.
3735
*
3836
* @author Oliver Gierke
37+
* @author Mark Paluch
3938
*/
4039
public class NamedQueriesBeanDefinitionParser implements BeanDefinitionParser {
4140

@@ -55,19 +54,14 @@ public NamedQueriesBeanDefinitionParser(String defaultLocation) {
5554
@NonNull
5655
public BeanDefinition parse(Element element, ParserContext parserContext) {
5756

58-
BeanDefinitionBuilder properties = BeanDefinitionBuilder.rootBeanDefinition(PropertiesFactoryBean.class);
59-
properties.addPropertyValue("locations", getDefaultedLocation(element));
57+
BeanDefinitionBuilder namedQueries = BeanDefinitionBuilder
58+
.rootBeanDefinition(PropertiesBasedNamedQueriesFactoryBean.class);
59+
namedQueries.addPropertyValue("locations", getDefaultedLocation(element));
6060

6161
if (isDefaultLocation(element)) {
62-
properties.addPropertyValue("ignoreResourceNotFound", true);
62+
namedQueries.addPropertyValue("ignoreResourceNotFound", true);
6363
}
6464

65-
AbstractBeanDefinition propertiesDefinition = properties.getBeanDefinition();
66-
propertiesDefinition.setSource(parserContext.extractSource(element));
67-
68-
BeanDefinitionBuilder namedQueries = BeanDefinitionBuilder.rootBeanDefinition(PropertiesBasedNamedQueries.class);
69-
namedQueries.addConstructorArgValue(propertiesDefinition);
70-
7165
AbstractBeanDefinition namedQueriesDefinition = namedQueries.getBeanDefinition();
7266
namedQueriesDefinition.setSource(parserContext.extractSource(element));
7367

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2022 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.repository.config;
17+
18+
import java.io.IOException;
19+
import java.util.Properties;
20+
21+
import org.springframework.beans.factory.FactoryBean;
22+
import org.springframework.beans.factory.InitializingBean;
23+
import org.springframework.core.io.support.PropertiesLoaderSupport;
24+
import org.springframework.data.repository.core.support.PropertiesBasedNamedQueries;
25+
import org.springframework.lang.Nullable;
26+
27+
/**
28+
* Factory bean to create {@link PropertiesBasedNamedQueries}.
29+
* <p>
30+
* Supports loading from a properties file and/or setting local properties on this FactoryBean. The created Properties
31+
* instance will be merged from loaded and local values. If neither a location nor local properties are set, an
32+
* exception will be thrown on initialization.
33+
* <p>
34+
* Can create a singleton or a new object on each request. Default is a singleton.
35+
*
36+
* @author Mark Paluch
37+
* @since 3.0
38+
*/
39+
public class PropertiesBasedNamedQueriesFactoryBean extends PropertiesLoaderSupport
40+
implements FactoryBean<PropertiesBasedNamedQueries>, InitializingBean {
41+
42+
private boolean singleton = true;
43+
44+
private @Nullable PropertiesBasedNamedQueries singletonInstance;
45+
46+
/**
47+
* Set whether a shared singleton {@code PropertiesBasedNamedQueries} instance should be created, or rather a new
48+
* {@code PropertiesBasedNamedQueries} instance on each request.
49+
* <p>
50+
* Default is {@code true} (a shared singleton).
51+
*/
52+
public void setSingleton(boolean singleton) {
53+
this.singleton = singleton;
54+
}
55+
56+
@Override
57+
public boolean isSingleton() {
58+
return this.singleton;
59+
}
60+
61+
@Override
62+
public void afterPropertiesSet() throws IOException {
63+
if (this.singleton) {
64+
this.singletonInstance = new PropertiesBasedNamedQueries(createProperties());
65+
}
66+
}
67+
68+
@Override
69+
@Nullable
70+
public PropertiesBasedNamedQueries getObject() throws IOException {
71+
if (this.singleton) {
72+
return this.singletonInstance;
73+
} else {
74+
return new PropertiesBasedNamedQueries(createProperties());
75+
}
76+
}
77+
78+
@Override
79+
public Class<PropertiesBasedNamedQueries> getObjectType() {
80+
return PropertiesBasedNamedQueries.class;
81+
}
82+
83+
protected Properties createProperties() throws IOException {
84+
return mergeProperties();
85+
}
86+
}

0 commit comments

Comments
 (0)