/** assign to fields by SpringBeanByType */ public static void injectSpringBeanByType( final ApplicationContext ctx, final Class testedClazz, final Object testedObject) { Set<Field> springBeanByTypeFields = getFieldsAnnotatedWith(testedClazz, SpringBeanByType.class); for (Field field : springBeanByTypeFields) { try { FieldHelper.setFieldValue(testedObject, field, getSpringBeanByType(ctx, field.getType())); } catch (Throwable e) { throw new Test4JException( "Unable to assign the Spring bean value to field annotated with @SpringBeanByType", e); } } }
/** assign to fields by SpringBeanByName */ public static void injectSpringBeanByName( final ApplicationContext ctx, final Class testedClazz, final Object testedObject) { Set<Field> springBeanByNamefields = getFieldsAnnotatedWith(testedClazz, SpringBeanByName.class); for (Field field : springBeanByNamefields) { try { SpringBeanByName byName = field.getAnnotation(SpringBeanByName.class); String beanName = field.getName(); if (StringHelper.isBlankOrNull(byName.value()) == false) { beanName = byName.value(); } FieldHelper.setFieldValue(testedObject, field, ctx.getBean(beanName)); } catch (Throwable e) { throw new Test4JException( "Unable to assign the Spring bean value to field annotated with @SpringBeanByName", e); } } }
/** * {@inheritDoc}<br> * <br> * 根据@SpringBeanByType注入spring bean<br> */ @Override public void injectBy(Test4JBeanFactory beanFactory, Object testedObject) { Class testedClazz = testedObject.getClass(); Set<Field> fields = getFieldsAnnotatedWith(testedClazz, SpringBeanByType.class); for (Field field : fields) { try { Object bean = getSpringBeanByType(beanFactory, field.getType()); FieldHelper.setFieldValue(testedObject, field, bean); } catch (Throwable e) { String error = String.format( "inject @SpringBeanByType field[%s] in class[%s] error.", field.getName(), testedClazz.getName()); throw new Test4JException(error, e); } } }