18
18
import java .util .Collection ;
19
19
import java .util .Optional ;
20
20
21
+ import org .springframework .beans .factory .config .BeanDefinition ;
21
22
import org .springframework .beans .factory .xml .ParserContext ;
22
23
import org .springframework .core .env .Environment ;
23
24
import org .springframework .core .type .filter .TypeFilter ;
29
30
import org .springframework .data .util .Streamable ;
30
31
import org .springframework .lang .Nullable ;
31
32
import org .springframework .util .Assert ;
33
+ import org .springframework .util .ClassUtils ;
32
34
import org .springframework .util .StringUtils ;
33
35
import org .w3c .dom .Element ;
34
36
40
42
* @author Christoph Strobl
41
43
* @author Peter Rietzler
42
44
* @author Jens Schauder
45
+ * @author Sascha Woo
43
46
*/
44
47
public class XmlRepositoryConfigurationSource extends RepositoryConfigurationSourceSupport {
45
48
@@ -50,9 +53,11 @@ public class XmlRepositoryConfigurationSource extends RepositoryConfigurationSou
50
53
private static final String REPOSITORY_FACTORY_BEAN_CLASS_NAME = "factory-class" ;
51
54
private static final String REPOSITORY_BASE_CLASS_NAME = "base-class" ;
52
55
private static final String CONSIDER_NESTED_REPOSITORIES = "consider-nested-repositories" ;
56
+ private static final String BEAN_NAME_GENERATOR = "name-generator" ;
53
57
54
58
private final Element element ;
55
59
private final ParserContext context ;
60
+ private final RepositoryBeanNameGenerator beanNameGenerator ;
56
61
57
62
private final Collection <TypeFilter > includeFilters ;
58
63
private final Collection <TypeFilter > excludeFilters ;
@@ -66,7 +71,7 @@ public class XmlRepositoryConfigurationSource extends RepositoryConfigurationSou
66
71
*/
67
72
public XmlRepositoryConfigurationSource (Element element , ParserContext context , Environment environment ) {
68
73
69
- super (environment , ConfigurationUtils . getRequiredClassLoader ( context . getReaderContext ()), context .getRegistry ());
74
+ super (environment , context .getRegistry ());
70
75
71
76
Assert .notNull (element , "Element must not be null!" );
72
77
@@ -76,6 +81,12 @@ public XmlRepositoryConfigurationSource(Element element, ParserContext context,
76
81
TypeFilterParser parser = new TypeFilterParser (context .getReaderContext ());
77
82
this .includeFilters = parser .parseTypeFilters (element , Type .INCLUDE );
78
83
this .excludeFilters = parser .parseTypeFilters (element , Type .EXCLUDE );
84
+ this .beanNameGenerator = getBeanNameGenerator ();
85
+ }
86
+
87
+ @ Override
88
+ public String generateBeanName (BeanDefinition beanDefinition ) {
89
+ return beanNameGenerator .generateBeanName (beanDefinition );
79
90
}
80
91
81
92
/*
@@ -211,4 +222,22 @@ public Optional<String> getAttribute(String name) {
211
222
public boolean usesExplicitFilters () {
212
223
return !(this .includeFilters .isEmpty () && this .excludeFilters .isEmpty ());
213
224
}
225
+
226
+ private RepositoryBeanNameGenerator getBeanNameGenerator () {
227
+ ClassLoader classLoader = ConfigurationUtils .getRequiredClassLoader (context .getReaderContext ());
228
+
229
+ Optional <String > attributeNameGenerator = getNullDefaultedAttribute (element , BEAN_NAME_GENERATOR );
230
+ if (!attributeNameGenerator .isPresent ())
231
+ return new RepositoryBeanNameGenerator (classLoader );
232
+
233
+ try {
234
+ @ SuppressWarnings ("unchecked" )
235
+ Class <? extends RepositoryBeanNameGenerator > beanNameGenerator = (Class <? extends RepositoryBeanNameGenerator >) //
236
+ ClassUtils .forName (attributeNameGenerator .get (), classLoader );
237
+
238
+ return getBeanNameGenerator (beanNameGenerator , classLoader );
239
+ } catch (ClassNotFoundException | LinkageError e ) {
240
+ throw new IllegalArgumentException ("Unsupported bean name generator specified " + attributeNameGenerator );
241
+ }
242
+ }
214
243
}
0 commit comments