diff --git a/src/main/java/org/springframework/data/repository/config/NamedQueriesBeanDefinitionBuilder.java b/src/main/java/org/springframework/data/repository/config/NamedQueriesBeanDefinitionBuilder.java index cafeeb419..975ff1d98 100644 --- a/src/main/java/org/springframework/data/repository/config/NamedQueriesBeanDefinitionBuilder.java +++ b/src/main/java/org/springframework/data/repository/config/NamedQueriesBeanDefinitionBuilder.java @@ -16,19 +16,18 @@ package org.springframework.data.repository.config; import org.springframework.beans.factory.config.BeanDefinition; -import org.springframework.beans.factory.config.PropertiesFactoryBean; import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.data.repository.core.NamedQueries; -import org.springframework.data.repository.core.support.PropertiesBasedNamedQueries; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.StringUtils; /** - * Builder to create a {@link BeanDefinition} for a {@link NamedQueries} instance. + * Builder to create a {@link BeanDefinition} for a {@link NamedQueries} instance using properties. * * @author Oliver Gierke + * @author Mark Paluch */ public class NamedQueriesBeanDefinitionBuilder { @@ -67,21 +66,15 @@ public class NamedQueriesBeanDefinitionBuilder { */ public BeanDefinition build(@Nullable Object source) { - BeanDefinitionBuilder properties = BeanDefinitionBuilder.rootBeanDefinition(PropertiesFactoryBean.class); - + BeanDefinitionBuilder namedQueries = BeanDefinitionBuilder + .rootBeanDefinition(PropertiesBasedNamedQueriesFactoryBean.class); String locationsToUse = StringUtils.hasText(locations) ? locations : defaultLocation; - properties.addPropertyValue("locations", locationsToUse); + namedQueries.addPropertyValue("locations", locationsToUse); if (!StringUtils.hasText(locations)) { - properties.addPropertyValue("ignoreResourceNotFound", true); + namedQueries.addPropertyValue("ignoreResourceNotFound", true); } - AbstractBeanDefinition propertiesDefinition = properties.getBeanDefinition(); - propertiesDefinition.setSource(source); - - BeanDefinitionBuilder namedQueries = BeanDefinitionBuilder.rootBeanDefinition(PropertiesBasedNamedQueries.class); - namedQueries.addConstructorArgValue(propertiesDefinition); - AbstractBeanDefinition namedQueriesDefinition = namedQueries.getBeanDefinition(); namedQueriesDefinition.setSource(source); diff --git a/src/main/java/org/springframework/data/repository/config/NamedQueriesBeanDefinitionParser.java b/src/main/java/org/springframework/data/repository/config/NamedQueriesBeanDefinitionParser.java index 72b8023e2..c72208c9b 100644 --- a/src/main/java/org/springframework/data/repository/config/NamedQueriesBeanDefinitionParser.java +++ b/src/main/java/org/springframework/data/repository/config/NamedQueriesBeanDefinitionParser.java @@ -18,13 +18,11 @@ package org.springframework.data.repository.config; import java.util.Properties; import org.springframework.beans.factory.config.BeanDefinition; -import org.springframework.beans.factory.config.PropertiesFactoryBean; import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.BeanDefinitionParser; import org.springframework.beans.factory.xml.ParserContext; import org.springframework.data.repository.core.NamedQueries; -import org.springframework.data.repository.core.support.PropertiesBasedNamedQueries; import org.springframework.lang.NonNull; import org.springframework.util.Assert; import org.springframework.util.StringUtils; @@ -36,6 +34,7 @@ import org.w3c.dom.Element; * {@link Properties} file fom the given location. * * @author Oliver Gierke + * @author Mark Paluch */ public class NamedQueriesBeanDefinitionParser implements BeanDefinitionParser { @@ -55,19 +54,14 @@ public class NamedQueriesBeanDefinitionParser implements BeanDefinitionParser { @NonNull public BeanDefinition parse(Element element, ParserContext parserContext) { - BeanDefinitionBuilder properties = BeanDefinitionBuilder.rootBeanDefinition(PropertiesFactoryBean.class); - properties.addPropertyValue("locations", getDefaultedLocation(element)); + BeanDefinitionBuilder namedQueries = BeanDefinitionBuilder + .rootBeanDefinition(PropertiesBasedNamedQueriesFactoryBean.class); + namedQueries.addPropertyValue("locations", getDefaultedLocation(element)); if (isDefaultLocation(element)) { - properties.addPropertyValue("ignoreResourceNotFound", true); + namedQueries.addPropertyValue("ignoreResourceNotFound", true); } - AbstractBeanDefinition propertiesDefinition = properties.getBeanDefinition(); - propertiesDefinition.setSource(parserContext.extractSource(element)); - - BeanDefinitionBuilder namedQueries = BeanDefinitionBuilder.rootBeanDefinition(PropertiesBasedNamedQueries.class); - namedQueries.addConstructorArgValue(propertiesDefinition); - AbstractBeanDefinition namedQueriesDefinition = namedQueries.getBeanDefinition(); namedQueriesDefinition.setSource(parserContext.extractSource(element)); diff --git a/src/main/java/org/springframework/data/repository/config/PropertiesBasedNamedQueriesFactoryBean.java b/src/main/java/org/springframework/data/repository/config/PropertiesBasedNamedQueriesFactoryBean.java new file mode 100644 index 000000000..a0c93c925 --- /dev/null +++ b/src/main/java/org/springframework/data/repository/config/PropertiesBasedNamedQueriesFactoryBean.java @@ -0,0 +1,86 @@ +/* + * Copyright 2022 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.repository.config; + +import java.io.IOException; +import java.util.Properties; + +import org.springframework.beans.factory.FactoryBean; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.core.io.support.PropertiesLoaderSupport; +import org.springframework.data.repository.core.support.PropertiesBasedNamedQueries; +import org.springframework.lang.Nullable; + +/** + * Factory bean to create {@link PropertiesBasedNamedQueries}. + *
+ * Supports loading from a properties file and/or setting local properties on this FactoryBean. The created Properties + * instance will be merged from loaded and local values. If neither a location nor local properties are set, an + * exception will be thrown on initialization. + *
+ * Can create a singleton or a new object on each request. Default is a singleton.
+ *
+ * @author Mark Paluch
+ * @since 3.0
+ */
+public class PropertiesBasedNamedQueriesFactoryBean extends PropertiesLoaderSupport
+ implements FactoryBean
+ * Default is {@code true} (a shared singleton).
+ */
+ public void setSingleton(boolean singleton) {
+ this.singleton = singleton;
+ }
+
+ @Override
+ public boolean isSingleton() {
+ return this.singleton;
+ }
+
+ @Override
+ public void afterPropertiesSet() throws IOException {
+ if (this.singleton) {
+ this.singletonInstance = new PropertiesBasedNamedQueries(createProperties());
+ }
+ }
+
+ @Override
+ @Nullable
+ public PropertiesBasedNamedQueries getObject() throws IOException {
+ if (this.singleton) {
+ return this.singletonInstance;
+ } else {
+ return new PropertiesBasedNamedQueries(createProperties());
+ }
+ }
+
+ @Override
+ public Class