Example usage for org.springframework.beans.factory.config BeanDefinition getBeanClassName

List of usage examples for org.springframework.beans.factory.config BeanDefinition getBeanClassName

Introduction

In this page you can find the example usage for org.springframework.beans.factory.config BeanDefinition getBeanClassName.

Prototype

@Nullable
String getBeanClassName();

Source Link

Document

Return the current bean class name of this bean definition.

Usage

From source file:com.reactivetechnologies.platform.datagrid.util.EntityFinder.java

/**
 * //  ww w .j a v  a  2 s .com
 * @param basePkg
 * @return
 * @throws ClassNotFoundException
 */
public static Collection<Class<?>> findEntityClasses(String basePkg) throws ClassNotFoundException {
    ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(
            false);
    provider.addIncludeFilter(new TypeFilter() {

        @Override
        public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
                throws IOException {
            AnnotationMetadata aMeta = metadataReader.getAnnotationMetadata();
            return aMeta.hasAnnotation(HzMapConfig.class.getName());
        }
    });
    //consider the finder class to be in the root package
    Set<BeanDefinition> beans = null;
    try {
        beans = provider.findCandidateComponents(StringUtils.hasText(basePkg) ? basePkg
                : EntityFinder.class.getName().substring(0, EntityFinder.class.getName().lastIndexOf(".")));
    } catch (Exception e) {
        throw new IllegalArgumentException("Unable to scan for entities under base package", e);
    }

    Set<Class<?>> classes = new HashSet<>();
    if (beans != null && !beans.isEmpty()) {
        classes = new HashSet<>(beans.size());
        for (BeanDefinition bd : beans) {
            classes.add(Class.forName(bd.getBeanClassName()));
        }
    } else {
        log.warn(">> Did not find any key value entities under the given base scan package [" + basePkg + "]");
    }
    return classes;

}

From source file:com.jk.annotations.AnnotationDetector.java

/**
 * Utility method to scan the given package and handler for the annotation
 * of the given class. Its uses the Spring annotation detector
 *
 * @param clas/* w  ww  .j  a  v a2 s  .com*/
 *            the clas
 * @param basePackage
 *            the base package
 * @param handler
 *            the handler
 */
public static void scan(final Class<? extends Annotation> clas, final String[] basePackage,
        final AnnotationHandler handler) {
    final ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(
            false);
    scanner.setResourceLoader(
            new PathMatchingResourcePatternResolver(Thread.currentThread().getContextClassLoader()));
    scanner.addIncludeFilter(new AnnotationTypeFilter(clas));
    for (final String pck : basePackage) {
        for (final BeanDefinition bd : scanner.findCandidateComponents(pck)) {
            handler.handleAnnotationFound(bd.getBeanClassName());
        }
    }
}

From source file:name.marcelomorales.siqisiqi.openjpa.spring.SpringRegistrar.java

public static void registerEntities(EntityRepository provider, String... basePackage) {
    for (String pack : basePackage) {
        ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(
                false);/*from  ww w. j  a va2  s  .com*/

        scanner.addIncludeFilter(new AnnotationTypeFilter(Entity.class));

        for (BeanDefinition bd : scanner.findCandidateComponents(pack)) {
            try {
                final Class<?> aClass = Class.forName(bd.getBeanClassName());
                LOGGER.info("Adding {} to entity repository", aClass);
                provider.register(aClass);
            } catch (ClassNotFoundException e) {
                LOGGER.error("Error", e);
            }
        }
    }
}

From source file:com.oembedler.moon.graphql.engine.GraphQLSchemaDiscoverer.java

public static Set<Class<?>> findSchemaClasses(final String basePackage) throws ClassNotFoundException {

    Set<Class<?>> initialEntitySet = new HashSet<Class<?>>();

    if (StringUtils.hasText(basePackage)) {
        ClassPathScanningCandidateComponentProvider componentProvider = new ClassPathScanningCandidateComponentProvider(
                false);/*from   ww w.j a v  a  2  s  .c o  m*/
        componentProvider.addIncludeFilter(new AnnotationTypeFilter(GRAPH_QL_SCHEMA_ANNOTATION));

        for (BeanDefinition candidate : componentProvider.findCandidateComponents(basePackage)) {
            initialEntitySet.add(ClassUtils.forName(candidate.getBeanClassName(),
                    GraphQLSchemaDiscoverer.class.getClassLoader()));
        }
    }
    return initialEntitySet;
}

From source file:com.reactive.hzdfs.utils.EntityFinder.java

/**
 * // w w  w  .  j a v a2s. c o  m
 * @param provider
 * @param basePkg
 * @return
 * @throws ClassNotFoundException
 */
private static Set<Class<?>> findComponents(ClassPathScanningCandidateComponentProvider provider,
        String basePkg) throws ClassNotFoundException {
    Set<BeanDefinition> beans = null;
    String pkg = "";
    try {
        pkg = StringUtils.hasText(basePkg) ? basePkg : EntityFinder.class.getPackage().getName();
        beans = provider.findCandidateComponents(pkg);
    } catch (Exception e) {
        throw new ClassNotFoundException("Unable to scan for classes under given base package",
                new IllegalArgumentException("Package=> " + pkg, e));
    }

    Set<Class<?>> classes = new HashSet<>();
    if (beans != null && !beans.isEmpty()) {
        classes = new HashSet<>(beans.size());
        for (BeanDefinition bd : beans) {
            classes.add(Class.forName(bd.getBeanClassName()));
        }
    } else {
        log.warn(">> Did not find any classes under the given base package [" + basePkg + "]");
    }
    return classes;
}

From source file:com.reactivetechnologies.platform.utils.EntityFinder.java

/**
 * // ww w.  j  a v a2  s  . c om
 * @param basePkg
 * @return
 * @throws ClassNotFoundException
 */
public static Collection<Class<?>> findMapEntityClasses(String basePkg) throws ClassNotFoundException {
    ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(
            false);
    provider.addIncludeFilter(new TypeFilter() {

        @Override
        public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
                throws IOException {
            AnnotationMetadata aMeta = metadataReader.getAnnotationMetadata();
            return aMeta.hasAnnotation(HzMapConfig.class.getName());
        }
    });
    //consider the finder class to be in the root package
    Set<BeanDefinition> beans = null;
    try {
        beans = provider.findCandidateComponents(StringUtils.hasText(basePkg) ? basePkg
                : EntityFinder.class.getName().substring(0, EntityFinder.class.getName().lastIndexOf(".")));
    } catch (Exception e) {
        throw new IllegalArgumentException("Unable to scan for entities under base package", e);
    }

    Set<Class<?>> classes = new HashSet<>();
    if (beans != null && !beans.isEmpty()) {
        classes = new HashSet<>(beans.size());
        for (BeanDefinition bd : beans) {
            classes.add(Class.forName(bd.getBeanClassName()));
        }
    } else {
        log.warn(">> Did not find any key value entities under the given base scan package [" + basePkg + "]");
    }
    return classes;

}

From source file:com.hortonworks.registries.common.util.ReflectionHelper.java

public static Collection<Class<?>> getAnnotatedClasses(String basePackage,
        Class<? extends Annotation> annotation) {
    Collection<Class<?>> classes = new ArrayList<>();
    ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(
            false);/*w  w w  .j a va  2 s. c  om*/
    provider.addIncludeFilter(new AnnotationTypeFilter(annotation));
    for (BeanDefinition beanDef : provider.findCandidateComponents(basePackage)) {
        try {
            classes.add(Class.forName(beanDef.getBeanClassName()));
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    return classes;
}

From source file:io.acme.solution.application.conf.CommandHandlerUtils.java

public static Map<String, CommandHandler> buildCommandHandlersRegistry(final String basePackage,
        final ApplicationContext context) {

    final Map<String, CommandHandler> registry = new HashMap<>();
    final ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(
            false);/*  w  w  w .  j  ava  2  s. co  m*/
    final AutowireCapableBeanFactory beanFactory = context.getAutowireCapableBeanFactory();
    scanner.addIncludeFilter(new AssignableTypeFilter(CommandHandler.class));

    CommandHandler currentHandler = null;

    for (BeanDefinition bean : scanner.findCandidateComponents(basePackage)) {
        currentHandler = (CommandHandler) beanFactory.createBean(
                ClassUtils.resolveClassName(bean.getBeanClassName(), context.getClassLoader()),
                AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
        registry.put(currentHandler.getInterest(), currentHandler);
    }

    return registry;
}

From source file:ch.sdi.core.util.ClassUtil.java

/**
* Lists all types in the given package (recursive) which are annotated by the given annotation.
* <p>/*www .  j av  a2s.  com*/
* All types which match the criteria are returned, no further checks (interface, abstract, embedded, etc.
* are performed.
* <p>
*
* @param aAnnotation
*        the desired annotation type
* @param aRoot
*        the package name where to start the search. Must not be empty. And not start
*        with 'org.springframework' (cannot parse springs library itself).
* @return a list of found types
*/
public static Collection<? extends Class<?>> findCandidatesByAnnotation(Class<? extends Annotation> aAnnotation,
        String aRoot) {
    if (!StringUtils.hasText(aRoot)) {
        throw new IllegalArgumentException("aRoot must not be empty (cannot parse spring library classes)");

    } // if StringUtils.hasText( aRoot )

    if (aRoot.startsWith("org.springframework")) {
        throw new IllegalArgumentException("cannot parse spring library classes");

    } // if StringUtils.hasText( aRoot )

    List<Class<?>> result = new ArrayList<Class<?>>();

    MyClassScanner scanner = new MyClassScanner();

    scanner.addIncludeFilter(new AnnotationTypeFilter(aAnnotation));
    Set<BeanDefinition> canditates = scanner.findCandidateComponents(aRoot);
    for (BeanDefinition beanDefinition : canditates) {
        try {
            String classname = beanDefinition.getBeanClassName();
            Class<?> clazz = Class.forName(classname);
            result.add(clazz);
        } catch (ClassNotFoundException t) {
            myLog.error("Springs type scanner returns a class name whose class cannot be evaluated!!!", t);
        }
    }

    return result;
}

From source file:com.foilen.smalltools.tools.Hibernate51Tools.java

/**
 * Generate the SQL file. This is based on the code in {@link LocalSessionFactoryBuilder#scanPackages(String...)}
 *
 * @param dialect//w  w w.  j  a va 2  s . c  om
 *            the dialect (e.g: org.hibernate.dialect.MySQL5InnoDBDialect )
 * @param outputSqlFile
 *            where to put the generated SQL file
 * @param useUnderscore
 *            true: to have tables names like "employe_manager" ; false: to have tables names like "employeManager"
 * @param packagesToScan
 *            the packages where your entities are
 */
public static void generateSqlSchema(Class<? extends Dialect> dialect, String outputSqlFile,
        boolean useUnderscore, String... packagesToScan) {

    BootstrapServiceRegistry bootstrapServiceRegistry = new BootstrapServiceRegistryBuilder().build();

    MetadataSources metadataSources = new MetadataSources(bootstrapServiceRegistry);

    ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(
            false);
    scanner.addIncludeFilter(new AnnotationTypeFilter(Entity.class));
    scanner.addIncludeFilter(new AnnotationTypeFilter(Embeddable.class));
    scanner.addIncludeFilter(new AnnotationTypeFilter(MappedSuperclass.class));
    for (String pkg : packagesToScan) {
        for (BeanDefinition beanDefinition : scanner.findCandidateComponents(pkg)) {
            metadataSources.addAnnotatedClassName(beanDefinition.getBeanClassName());
        }
    }

    StandardServiceRegistryBuilder standardServiceRegistryBuilder = new StandardServiceRegistryBuilder(
            bootstrapServiceRegistry);
    standardServiceRegistryBuilder.applySetting(AvailableSettings.DIALECT, dialect.getName());
    StandardServiceRegistryImpl ssr = (StandardServiceRegistryImpl) standardServiceRegistryBuilder.build();
    MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder(ssr);

    if (useUnderscore) {
        metadataBuilder.applyImplicitNamingStrategy(new SpringImplicitNamingStrategy());
        metadataBuilder.applyPhysicalNamingStrategy(new SpringPhysicalNamingStrategy());
    }

    new SchemaExport() //
            .setHaltOnError(true) //
            .setOutputFile(outputSqlFile) //
            .setFormat(true) //
            .setDelimiter(";") //
            .execute(EnumSet.of(TargetType.SCRIPT), SchemaExport.Action.CREATE, metadataBuilder.build());

}