|
1 | 1 | package io.dinject.core;
|
2 | 2 |
|
3 | 3 | import io.dinject.BeanContext;
|
| 4 | +import io.dinject.BeanEntry; |
4 | 5 | import org.slf4j.Logger;
|
5 | 6 | import org.slf4j.LoggerFactory;
|
6 | 7 |
|
@@ -55,20 +56,25 @@ public <T> T getBean(Class<T> beanClass) {
|
55 | 56 | return getBean(beanClass, null);
|
56 | 57 | }
|
57 | 58 |
|
58 |
| - @SuppressWarnings("unchecked") |
59 | 59 | @Override
|
60 |
| - public <T> T getBean(Class<T> beanClass, String name) { |
61 |
| - T bean = beans.getBean(beanClass, name); |
62 |
| - if (bean != null) { |
63 |
| - return bean; |
64 |
| - } |
| 60 | + public <T> BeanEntry<T> candidate(Class<T> type, String name) { |
| 61 | + |
| 62 | + // sort candiates by priority - Primary, Normal, Secondary |
| 63 | + EntrySort<T> entrySort = new EntrySort<>(); |
| 64 | + |
| 65 | + entrySort.add(beans.candidate(type, name)); |
65 | 66 | for (BeanContext childContext : children.values()) {
|
66 |
| - bean = childContext.getBean(beanClass, name); |
67 |
| - if (bean != null) { |
68 |
| - return bean; |
69 |
| - } |
| 67 | + entrySort.add(childContext.candidate(type, name)); |
70 | 68 | }
|
71 |
| - return null; |
| 69 | + return entrySort.get(); |
| 70 | + } |
| 71 | + |
| 72 | + @SuppressWarnings("unchecked") |
| 73 | + @Override |
| 74 | + public <T> T getBean(Class<T> beanClass, String name) { |
| 75 | + |
| 76 | + BeanEntry<T> candidate = candidate(beanClass, name); |
| 77 | + return (candidate == null) ? null : candidate.getBean(); |
72 | 78 | }
|
73 | 79 |
|
74 | 80 | @Override
|
@@ -125,4 +131,49 @@ public void close() {
|
125 | 131 | }
|
126 | 132 | }
|
127 | 133 | }
|
| 134 | + |
| 135 | + private static class EntrySort<T> { |
| 136 | + |
| 137 | + private BeanEntry<T> primary; |
| 138 | + private int primaryCount; |
| 139 | + private BeanEntry<T> secondary; |
| 140 | + private int secondaryCount; |
| 141 | + private BeanEntry<T> normal; |
| 142 | + private int normalCount; |
| 143 | + |
| 144 | + void add(BeanEntry<T> candidate) { |
| 145 | + |
| 146 | + if (candidate != null) { |
| 147 | + if (candidate.getPriority() == Flag.PRIMARY) { |
| 148 | + primary = candidate; |
| 149 | + primaryCount++; |
| 150 | + } else if (candidate.getPriority() == Flag.SECONDARY) { |
| 151 | + secondary = candidate; |
| 152 | + secondaryCount++; |
| 153 | + } else { |
| 154 | + normal = candidate; |
| 155 | + normalCount++; |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + BeanEntry<T> get() { |
| 161 | + if (primaryCount > 1) { |
| 162 | + throw new IllegalStateException("Multiple @Primary beans when only expecting one? Last was: " + primary); |
| 163 | + } |
| 164 | + if (primaryCount == 1) { |
| 165 | + return primary; |
| 166 | + } |
| 167 | + if (normalCount > 1) { |
| 168 | + throw new IllegalStateException("Multiple beans when only expecting one? Maybe use @Primary or @Secondary? Last was: " + normal); |
| 169 | + } |
| 170 | + if (normalCount == 1) { |
| 171 | + return normal; |
| 172 | + } |
| 173 | + if (secondaryCount > 1) { |
| 174 | + throw new IllegalStateException("Multiple @Secondary beans when only expecting one? Last was: " + primary); |
| 175 | + } |
| 176 | + return secondary; |
| 177 | + } |
| 178 | + } |
128 | 179 | }
|
0 commit comments