Java Code Examples for org.springframework.beans.factory.config.BeanDefinition#isAbstract()

The following examples show how to use org.springframework.beans.factory.config.BeanDefinition#isAbstract() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DataDictionaryConfigurationTest.java    From kfs with GNU Affero General Public License v3.0 6 votes vote down vote up
public void testAllParentBeansAreAbstract() throws Exception {
    Field f = dataDictionary.getClass().getDeclaredField("ddBeans");
    f.setAccessible(true);
    KualiDefaultListableBeanFactory ddBeans = (KualiDefaultListableBeanFactory)f.get(dataDictionary);
    List<String> failingBeanNames = new ArrayList<String>();
    for ( String beanName : ddBeans.getBeanDefinitionNames() ) {
        BeanDefinition beanDef = ddBeans.getMergedBeanDefinition(beanName);
        String beanClass = beanDef.getBeanClassName();
        // skip Rice classes
        if ( beanClass != null && beanClass.startsWith("org.kuali.rice") ) {
            continue;
        }
        if ( (beanName.endsWith("-parentBean") || beanName.endsWith("-baseBean"))
                && !beanDef.isAbstract() ) {
            failingBeanNames.add(beanName + " : " + beanDef.getResourceDescription()+"\n");
        }
    }
    assertEquals( "The following parent beans are not defined as abstract:\n" + failingBeanNames, 0, failingBeanNames.size() );
}
 
Example 2
Source File: SpringLoader.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
@Override
public <T> Collection<Plugin<T>> load(final Class<T> extensible) {
    if (extensible == null) {
        return null;
    }
    List<Plugin<T>> result = new LinkedList<>();
    if (registry != null) {
        BeanDefinition definition;
        Class<?> clazz;
        for (String name : registry.getBeanDefinitionNames()) {
            definition = registry.getBeanDefinition(name);
            if (!definition.isAbstract() && !isEmpty(definition.getBeanClassName())) {
                try {
                    clazz = ClassUtils.forName(definition.getBeanClassName(), Thread.currentThread().getContextClassLoader());
                    //工程方法创建Bean,不支持FactoryBean
                    String factoryMethodName = definition.getFactoryMethodName();
                    if (!isEmpty(factoryMethodName) && isEmpty(definition.getFactoryBeanName())) {
                        //找到方法
                        Method[] methods = clazz.getMethods();
                        for (Method method : methods) {
                            if (method.getName().equals(factoryMethodName)) {
                                //获取方法的返回类型
                                clazz = method.getReturnType();
                                break;
                            }
                        }
                    }
                    if (extensible.isAssignableFrom(clazz)) {
                        //延迟加载,防止Bean还没有初始化好
                        result.add(new Plugin<T>(new Name<>((Class<T>) clazz, name), instance,
                                definition.isSingleton(), null, this));
                    }
                } catch (Exception ignored) {
                }
            }
        }
    }
    return result;
}
 
Example 3
Source File: DataDictionaryConfigurationTest.java    From kfs with GNU Affero General Public License v3.0 5 votes vote down vote up
protected void somethingShouldHaveParentBeans( Class<?> baseClass, List<String> exclusions ) throws Exception {
    Field f = dataDictionary.getClass().getDeclaredField("ddBeans");
    f.setAccessible(true);
    KualiDefaultListableBeanFactory ddBeans = (KualiDefaultListableBeanFactory)f.get(dataDictionary);
    List<String> failingBeanNames = new ArrayList<String>();

    for ( String beanName : ddBeans.getBeanDefinitionNames() ) {
        if ( doesBeanNameMatchList(beanName, exclusions)) {
            continue ;
        }
        BeanDefinition beanDef = ddBeans.getMergedBeanDefinition(beanName);
        String beanClass = beanDef.getBeanClassName();
        if ( beanClass == null ) {
            System.err.println( "ERROR: Bean " + beanName + " has a null class." );
        }
        if ( !beanDef.isAbstract()
                && beanClass != null
                && baseClass.isAssignableFrom(Class.forName(beanClass) ) ) {
            try {
                BeanDefinition parentBean = ddBeans.getBeanDefinition(beanName + "-parentBean");
            } catch ( NoSuchBeanDefinitionException ex ) {
                failingBeanNames.add(beanName + " : " + beanDef.getResourceDescription() +"\n");
            }
        }
    }
    assertEquals( "The following " + baseClass.getSimpleName() + " beans do not have \"-parentBean\"s:\n" + failingBeanNames, 0, failingBeanNames.size() );
}
 
Example 4
Source File: SpringConfigurationConsistencyCheckTest.java    From kfs with GNU Affero General Public License v3.0 5 votes vote down vote up
public void testParentBeansShouldBeAbstract() {
    List<String> failingBeanNames = new ArrayList<String>();
    for ( String beanName : SpringContext.applicationContext.getBeanDefinitionNames() ) {
        BeanDefinition beanDef = SpringContext.applicationContext.getBeanFactory().getBeanDefinition(beanName);
        // skip entries in the rice import files or in testing files
        if ( StringUtils.contains( beanDef.getResourceDescription(), "spring-kfs-imported-rice-beans.xml" )
                || StringUtils.contains( beanDef.getResourceDescription(), "-test.xml" ) ) {
            continue;
        }
        if ( beanName.endsWith("-parentBean") && !beanDef.isAbstract() ) {
            failingBeanNames.add(beanName + " : " + beanDef.getResourceDescription()+"\n");
        }
    }
    assertEquals( "The following parent beans are not defined as abstract:\n" + failingBeanNames, 0, failingBeanNames.size() );
}
 
Example 5
Source File: SpringConfigurationConsistencyCheckTest.java    From kfs with GNU Affero General Public License v3.0 5 votes vote down vote up
public void testServicesShouldHaveParentBeans() {
    List<String> failingBeanNames = new ArrayList<String>();
    for ( String beanName : SpringContext.applicationContext.getBeanDefinitionNames() ) {
        // skip testing mock beans
        if ( StringUtils.containsIgnoreCase(beanName, "mock") ) {
            continue;
        }
        BeanDefinition beanDef = SpringContext.applicationContext.getBeanFactory().getBeanDefinition(beanName);
        // skip entries in the rice import files or in testing files
        if ( StringUtils.contains( beanDef.getResourceDescription(), "spring-kfs-imported-rice-beans.xml" )
                || StringUtils.contains( beanDef.getResourceDescription(), "-test.xml" ) ) {
            continue;
        }
        String serviceClass = beanDef.getBeanClassName();
        if (  StringUtils.contains(serviceClass, "service.impl") &&  //should be a service
                !StringUtils.startsWith(serviceClass, "org.kuali.rice") && //let rice test their code
                !beanDef.isAbstract() ) { //abstract = parent

            try {
                BeanDefinition parentBean = SpringContext.applicationContext.getBeanFactory().getBeanDefinition(beanName + "-parentBean");
                String parentClass = parentBean.getBeanClassName();
            } catch ( NoSuchBeanDefinitionException ex ) {
                failingBeanNames.add(beanName + " : " + beanDef.getResourceDescription()+"\n");
            }
        }
    }
    assertEquals( "The following service beans do not have \"-parentBean\"s:\n" + failingBeanNames, 0, failingBeanNames.size() );
}
 
Example 6
Source File: StatefulFactory.java    From statefulj with Apache License 2.0 5 votes vote down vote up
/**
 * Iterate thru all beans and fetch the StatefulControllers
 *
 * @param reg
 * @return
 * @throws ClassNotFoundException
 */
private void mapControllerAndEntityClasses(
		BeanDefinitionRegistry reg,
		Map<String, Class<?>> controllerToEntityMapping,
		Map<Class<?>, String> entityToRepositoryMapping,
		Map<Class<?>, Set<String>> entityToControllerMappings) throws ClassNotFoundException {

	// Loop thru the bean registry
	//
	for(String bfName : reg.getBeanDefinitionNames()) {

		BeanDefinition bf = reg.getBeanDefinition(bfName);

		if (bf.isAbstract()) {
			logger.debug("Skipping abstract bean " + bfName);
			continue;
		}

		Class<?> clazz = getClassFromBeanDefinition(bf, reg);

		if (clazz == null) {
			logger.debug("Unable to resolve class for bean " + bfName);
			continue;
		}

		// If it's a StatefulController, map controller to the entity and the entity to the controller
		//
		if (ReflectionUtils.isAnnotationPresent(clazz, StatefulController.class)) {
			mapEntityWithController(controllerToEntityMapping, entityToControllerMappings, bfName, clazz);
		}

		// Else, if the Bean is a Repository, then map the
		// Entity associated with the Repo to the PersistenceSupport object
		//
		else if (RepositoryFactoryBeanSupport.class.isAssignableFrom(clazz)) {
			mapEntityToRepository(entityToRepositoryMapping, bfName, bf);
		}
	}
}
 
Example 7
Source File: SpringBeanLocator.java    From cxf with Apache License 2.0 4 votes vote down vote up
public boolean hasConfiguredPropertyValue(String beanName, String propertyName, String searchValue) {
    if (context.containsBean(beanName) && !passThroughs.contains(beanName)) {
        ConfigurableApplicationContext ctxt = (ConfigurableApplicationContext)context;
        BeanDefinition def = ctxt.getBeanFactory().getBeanDefinition(beanName);
        if (!ctxt.getBeanFactory().isSingleton(beanName) || def.isAbstract()) {
            return false;
        }
        Collection<?> ids = null;
        PropertyValue pv = def.getPropertyValues().getPropertyValue(propertyName);

        if (pv != null) {
            Object value = pv.getValue();
            if (!(value instanceof Collection)) {
                throw new RuntimeException("The property " + propertyName + " must be a collection!");
            }

            if (value instanceof Mergeable) {
                if (!((Mergeable)value).isMergeEnabled()) {
                    ids = (Collection<?>)value;
                }
            } else {
                ids = (Collection<?>)value;
            }
        }

        if (ids != null) {
            for (Iterator<?> itr = ids.iterator(); itr.hasNext();) {
                Object o = itr.next();
                if (o instanceof TypedStringValue) {
                    if (searchValue.equals(((TypedStringValue) o).getValue())) {
                        return true;
                    }
                } else {
                    if (searchValue.equals(o)) {
                        return true;
                    }
                }
            }
        }
    }
    return orig.hasConfiguredPropertyValue(beanName, propertyName, searchValue);
}