Example usage for org.springframework.beans.factory.support DefaultListableBeanFactory getBeansWithAnnotation

List of usage examples for org.springframework.beans.factory.support DefaultListableBeanFactory getBeansWithAnnotation

Introduction

In this page you can find the example usage for org.springframework.beans.factory.support DefaultListableBeanFactory getBeansWithAnnotation.

Prototype

@Override
    public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) 

Source Link

Usage

From source file:com.helpinput.spring.refresher.SessiontRefresher.java

@SuppressWarnings("unchecked")
ManagedList<Object> getManageList(DefaultListableBeanFactory dlbf, PropertyValue oldPropertyValue) {
    Set<String> oldClasses = null;

    if (oldPropertyValue != null) {
        Object value = oldPropertyValue.getValue();
        if (value != null && value instanceof ManagedList) {
            ManagedList<Object> real = (ManagedList<Object>) value;
            oldClasses = new HashSet<>(real.size() >>> 1);
            ClassLoader parentClassLoader = ClassUtils.getDefaultClassLoader();
            for (Object object : real) {
                TypedStringValue typedStringValue = (TypedStringValue) object;
                String className = typedStringValue.getValue();
                try {
                    parentClassLoader.loadClass(className);
                    oldClasses.add(className);
                } catch (ClassNotFoundException e) {
                }//from   w  w  w . j  a  va  2s . c o  m
            }
        }
    }

    int oldClassSize = (Utils.hasLength(oldClasses) ? oldClasses.size() : 0);
    Map<String, Object> beans = dlbf.getBeansWithAnnotation(Entity.class);
    HashSet<String> totalClasses = new HashSet<>(beans.size() + oldClassSize);
    if (oldClassSize > 0) {
        totalClasses.addAll(oldClasses);
    }

    for (Object entity : beans.values()) {
        String clzName = entity.getClass().getName();
        if (!totalClasses.contains(clzName)) {
            totalClasses.add(clzName);
        }
    }

    ManagedList<Object> list = new ManagedList<>(totalClasses.size());
    for (String clzName : totalClasses) {
        TypedStringValue typedStringValue = new TypedStringValue(clzName);
        list.add(typedStringValue);
    }
    return list;
}