Search in sources :

Example 1 with CachingMetadataReaderFactory

use of org.springframework.core.type.classreading.CachingMetadataReaderFactory in project spring-framework by spring-projects.

the class ConfigurationClassPostProcessor method setResourceLoader.

@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
    Assert.notNull(resourceLoader, "ResourceLoader must not be null");
    this.resourceLoader = resourceLoader;
    if (!this.setMetadataReaderFactoryCalled) {
        this.metadataReaderFactory = new CachingMetadataReaderFactory(resourceLoader);
    }
}
Also used : CachingMetadataReaderFactory(org.springframework.core.type.classreading.CachingMetadataReaderFactory)

Example 2 with CachingMetadataReaderFactory

use of org.springframework.core.type.classreading.CachingMetadataReaderFactory in project spring-framework by spring-projects.

the class DefaultPersistenceUnitManager method scanPackage.

private void scanPackage(SpringPersistenceUnitInfo scannedUnit, String pkg) {
    try {
        String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(pkg) + CLASS_RESOURCE_PATTERN;
        Resource[] resources = this.resourcePatternResolver.getResources(pattern);
        MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(this.resourcePatternResolver);
        for (Resource resource : resources) {
            if (resource.isReadable()) {
                MetadataReader reader = readerFactory.getMetadataReader(resource);
                String className = reader.getClassMetadata().getClassName();
                if (matchesFilter(reader, readerFactory)) {
                    scannedUnit.addManagedClassName(className);
                    if (scannedUnit.getPersistenceUnitRootUrl() == null) {
                        URL url = resource.getURL();
                        if (ResourceUtils.isJarURL(url)) {
                            scannedUnit.setPersistenceUnitRootUrl(ResourceUtils.extractJarFileURL(url));
                        }
                    }
                } else if (className.endsWith(PACKAGE_INFO_SUFFIX)) {
                    scannedUnit.addManagedPackage(className.substring(0, className.length() - PACKAGE_INFO_SUFFIX.length()));
                }
            }
        }
    } catch (IOException ex) {
        throw new PersistenceException("Failed to scan classpath for unlisted entity classes", ex);
    }
}
Also used : CachingMetadataReaderFactory(org.springframework.core.type.classreading.CachingMetadataReaderFactory) MetadataReaderFactory(org.springframework.core.type.classreading.MetadataReaderFactory) Resource(org.springframework.core.io.Resource) PersistenceException(javax.persistence.PersistenceException) MetadataReader(org.springframework.core.type.classreading.MetadataReader) CachingMetadataReaderFactory(org.springframework.core.type.classreading.CachingMetadataReaderFactory) IOException(java.io.IOException) URL(java.net.URL)

Example 3 with CachingMetadataReaderFactory

use of org.springframework.core.type.classreading.CachingMetadataReaderFactory in project spring-boot by spring-projects.

the class CloudAutoConfigurationTests method testOrder.

@Test
public void testOrder() throws Exception {
    TestAutoConfigurationSorter sorter = new TestAutoConfigurationSorter(new CachingMetadataReaderFactory());
    Collection<String> classNames = new ArrayList<>();
    classNames.add(MongoAutoConfiguration.class.getName());
    classNames.add(DataSourceAutoConfiguration.class.getName());
    classNames.add(MongoRepositoriesAutoConfiguration.class.getName());
    classNames.add(JpaRepositoriesAutoConfiguration.class.getName());
    classNames.add(CloudAutoConfiguration.class.getName());
    List<String> ordered = sorter.getInPriorityOrder(classNames);
    assertThat(ordered.get(0)).isEqualTo(CloudAutoConfiguration.class.getName());
}
Also used : TestAutoConfigurationSorter(org.springframework.boot.autoconfigure.TestAutoConfigurationSorter) MongoRepositoriesAutoConfiguration(org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration) ArrayList(java.util.ArrayList) CachingMetadataReaderFactory(org.springframework.core.type.classreading.CachingMetadataReaderFactory) MongoAutoConfiguration(org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration) JpaRepositoriesAutoConfiguration(org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration) DataSourceAutoConfiguration(org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration) Test(org.junit.Test)

Example 4 with CachingMetadataReaderFactory

use of org.springframework.core.type.classreading.CachingMetadataReaderFactory in project spring-boot by spring-projects.

the class NoSuchBeanDefinitionFailureAnalyzer method setBeanFactory.

@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
    Assert.isInstanceOf(ConfigurableListableBeanFactory.class, beanFactory);
    this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
    this.metadataReaderFactory = new CachingMetadataReaderFactory(this.beanFactory.getBeanClassLoader());
    // Get early as won't be accessible once context has failed to start
    this.report = ConditionEvaluationReport.get(this.beanFactory);
}
Also used : CachingMetadataReaderFactory(org.springframework.core.type.classreading.CachingMetadataReaderFactory)

Example 5 with CachingMetadataReaderFactory

use of org.springframework.core.type.classreading.CachingMetadataReaderFactory in project spring-framework by spring-projects.

the class ClassPathJaxb2TypeScanner method scanPackages.

/**
	 * Scan the packages for classes marked with JAXB2 annotations.
	 * @throws UncategorizedMappingException in case of errors
	 */
public Class<?>[] scanPackages() throws UncategorizedMappingException {
    try {
        List<Class<?>> jaxb2Classes = new ArrayList<>();
        for (String packageToScan : this.packagesToScan) {
            String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(packageToScan) + RESOURCE_PATTERN;
            Resource[] resources = this.resourcePatternResolver.getResources(pattern);
            MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(this.resourcePatternResolver);
            for (Resource resource : resources) {
                MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource);
                if (isJaxb2Class(metadataReader, metadataReaderFactory)) {
                    String className = metadataReader.getClassMetadata().getClassName();
                    Class<?> jaxb2AnnotatedClass = this.resourcePatternResolver.getClassLoader().loadClass(className);
                    jaxb2Classes.add(jaxb2AnnotatedClass);
                }
            }
        }
        return jaxb2Classes.toArray(new Class<?>[jaxb2Classes.size()]);
    } catch (IOException ex) {
        throw new UncategorizedMappingException("Failed to scan classpath for unlisted classes", ex);
    } catch (ClassNotFoundException ex) {
        throw new UncategorizedMappingException("Failed to load annotated classes from classpath", ex);
    }
}
Also used : CachingMetadataReaderFactory(org.springframework.core.type.classreading.CachingMetadataReaderFactory) MetadataReaderFactory(org.springframework.core.type.classreading.MetadataReaderFactory) ArrayList(java.util.ArrayList) Resource(org.springframework.core.io.Resource) MetadataReader(org.springframework.core.type.classreading.MetadataReader) CachingMetadataReaderFactory(org.springframework.core.type.classreading.CachingMetadataReaderFactory) IOException(java.io.IOException) UncategorizedMappingException(org.springframework.oxm.UncategorizedMappingException)

Aggregations

CachingMetadataReaderFactory (org.springframework.core.type.classreading.CachingMetadataReaderFactory)8 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Resource (org.springframework.core.io.Resource)3 MetadataReader (org.springframework.core.type.classreading.MetadataReader)3 MetadataReaderFactory (org.springframework.core.type.classreading.MetadataReaderFactory)3 URL (java.net.URL)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 TreeSet (java.util.TreeSet)1 PersistenceException (javax.persistence.PersistenceException)1 MappingException (org.hibernate.MappingException)1 Test (org.junit.Test)1 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)1 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)1 SingletonBeanRegistry (org.springframework.beans.factory.config.SingletonBeanRegistry)1 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)1 BeanNameGenerator (org.springframework.beans.factory.support.BeanNameGenerator)1 TestAutoConfigurationSorter (org.springframework.boot.autoconfigure.TestAutoConfigurationSorter)1 JpaRepositoriesAutoConfiguration (org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration)1