public BeanContainer(AppConfig config){ this.config = config; this.beanConfigurator = new BeanConfigurator(config); this.placeholderResolver = config.getPlaceholderResolver(); this.annotationBeanDefinitionLoader = new AnnotationBeanDefinitionLoader(); this.xmlBeanDefinitionLoader = new XmlBeanDefinitionLoader(this); }
@Override public void configure(Object bean, String prefix) { beanConfigurator.configure(bean, BeanType.of(bean.getClass()), prefix); }
public boolean configure(Object[] args, ReflectParameter p, String keyPrefix) { keyPrefix = keyPrefix(keyPrefix); if(Property.class.isAssignableFrom(p.getType())) { doBeanConfigure(args, p, keyPrefix, p.getAnnotation(ConfigProperty.class)); return true; } ConfigProperty a = p.getAnnotation(ConfigProperty.class); if(null != a) { doBeanConfigure(args, p, keyPrefix, a); return true; } return false; }
protected void doBeanConfigure(Object bean, ReflectValued v, String keyPrefix, ConfigProperty a) { String defaultValue = null == a ? null : a.defaultValue(); if(null != a) { String[] keys = a.key(); if(keys.length == 0) { keys = a.value(); } if(keys.length > 0) { for(String key : keys) { if(doBeanConfigureByKey(bean, v, key(keyPrefix, key), defaultValue)) { break; } } return; } } if(doBeanConfigureByKey(bean, v, keyPrefix + v.getName(), defaultValue)) { return; } if(doBeanConfigureByKey(bean, v, keyPrefix + Strings.lowerHyphen(v.getName()), defaultValue)) { return; } if(doBeanConfigureByKey(bean, v, keyPrefix + Strings.lowerUnderscore(v.getName()), defaultValue)) { return; } }
public void configure(Object bean, BeanType bt, String keyPrefix) { keyPrefix = keyPrefix(keyPrefix); Configurable.Nested nested = bp.getAnnotation(Configurable.Nested.class); if(null != nested) { configureNested(bean, keyPrefix, bp, nested); continue; doBeanConfigure(bean, new SetByStringValued(bean, bp, setByStringMethod), keyPrefix, a); }else if(bp.isWritable()) { doBeanConfigure(bean, bp, keyPrefix, a); }else{ ReflectField rf = bp.getReflectField(); doBeanConfigure(bean, rf, keyPrefix, a); doBeanConfigure(bean, field, keyPrefix, field.getAnnotation(ConfigProperty.class)); continue; configureNested(bean, keyPrefix, field, nested); continue; doBeanConfigure(bean, field, keyPrefix, a); Configurable.Nested nested = m.getAnnotation(Configurable.Nested.class); if(null != nested) { configureNested(bean, keyPrefix, new MethodReflectValued(bean, m), nested); continue; doBeanConfigure(bean, new MethodReflectValued(bean, m), keyPrefix, a);
private void configureNested(Object bean, String keyPrefix, ReflectValued member, Configurable.Nested nested) { String nestedPrefix = nested.prefix(); if(Strings.isEmpty(nestedPrefix)) { nestedPrefix = Strings.lowerUnderscore(member.getName()); if(nestedPrefix.endsWith("_config")) { nestedPrefix = Strings.removeEnd(nestedPrefix, "_config"); } } String fullKeyPrefix = keyPrefix(keyPrefix + nestedPrefix); Object nestedBean = member.getValue(bean); BeanType nestedType; if(null == nestedBean) { nestedType = BeanType.of(member.getType()); nestedBean = nestedType.newInstance(); member.setValue(bean, nestedBean); }else{ nestedType = BeanType.of(nestedBean.getClass()); } configure(nestedBean, nestedType, fullKeyPrefix); }
protected boolean doBeanConfigureByKey(Object bean, ReflectValued v, String key, String defaultValue) { if(Property.class.isAssignableFrom(v.getType())) { doBeanConfigureDynaProperty(bean, v, key, defaultValue); return true;
protected void doBeanConfigure(BeanDefinitionBase bd, Object bean) throws Throwable { if(null != bd && bd.isConfigurable()) { BeanType bt = null == bd ? BeanType.of(bean.getClass()) : bd.getBeanClassType(); beanConfigurator.configure(bean, bt, bd.getConfigurationPrefix()); } }
ReflectParameter p = dc.getParameters()[i]; if(beanConfigurator.configure(args, p, bd.getConfigurationPrefix())){ continue;