1
1
/*
2
- * Copyright 2002-2017 the original author or authors.
2
+ * Copyright 2002-2019 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
26
26
* @author Rod Johnson
27
27
* @author Juergen Hoeller
28
28
* @see BeanDefinition#getPropertyValues()
29
- * @see org.springframework.beans.factory.BeanFactory#getBean
29
+ * @see org.springframework.beans.factory.BeanFactory#getBean(String)
30
+ * @see org.springframework.beans.factory.BeanFactory#getBean(Class)
30
31
*/
31
32
public class RuntimeBeanReference implements BeanReference {
32
33
33
34
private final String beanName ;
34
35
36
+ @ Nullable
37
+ private final Class <?> beanType ;
38
+
35
39
private final boolean toParent ;
36
40
37
41
@ Nullable
38
42
private Object source ;
39
43
40
44
41
45
/**
42
- * Create a new RuntimeBeanReference to the given bean name,
43
- * without explicitly marking it as reference to a bean in
44
- * the parent factory.
46
+ * Create a new RuntimeBeanReference to the given bean name.
45
47
* @param beanName name of the target bean
46
48
*/
47
49
public RuntimeBeanReference (String beanName ) {
@@ -50,27 +52,64 @@ public RuntimeBeanReference(String beanName) {
50
52
51
53
/**
52
54
* Create a new RuntimeBeanReference to the given bean name,
53
- * with the option to mark it as reference to a bean in
54
- * the parent factory.
55
+ * with the option to mark it as reference to a bean in the parent factory.
55
56
* @param beanName name of the target bean
56
- * @param toParent whether this is an explicit reference to
57
- * a bean in the parent factory
57
+ * @param toParent whether this is an explicit reference to a bean in the
58
+ * parent factory
58
59
*/
59
60
public RuntimeBeanReference (String beanName , boolean toParent ) {
60
61
Assert .hasText (beanName , "'beanName' must not be empty" );
61
62
this .beanName = beanName ;
63
+ this .beanType = null ;
62
64
this .toParent = toParent ;
63
65
}
64
66
67
+ /**
68
+ * Create a new RuntimeBeanReference to a bean of the given type.
69
+ * @param beanType type of the target bean
70
+ * @since 5.2
71
+ */
72
+ public RuntimeBeanReference (Class <?> beanType ) {
73
+ this (beanType , false );
74
+ }
65
75
76
+ /**
77
+ * Create a new RuntimeBeanReference to a bean of the given type,
78
+ * with the option to mark it as reference to a bean in the parent factory.
79
+ * @param beanType type of the target bean
80
+ * @param toParent whether this is an explicit reference to a bean in the
81
+ * parent factory
82
+ * @since 5.2
83
+ */
84
+ public RuntimeBeanReference (Class <?> beanType , boolean toParent ) {
85
+ Assert .notNull (beanType , "'beanType' must not be empty" );
86
+ this .beanName = beanType .getName ();
87
+ this .beanType = beanType ;
88
+ this .toParent = toParent ;
89
+ }
90
+
91
+
92
+ /**
93
+ * Return the requested bean name, or the fully-qualified type name
94
+ * in case of by-type resolution.
95
+ * @see #getBeanType()
96
+ */
66
97
@ Override
67
98
public String getBeanName () {
68
99
return this .beanName ;
69
100
}
70
101
71
102
/**
72
- * Return whether this is an explicit reference to a bean
73
- * in the parent factory.
103
+ * Return the requested bean type if resolution by type is demanded.
104
+ * @since 5.2
105
+ */
106
+ @ Nullable
107
+ public Class <?> getBeanType () {
108
+ return this .beanType ;
109
+ }
110
+
111
+ /**
112
+ * Return whether this is an explicit reference to a bean in the parent factory.
74
113
*/
75
114
public boolean isToParent () {
76
115
return this .toParent ;
@@ -100,7 +139,8 @@ public boolean equals(@Nullable Object other) {
100
139
return false ;
101
140
}
102
141
RuntimeBeanReference that = (RuntimeBeanReference ) other ;
103
- return (this .beanName .equals (that .beanName ) && this .toParent == that .toParent );
142
+ return (this .beanName .equals (that .beanName ) && this .beanType == that .beanType &&
143
+ this .toParent == that .toParent );
104
144
}
105
145
106
146
@ Override
0 commit comments