Example usage for org.springframework.context.annotation ClassPathBeanDefinitionScanner ClassPathBeanDefinitionScanner

List of usage examples for org.springframework.context.annotation ClassPathBeanDefinitionScanner ClassPathBeanDefinitionScanner

Introduction

In this page you can find the example usage for org.springframework.context.annotation ClassPathBeanDefinitionScanner ClassPathBeanDefinitionScanner.

Prototype

public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry) 

Source Link

Document

Create a new ClassPathBeanDefinitionScanner for the given bean factory.

Usage

From source file:org.opentides.bean.factory.support.BaseEntityRegistry.java

@Override
public void afterPropertiesSet() throws Exception {
    if (packagesToScan == null)
        packagesToScan = new ArrayList<String>();
    packagesToScan.add("org.opentides.bean");
    baseEntities = new ArrayList<String>();
    BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
    ClassPathBeanDefinitionScanner s = new ClassPathBeanDefinitionScanner(registry);
    TypeFilter tf = new AssignableTypeFilter(BaseEntity.class);
    s.resetFilters(false);//from w w  w . j a v a2 s.  com
    s.addIncludeFilter(tf);
    s.scan(packagesToScan.toArray(new String[packagesToScan.size()]));
    for (String name : registry.getBeanDefinitionNames()) {
        Class<?> clazz = Class.forName(registry.getBeanDefinition(name).getBeanClassName());
        if (BaseEntity.class.isAssignableFrom(clazz))
            baseEntities.add(registry.getBeanDefinition(name).getBeanClassName());
    }
}

From source file:org.elasticsoftware.elasticactors.spring.AnnotationConfigApplicationContext.java

/**
 * Create a new AnnotationConfigApplicationContext that needs to be populated
 * through {@link #register} calls and then manually {@linkplain #refresh refreshed}.
 *//*from  w  w  w. ja  v a  2 s  .  c  o m*/
public AnnotationConfigApplicationContext() {
    this.reader = new AnnotatedBeanDefinitionReader(this);
    this.scanner = new ClassPathBeanDefinitionScanner(this);
}

From source file:ru.anr.cmdline.base.BootstrapImpl.java

/**
 * Bean initialization. When starting, it tries to find out ant additional
 * plugins, which can be provided in a spring context.
 *//*  w ww. ja v a 2 s . c  o  m*/
@Override
@PostConstruct
public void init() {

    createAndRegisterBeanDefinition(context, shellClass, "shell");
    context.getBeanFactory().registerSingleton("commandLine", commandLine);

    // built-in commands and converters
    ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(
            (BeanDefinitionRegistry) context);
    scanner.scan("org.springframework.shell.converters", "org.springframework.shell.plugin.support");

    getJLineShellComponent().setHistorySize(Integer.MAX_VALUE);
    getJLineShellComponent().setDevelopmentMode(!isProdMode());
}

From source file:org.wso2.msf4j.spring.MSF4JSpringApplication.java

/**
 * This will add a given service class to the running instnace with given base path.
 *
 * @param configurableApplicationContext ConfigurableApplicationContext of running app
 * @param serviceClass Service class//w w w .ja v a2  s  .c  o m
 * @param basePath Base path teh servuce get registered
 */
public void addService(ConfigurableApplicationContext configurableApplicationContext, Class serviceClass,
        String basePath) {
    ClassPathBeanDefinitionScanner classPathBeanDefinitionScanner = new ClassPathBeanDefinitionScanner(
            (BeanDefinitionRegistry) configurableApplicationContext);
    classPathBeanDefinitionScanner.scan(serviceClass.getPackage().getName());
    SpringMicroservicesRunner springMicroservicesRunner = configurableApplicationContext
            .getBean(SpringMicroservicesRunner.class);
    springMicroservicesRunner.deploy(basePath, configurableApplicationContext.getBean(serviceClass));
}

From source file:org.elasticsoftware.elasticactors.spring.AnnotationConfigWebApplicationContext.java

/**
 * Register a {@link org.springframework.beans.factory.config.BeanDefinition} for
 * any classes specified by {@link #register(Class...)} and scan any packages
 * specified by {@link #scan(String...)}.
 * <p>For any values specified by {@link #setConfigLocation(String)} or
 * {@link #setConfigLocations(String[])}, attempt first to load each location as a
 * class, registering a {@code BeanDefinition} if class loading is successful,
 * and if class loading fails (i.e. a {@code ClassNotFoundException} is raised),
 * assume the value is a package and attempt to scan it for annotated classes.
 * <p>Enables the default set of annotation configuration post processors, such that
 * {@code @Autowired}, {@code @Required}, and associated annotations can be used.
 * <p>Configuration class bean definitions are registered with generated bean
 * definition names unless the {@code value} attribute is provided to the stereotype
 * annotation.// ww w  .  j a  va  2s  .co m
 * @see #register(Class...)
 * @see #scan(String...)
 * @see #setConfigLocation(String)
 * @see #setConfigLocations(String[])
 * @see AnnotatedBeanDefinitionReader
 * @see ClassPathBeanDefinitionScanner
 */
@Override
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) {
    AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(beanFactory);
    reader.setEnvironment(this.getEnvironment());

    ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(beanFactory);
    scanner.setEnvironment(this.getEnvironment());

    BeanNameGenerator beanNameGenerator = getBeanNameGenerator();
    ScopeMetadataResolver scopeMetadataResolver = getScopeMetadataResolver();
    if (beanNameGenerator != null) {
        reader.setBeanNameGenerator(beanNameGenerator);
        scanner.setBeanNameGenerator(beanNameGenerator);
        beanFactory.registerSingleton(AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR,
                beanNameGenerator);
    }
    if (scopeMetadataResolver != null) {
        reader.setScopeMetadataResolver(scopeMetadataResolver);
        scanner.setScopeMetadataResolver(scopeMetadataResolver);
    }

    if (!this.annotatedClasses.isEmpty()) {
        if (logger.isInfoEnabled()) {
            logger.info("Registering annotated classes: ["
                    + StringUtils.collectionToCommaDelimitedString(this.annotatedClasses) + "]");
        }
        reader.register(this.annotatedClasses.toArray(new Class<?>[this.annotatedClasses.size()]));
    }

    if (!this.basePackages.isEmpty()) {
        if (logger.isInfoEnabled()) {
            logger.info("Scanning base packages: ["
                    + StringUtils.collectionToCommaDelimitedString(this.basePackages) + "]");
        }
        for (TypeFilter typeFilter : includeFilters) {
            scanner.addIncludeFilter(typeFilter);
        }
        scanner.scan(this.basePackages.toArray(new String[this.basePackages.size()]));
    }

    String[] configLocations = getConfigLocations();
    if (configLocations != null) {
        for (String configLocation : configLocations) {
            try {
                Class<?> clazz = getClassLoader().loadClass(configLocation);
                if (logger.isInfoEnabled()) {
                    logger.info("Successfully resolved class for [" + configLocation + "]");
                }
                reader.register(clazz);
            } catch (ClassNotFoundException ex) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Could not load class for config location [" + configLocation
                            + "] - trying package scan. " + ex);
                }
                int count = scanner.scan(configLocation);
                if (logger.isInfoEnabled()) {
                    if (count == 0) {
                        logger.info("No annotated classes found for specified class/package [" + configLocation
                                + "]");
                    } else {
                        logger.info(
                                "Found " + count + " annotated classes in package [" + configLocation + "]");
                    }
                }
            }
        }
    }
}

From source file:com.fitbur.testify.di.spring.SpringServiceLocator.java

@Override
public void scanPackages(String... packages) {
    BeanDefinitionRegistry registry = (BeanDefinitionRegistry) context;

    new ClassPathBeanDefinitionScanner(registry).scan(packages);
}

From source file:org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext.java

/**
 * Create a new {@link AnnotationConfigServletWebServerApplicationContext} that needs
 * to be populated through {@link #register} calls and then manually
 * {@linkplain #refresh refreshed}.// ww  w. j  a va  2s.com
 */
public AnnotationConfigServletWebServerApplicationContext() {
    this.reader = new AnnotatedBeanDefinitionReader(this);
    this.scanner = new ClassPathBeanDefinitionScanner(this);
}

From source file:org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext.java

/**
 * Create a new {@link AnnotationConfigServletWebServerApplicationContext} with the
 * given {@code DefaultListableBeanFactory}. The context needs to be populated through
 * {@link #register} calls and then manually {@linkplain #refresh refreshed}.
 * @param beanFactory the DefaultListableBeanFactory instance to use for this context
 *//*w  w w . j  a va  2s  . c o  m*/
public AnnotationConfigServletWebServerApplicationContext(DefaultListableBeanFactory beanFactory) {
    super(beanFactory);
    this.reader = new AnnotatedBeanDefinitionReader(this);
    this.scanner = new ClassPathBeanDefinitionScanner(this);
}