|
15 | 15 | */
|
16 | 16 | package org.springframework.data.jdbc.repository.config;
|
17 | 17 |
|
18 |
| -import java.util.ArrayList; |
19 |
| -import java.util.Arrays; |
20 |
| -import java.util.List; |
21 | 18 | import java.util.Locale;
|
22 |
| -import java.util.Optional; |
23 |
| -import java.util.function.Supplier; |
24 | 19 |
|
25 | 20 | import org.springframework.beans.factory.ListableBeanFactory;
|
26 |
| -import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
27 |
| -import org.springframework.beans.factory.NoUniqueBeanDefinitionException; |
28 |
| -import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
29 | 21 | import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
30 | 22 | import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
31 |
| -import org.springframework.data.jdbc.core.DataAccessStrategy; |
32 | 23 | import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactoryBean;
|
33 | 24 | import org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport;
|
34 | 25 | import org.springframework.data.repository.config.RepositoryConfigurationSource;
|
35 |
| -import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; |
36 |
| -import org.springframework.lang.Nullable; |
37 |
| -import org.springframework.util.Assert; |
38 |
| -import org.springframework.util.ObjectUtils; |
39 | 26 | import org.springframework.util.StringUtils;
|
40 | 27 |
|
41 | 28 | /**
|
@@ -87,103 +74,20 @@ public void registerBeansForRoot(BeanDefinitionRegistry registry, RepositoryConf
|
87 | 74 | }
|
88 | 75 | }
|
89 | 76 |
|
90 |
| - /* |
| 77 | + /* |
91 | 78 | * (non-Javadoc)
|
92 | 79 | * @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#postProcess(org.springframework.beans.factory.support.BeanDefinitionBuilder, org.springframework.data.repository.config.RepositoryConfigurationSource)
|
93 | 80 | */
|
94 | 81 | @Override
|
95 | 82 | public void postProcess(BeanDefinitionBuilder builder, RepositoryConfigurationSource source) {
|
96 | 83 |
|
97 |
| - resolveReference(builder, source, "jdbcOperationsRef", "jdbcOperations", NamedParameterJdbcOperations.class, true); |
98 |
| - resolveReference(builder, source, "dataAccessStrategyRef", "dataAccessStrategy", DataAccessStrategy.class, false); |
99 |
| - } |
100 |
| - |
101 |
| - private void resolveReference(BeanDefinitionBuilder builder, RepositoryConfigurationSource source, |
102 |
| - String attributeName, String propertyName, Class<?> classRef, boolean required) { |
103 |
| - |
104 |
| - Optional<String> beanNameRef = source.getAttribute(attributeName).filter(StringUtils::hasText); |
105 |
| - |
106 |
| - String beanName = beanNameRef.orElseGet(() -> determineMatchingBeanName(propertyName, classRef, required)); |
107 |
| - |
108 |
| - if (beanName != null) { |
109 |
| - builder.addPropertyReference(propertyName, beanName); |
110 |
| - } else { |
111 |
| - Assert.isTrue(!required, |
112 |
| - "The beanName must not be null when requested as 'required'. Please report this as a bug."); |
113 |
| - } |
114 |
| - |
115 |
| - } |
116 |
| - |
117 |
| - @Nullable |
118 |
| - private String determineMatchingBeanName(String propertyName, Class<?> classRef, boolean required) { |
| 84 | + source.getAttribute("jdbcOperationsRef") // |
| 85 | + .filter(s -> !StringUtils.isEmpty(s)) // |
| 86 | + .ifPresent(s -> builder.addPropertyReference("jdbcOperations", s)); |
119 | 87 |
|
120 |
| - if (this.beanFactory == null) { |
121 |
| - return nullOrThrowException(required, |
122 |
| - () -> new NoSuchBeanDefinitionException(classRef, "No BeanFactory available.")); |
123 |
| - } |
124 |
| - |
125 |
| - List<String> beanNames = Arrays.asList(beanFactory.getBeanNamesForType(classRef)); |
126 |
| - |
127 |
| - if (beanNames.isEmpty()) { |
128 |
| - return nullOrThrowException(required, |
129 |
| - () -> new NoSuchBeanDefinitionException(classRef, String.format("No bean of type %s available", classRef))); |
130 |
| - } |
131 |
| - |
132 |
| - if (beanNames.size() == 1) { |
133 |
| - return beanNames.get(0); |
134 |
| - } |
135 |
| - |
136 |
| - if (!(beanFactory instanceof ConfigurableListableBeanFactory)) { |
137 |
| - |
138 |
| - return nullOrThrowException(required, |
139 |
| - () -> new NoSuchBeanDefinitionException(String.format( |
140 |
| - "BeanFactory does not implement ConfigurableListableBeanFactory when trying to find bean of type %s.", |
141 |
| - classRef))); |
142 |
| - } |
143 |
| - |
144 |
| - List<String> primaryBeanNames = getPrimaryBeanDefinitions(beanNames, (ConfigurableListableBeanFactory) beanFactory); |
145 |
| - |
146 |
| - if (primaryBeanNames.size() == 1) { |
147 |
| - return primaryBeanNames.get(0); |
148 |
| - } |
149 |
| - |
150 |
| - if (primaryBeanNames.size() > 1) { |
151 |
| - throw new NoUniqueBeanDefinitionException(classRef, primaryBeanNames.size(), |
152 |
| - "more than one 'primary' bean found among candidates: " + primaryBeanNames); |
153 |
| - } |
154 |
| - |
155 |
| - for (String beanName : beanNames) { |
156 |
| - |
157 |
| - if (propertyName.equals(beanName) |
158 |
| - || ObjectUtils.containsElement(beanFactory.getAliases(beanName), propertyName)) { |
159 |
| - return beanName; |
160 |
| - } |
161 |
| - } |
162 |
| - |
163 |
| - return nullOrThrowException(required, |
164 |
| - () -> new NoSuchBeanDefinitionException(String.format("No bean of name %s found.", propertyName))); |
165 |
| - } |
166 |
| - |
167 |
| - private static List<String> getPrimaryBeanDefinitions(List<String> beanNames, |
168 |
| - ConfigurableListableBeanFactory beanFactory) { |
169 |
| - |
170 |
| - ArrayList<String> primaryBeanNames = new ArrayList<>(); |
171 |
| - for (String name : beanNames) { |
172 |
| - |
173 |
| - if (beanFactory.getBeanDefinition(name).isPrimary()) { |
174 |
| - primaryBeanNames.add(name); |
175 |
| - } |
176 |
| - } |
177 |
| - return primaryBeanNames; |
178 |
| - } |
179 |
| - |
180 |
| - @Nullable |
181 |
| - private static String nullOrThrowException(boolean required, Supplier<RuntimeException> exception) { |
182 |
| - |
183 |
| - if (required) { |
184 |
| - throw exception.get(); |
185 |
| - } |
186 |
| - return null; |
| 88 | + source.getAttribute("dataAccessStrategyRef") // |
| 89 | + .filter(s -> !StringUtils.isEmpty(s)) // |
| 90 | + .ifPresent(s -> builder.addPropertyReference("dataAccessStrategy", s)); |
187 | 91 | }
|
188 | 92 |
|
189 | 93 | }
|
0 commit comments