Example usage for org.springframework.context.annotation ClassPathScanningCandidateComponentProvider setResourceLoader

List of usage examples for org.springframework.context.annotation ClassPathScanningCandidateComponentProvider setResourceLoader

Introduction

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

Prototype

@Override
public void setResourceLoader(@Nullable ResourceLoader resourceLoader) 

Source Link

Document

Set the ResourceLoader to use for resource locations.

Usage

From source file:org.springframework.data.repository.config.AbstractRepositoryConfigDefinitionParser.java

/**
 * Tries to detect a custom implementation for a repository bean by classpath scanning.
 * //  w w w . j a v  a2s.com
 * @param config
 * @param parser
 * @return the {@code AbstractBeanDefinition} of the custom implementation or {@literal null} if none found
 */
private AbstractBeanDefinition detectCustomImplementation(T config, ParserContext parser) {

    // Build pattern to lookup implementation class
    Pattern pattern = Pattern.compile(".*\\." + config.getImplementationClassName());

    // Build classpath scanner and lookup bean definition
    ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(
            false);
    provider.setResourceLoader(parser.getReaderContext().getResourceLoader());
    provider.addIncludeFilter(new RegexPatternTypeFilter(pattern));
    Set<BeanDefinition> definitions = provider.findCandidateComponents(config.getBasePackage());

    if (definitions.size() == 0) {
        return null;
    }

    if (definitions.size() == 1) {
        return (AbstractBeanDefinition) definitions.iterator().next();
    }

    List<String> implementationClassNames = new ArrayList<String>();
    for (BeanDefinition bean : definitions) {
        implementationClassNames.add(bean.getBeanClassName());
    }

    throw new IllegalStateException(String.format(
            "Ambiguous custom implementations detected! Found %s but expected a single implementation!",
            StringUtils.collectionToCommaDelimitedString(implementationClassNames)));
}

From source file:org.springframework.data.repository.config.RepositoryBeanDefinitionBuilder.java

/**
 * Tries to detect a custom implementation for a repository bean by classpath scanning.
 * /*from   www  . ja v  a 2  s  . co m*/
 * @param config
 * @param parser
 * @return the {@code AbstractBeanDefinition} of the custom implementation or {@literal null} if none found
 */
private AbstractBeanDefinition detectCustomImplementation(BeanDefinitionRegistry registry,
        ResourceLoader loader) {

    // Build pattern to lookup implementation class
    Pattern pattern = Pattern.compile(".*\\." + configuration.getImplementationClassName());

    // Build classpath scanner and lookup bean definition
    ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(
            false);
    provider.setResourceLoader(loader);
    provider.addIncludeFilter(new RegexPatternTypeFilter(pattern));

    Set<BeanDefinition> definitions = new HashSet<BeanDefinition>();

    for (String basePackage : configuration.getBasePackages()) {
        definitions.addAll(provider.findCandidateComponents(basePackage));
    }

    if (definitions.isEmpty()) {
        return null;
    }

    if (definitions.size() == 1) {
        return (AbstractBeanDefinition) definitions.iterator().next();
    }

    List<String> implementationClassNames = new ArrayList<String>();
    for (BeanDefinition bean : definitions) {
        implementationClassNames.add(bean.getBeanClassName());
    }

    throw new IllegalStateException(String.format(
            "Ambiguous custom implementations detected! Found %s but expected a single implementation!",
            StringUtils.collectionToCommaDelimitedString(implementationClassNames)));
}