public <T> BeanDefinitionBase create(String id, Class<? super T> typeClass, boolean primary, String name, boolean lazyInit, Class<?> beanClass, Object... constructorArguments) { BeanDefinitionBase bd = new BeanDefinitionBase(RUNTIME_SOURCE); bd.setId(id); bd.setName(name); bd.setType(typeClass); bd.setBeanClass(beanClass); bd.setBeanClassType(BeanType.of(beanClass)); bd.setPrimary(primary); bd.setSingleton(true); bd.setLazyInit(lazyInit); for (Object arg : constructorArguments) { bd.addConstructorArgument(new ArgumentDefinition(new ValueDefinition(arg))); } resolveBeanConstructor(bd); return bd; }
protected void addNotRepeatAdditionalTypeDef(BeanDefinitionBase bd, Class<?> cls){ for(TypeDefinition def : bd.getAdditionalTypeDefs()){ if(def.getType() == cls){ return; } } TypeDefinitionBase def = new TypeDefinitionBase(); def.setType(cls); bd.addAdditionalTypeDef(def); } }
protected void readInvoke(BeanContainer container, XmlReader reader, LoaderContext context, BeanDefinitionBase bean) { InvokeDefinition invoke = new InvokeDefinition(); readMethodDefinition(reader, context, bean, bean.getBeanClass(), invoke, true); bean.addInvoke(invoke); }
protected <T> BeanDefinitionBase createBeanDefinition(Class<T> type, T bean, String name, boolean primary){ BeanDefinitionBase bd = new BeanDefinitionBase(XmlBeanDefinitionLoader.RUNTIME_SOURCE); bd.setName(name); bd.setType(type); bd.setBeanClass(bean.getClass()); bd.setSingleton(true); bd.setSingletonInstance(bean); bd.setPrimary(primary); return bd; }
protected BeanDefinitionBase create(Class<?> type, Class<?> beanClass) { BeanDefinitionBase bd = new BeanDefinitionBase(XmlBeanDefinitionLoader.RUNTIME_SOURCE); bd.setType(type); bd.setBeanClass(beanClass); bd.setBeanClassType(BeanType.of(beanClass)); bd.setSingleton(true); bd.setPrimary(true); return bd; }
BeanDefinitionBase bean = new BeanDefinitionBase(reader.getSource()); bean.setNamespace(proxy ? reader.getAttribute(TARGET_NAMESPACE_ATTRIBUTE) : reader.getAttribute(NAMESPACE_ATTRIBUTE)); bean.setId(proxy ? reader.getAttribute(TARGET_ID_ATTRIBUTE) : reader.getAttribute(ID_ATTRIBUTE)); bean.setName(proxy ? reader.getAttribute(TARGET_NAME_ATTRIBUTE) : reader.getAttribute(NAME_ATTRIBUTE)); AppClassLoader.addInstrumentClass(beanClassName); log.trace("Resolving bean class '{}'...", beanClassName); bean.setBeanClass(forName(beanClassName)); } catch (NestedClassNotFoundException e) { throw new BeanDefinitionException("Error resolving bean class '" + beanClassName + "' , source : " + reader.getSource(), e); bean.setInitMethod(Reflection.findMethod(bean.getBeanClass(), initMethodName)); if (null == bean.getInitMethod()) { throw new BeanDefinitionException("init method '" + initMethodName + "' not found in class '" + beanClassName + "', source : " + reader.getSource()); bean.setDestroyMethod(Reflection.findMethod(bean.getBeanClass(), destroyMethodName)); if (null == bean.getDestroyMethod()) { throw new BeanDefinitionException("destroy method '" + initMethodName + "' not found in class '" + beanClassName + "', source : " + reader.getSource()); bean.setBeanClassType(BeanType.of(bean.getBeanClass())); if (!type.isAssignableFrom(bean.getBeanClass())) { throw new BeanDefinitionException("bean's type '" + typeClassName + "' must be assignable from bean's class '" + beanClassName + "', source : " + reader.getSource()); bean.setType(type); } else { bean.setType(bean.getBeanClass());
protected void readBean(BeanContainer container,Class<?> cls){ Bean a = cls.getAnnotation(Bean.class); BeanDefinitionBase bd = new BeanDefinitionBase(Classes.getClassResourcePath(cls)); bd.setAnnotation(true); if(!Strings.isEmpty(a.value())) { bd.setId(a.value()); }else{ bd.setType(a.type()); }else{ bd.setType(resolveBeanType(cls,a)); bd.setId(a.id()); bd.setName(a.name()); bd.setBeanClass(cls); bd.setBeanClassType(BeanType.of(cls)); bd.setPrimary(a.primary()); bd.setSingleton(a.singleton()); bd.setLazyInit(a.lazyInit()); bd.setSortOrder(a.sortOrder()); throw new BeanDefinitionException("Target type '" + target.getName() + "' not found, source : " + cls.getName()); bd.addFactoryBeanDef(new FactoryBeanDefinitionBase(target));
protected BeanDefinitionBase add(BeanDefinitionBase bd) throws BeanDefinitionException { if(null != appContext && !appContext.isServletEnvironment()){ boolean ignore; Class<?> beanClass = bd.getBeanClass(); String id = bd.getId(); if(null != existsBeanDefinition){ throw new BeanDefinitionException("Found duplicated bean id '" + id + "' in resource : " + bd.getSource() + ", " + existsBeanDefinition.getSource()); if(!Strings.isEmpty(bd.getNamespace()) && !Strings.isEmpty(bd.getName())) { id = id(bd.getNamespace(), bd.getName()); BeanDefinitionBase existsBeanDefinition = identifiedBeanDefinitions.get(id); if(null != existsBeanDefinition){ throw new BeanDefinitionException("Found duplicated bean namespace '" + bd.getNamespace() + "' and name '" + bd.getName() + "' in resource : " + bd.getSource() + ", " + existsBeanDefinition.getSource()); if(null == bd.getType()) { bd.setType(bd.getBeanClass()); defs.addAll(bd.getAdditionalTypeDefs()); Set<BeanDefinitionBase> clsSet = beanClassDefinitions.get(bd.getBeanClass()); if(null == clsSet){ clsSet = new HashSet<>(1); beanClassDefinitions.put(bd.getBeanClass(), clsSet);
BeanDefinitionBase existsBeanDefinition = namedBeanDefinitions.get(key); if(existsBeanDefinition != null){ Optional<TypeDefinition> op = existsBeanDefinition.getAdditionalTypeDefs().stream() .filter(td -> td.getType().equals(type.getType())).findAny(); TypeDefinition existTd = op.orElse(existsBeanDefinition); namedBeanDefinitions.put(key, bd); }else if(type.isOverride() && existTd.isOverride()){ if(bd.getSortOrder() == existsBeanDefinition.getSortOrder()){ log.warn("Found duplicated name bean " + (proxy ? "proxy " : "bean ") + definition + " for type '" + beanType.getName() + "' with exists " + (proxy ? "proxy" : "bean") + " in " + existsBeanDefinition.getSource()+", please use sort-order to defined which bean definition to be use."); log.warn("Now use is "+bd); if(bd.getSortOrder() >= existsBeanDefinition.getSortOrder()){ log.info("In profile '"+config.getProfile()+"' override exist bean definition " + existsBeanDefinition + " with " + definition); namedBeanDefinitions.put(key, bd); if(null != existsBeanDefinition && existsBeanDefinition != NULL_BD && !existsBeanDefinition.isDefaultOverride()){ Optional<TypeDefinition> op = existsBeanDefinition.getAdditionalTypeDefs().stream() .filter(td -> td.getType().equals(type.getType())).findAny(); TypeDefinition existTd = op.orElse(existsBeanDefinition); throw new BeanDefinitionException("Found duplicated primary " + (proxy ? "proxy " : "bean ") + definition + " for type '" + beanType.getName() + "' with exists " + (proxy ? "proxy" : "bean") + " in " + existsBeanDefinition.getSource()); primaryBeanDefinitions.put(beanType, bd);
protected BeanDefinitionBase readMapBean(BeanContainer container, XmlReader reader, LoaderContext context) { BeanDefinitionBase bean = new BeanDefinitionBase(reader.getSource()); readValuedBeanAttributes(container, reader, context, bean); bean.setBeanClass(classAttribute(reader, MAP_CLASS_ATTRIBUTE, false)); bean.setValueDefinition(readMap(container, reader, context)); bean.getValueDefinition().setDefinedType(bean.getBeanClass()); if (null == bean.getBeanClass()) { bean.setBeanClass(LinkedHashMap.class); } return bean; }
protected void readProperty(BeanContainer container, XmlReader reader, LoaderContext context, BeanDefinitionBase bean) { if(!testIfAttributes(container, reader)) { reader.nextToEndElement(); return; } PropertyDefinition prop = new PropertyDefinition(); prop.setName(reader.getRequiredAttribute(NAME_ATTRIBUTE)); prop.setDefaultValue(reader.getAttribute(DEFAULT_VALUE_ATTRIBUTE)); prop.setValueDefinition(readValue(container, reader, context, PROPERTY_ELEMENT)); prop.setProperty(bean.getBeanClassType().getProperty(prop.getName())); if (null == prop.getProperty()) { throw new BeanDefinitionException("property '" + prop.getName() + "' not found in bean class '" + bean.getBeanClass().getName() + ", source : " + reader.getSource()); } if (null == prop.getValueDefinition()) { throw new BeanDefinitionException("property '" + prop.getName() + "' must define a value, check source : " + bean.getSource()); } bean.addProperty(prop); }
@SuppressWarnings("unchecked") protected Object doBeanCreationByConstructor(BeanDefinitionBase bd){ Class<?> beanClass = bd.getBeanClass(); Object bean = null; List<ArgumentDefinition> constructorArguments = bd.getConstructorArguments(); if(constructorArguments.isEmpty()){ ReflectClass rc = ReflectClass.of(beanClass); ReflectParameter p = dc.getParameters()[i]; if(beanConfigurator.configure(args, p, bd.getConfigurationPrefix())){ continue; bean = Reflection.newInstance(bd.getConstructor(),doResolveArgs(bd, constructorArguments));
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()); } }
protected void readBeanTypeDef(BeanContainer container, XmlReader reader, LoaderContext context, BeanDefinitionBase bean) { TypeDefinitionBase def = new TypeDefinitionBase(); String typeClassName = reader.getRequiredAttribute(TYPE_ATTRIBUTE); Class<?> typeClass = tryForName(typeClassName); if (null == typeClass) { throw new BeanDefinitionException("Class '" + typeClassName + "' not found, check source : " + bean.getSource()); } if (!typeClass.isAssignableFrom(bean.getBeanClass())) { throw new BeanDefinitionException("bean's type '" + typeClassName + "' must be assignable from bean's class '" + bean.getBeanClass().getName() + "', source : " + reader.getSource()); } def.setType(typeClass); def.setName(reader.getAttribute(NAME_ATTRIBUTE)); def.setPrimary(reader.getBooleanAttribute(PRIMARY_ATTRIBUTE, false)); def.setOverride(reader.getBooleanAttribute(OVERRIDE_ATTRIBUTE, context.defaultOverride)); bean.addAdditionalTypeDef(def); }
protected void resolveBeanConstructor(BeanDefinitionBase bean) { Class<?> beanClass = bean.getBeanClass(); if (null != beanClass && !bean.getConstructorArguments().isEmpty()) { Constructor<?> beanConstructor = null; List<Constructor<?>> matchContructors = New.arrayList(); for (Constructor<?> constructor : beanClass.getConstructors()) { Class<?>[] paramTypes = constructor.getParameterTypes(); if (paramTypes.length == bean.getConstructorArguments().size()) { boolean matched = true; for (int i = 0; i < paramTypes.length; i++) { Class<?> pt = paramTypes[i]; ArgumentDefinition ad = bean.getConstructorArguments().get(i); String ptClassName = ad.getTypeClassName(); if (!Strings.isEmpty(ptClassName) && !(ptClassName.equals(pt.getSimpleName()) || ptClassName.equals(pt.getName()))) { Class<?> definedType = bd.getBeanClass(); if (definedType == null) { continue; bean.setConstructor(beanConstructor); for (int i = 0; i < bean.getConstructorArguments().size(); i++) { ArgumentDefinition ad = bean.getConstructorArguments().get(i); Class<?> type = beanConstructor.getParameterTypes()[i];
protected Object doCreateProxy(BeanDefinitionBase pd, Class<?> type, Object bean){ Object proxy; if(ProxyBean.class.isAssignableFrom(pd.getBeanClass())) { proxy = doCreateBeanOnly(pd); ((ProxyBean)proxy).setTargetBean(bean); }else{ ReflectConstructor c = ReflectClass.of(pd.getBeanClass()).getConstructor(type); if(null == c) { throw new BeanCreationException("Can't create proxy '" + pd.getBeanClass() + "', No valid constructor"); } proxy = c.newInstance(bean); beforeBeanCreation(pd); processBeanCreation(pd, proxy); afterBeanCreation(pd); pd.setInited(true); } return proxy; }
protected void readBeanFactoryDef(BeanContainer container, XmlReader reader, LoaderContext context, BeanDefinitionBase bean) { if (!(FactoryBean.class.isAssignableFrom(bean.getBeanClass()))) { throw new BeanDefinitionException("Bean [" + bean + "] must be implements FactoryBean"); } String targetTypeName = reader.getRequiredAttribute(TARGET_TYPE_ATTRIBUTE); if (Strings.isEmpty(targetTypeName)) { throw new BeanDefinitionException("Attribute '" + TARGET_TYPE_ATTRIBUTE + "' of element '" + REGISTER_BEAN_FACTORY_ELEMENT + "' must not be empty, source : " + reader.getSource()); } Class<?> targetType = tryForName(targetTypeName); if (null == targetType) { throw new BeanDefinitionException("Target type '" + targetTypeName + "' not found, source : " + reader.getSource()); } bean.addFactoryBeanDef(new FactoryBeanDefinitionBase(targetType)); }
protected void doBeanSetProperties(BeanDefinitionBase bd,Object bean){ BeanType beanType = bd.getBeanClassType(); for(PropertyDefinition pd : bd.getProperties()){ BeanProperty bp = beanType.getProperty(pd.getName()); if(null == bp){ throw new BeanCreationException("No such property '" + pd.getName() + " in bean " + bd); } bp.setValue(bean, doResolveValue(bd, pd.getValueDefinition(), pd.getDefaultValue())); } }
public BeanDefinitionBase remove(Class<?> type, Class<?> cls) { Set<BeanDefinitionBase> set = beanTypeDefinitions.get(type); if(null != set) { Set<BeanDefinitionBase> found = new HashSet<>(); Set<BeanDefinitionBase> notFound = new LinkedHashSet<>(); for(BeanDefinitionBase bd : set) { if(bd.getBeanClass().equals(cls)) { found.add(bd); }else{ notFound.add(bd); } } set.clear(); set.addAll(notFound); for(BeanDefinitionBase bd : found) { removeAll(bd); } } return null; }
protected void readConstructorArgument(BeanContainer container, XmlReader reader, LoaderContext context, BeanDefinitionBase bean) { ArgumentDefinition arg = new ArgumentDefinition(); arg.setTypeClassName(reader.getAttribute(TYPE_ATTRIBUTE)); arg.setDefaultValue(reader.getAttribute(DEFAULT_VALUE_ATTRIBUTE)); arg.setValueDefinition(readValue(container, reader, context, CONSTRUCTOR_ARG_ELEMENT)); bean.addConstructorArgument(arg); }