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