Skip to content
This repository
Browse code

让Iocbean.fields更完善,支持与Inject一样的语法

  • Loading branch information...
commit 6536b4051ed31ceb88fca0ab7229dd7de0d3246d 1 parent 9e7c9c9
Wendal Chen wendal authored

Showing 1 changed file with 20 additions and 13 deletions. Show diff stats Hide diff stats

  1. +20 13 src/org/nutz/ioc/loader/annotation/AnnotationIocLoader.java
33 src/org/nutz/ioc/loader/annotation/AnnotationIocLoader.java
@@ -152,22 +152,28 @@ private void addClass(Class<?> classZ) {
152 152 fieldList.add(iocField.getName());
153 153 }
154 154 }
155   - // 处理字段(以@IocBean.field方式),只允许引用同名的bean, 就映射为 refer:FieldName
  155 + // 处理字段(以@IocBean.field方式)
156 156 String[] flds = iocBean.fields();
157   -// if (null == flds || flds.length == 0) {
158   -// flds = iocBean.field();
159   -// }
160 157 if (flds != null && flds.length > 0) {
161 158 for (String fieldInfo : flds) {
162 159 if (fieldList.contains(fieldInfo))
163 160 throw duplicateField(classZ,fieldInfo);
164 161 IocField iocField = new IocField();
165   - iocField.setName(fieldInfo);
166   - IocValue iocValue = new IocValue();
167   - iocValue.setType(IocValue.TYPE_REFER);
168   - iocValue.setValue(fieldInfo);
169   - iocField.setValue(iocValue);
170   - iocObject.addField(iocField);
  162 + if (fieldInfo.contains(":")) { // dao:jndi:dataSource/jdbc形式
  163 + String[] datas = fieldInfo.split(":", 2);
  164 + //完整形式, 与@Inject完全一致了
  165 + iocField.setName(datas[0]);
  166 + iocField.setValue(convert(datas[1]));
  167 + iocObject.addField(iocField);
  168 + } else {
  169 + //基本形式, 引用与自身同名的bean
  170 + iocField.setName(fieldInfo);
  171 + IocValue iocValue = new IocValue();
  172 + iocValue.setType(IocValue.TYPE_REFER);
  173 + iocValue.setValue(fieldInfo);
  174 + iocField.setValue(iocValue);
  175 + iocObject.addField(iocField);
  176 + }
171 177 fieldList.add(iocField.getName());
172 178 }
173 179 }
@@ -178,11 +184,12 @@ private void addClass(Class<?> classZ) {
178 184
179 185 protected IocValue convert(String value) {
180 186 IocValue iocValue = new IocValue();
181   - if (value.indexOf(':') > -1) {
  187 + if (value.contains(":")) {
182 188 iocValue.setType(value.substring(0, value.indexOf(':')));
183 189 iocValue.setValue(value.substring(value.indexOf(':') + 1));
184   - } else
185   - iocValue.setValue(value);
  190 + } else {
  191 + iocValue.setValue(value); //TODO 是否应该改为默认refer呢?
  192 + }
186 193 return iocValue;
187 194 }
188 195

0 comments on commit 6536b40

Please sign in to comment.
Something went wrong with that request. Please try again.