Skip to content

ConfigurationClassParser needs to load annotations through source class loader [SPR-10343] #14977

Closed
@spring-projects-issues

Description

@spring-projects-issues

Olaf Otto opened SPR-10343 and commented

A change from 3.1.3.RELEASE to 3.1.4.RELEASE in org.springframework.context.annotation.ConfigurationClassParser causes the getImport method to recursively load all annotations of a class(looking for @Import), using a metadataReaderFactory solely relying on the classloader of the annotated type. Resulting, the method fails in environments with classloader isolation (i.e. OSGi):

// the package containing @MyAnnotation is explicitly
// imported in the bundle containing AnnotatedType
@MyAnnotation 
public class AnnotatedType {
}
// This meta annotation stems from the spring bundle, i.e. is a transitive 
// import from @MyAnnotation and not explicitly imported into the bundle containing "AnnotatedType"
@Scope("prototype") 
public @interface MyAnnotation {
}

Resulting exception as the SimpleMetadataReader tries to obtain @Scope from the classloader containing AnnotatedType:

-- snipp --
Caused by: java.io.FileNotFoundException: class path resource [org/springframework/context/annotation/Scope.class] cannot be opened because it does not exist
	at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157)
	at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:49)
	at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:80)
	at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:101)
	at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:76)
	at org.springframework.context.annotation.ConfigurationClassParser.getImports(ConfigurationClassParser.java:291)
	at org.springframework.context.annotation.ConfigurationClassParser.getImports(ConfigurationClassParser.java:293)
	at org.springframework.context.annotation.ConfigurationClassParser.getImports(ConfigurationClassParser.java:293)
	at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:223)
	at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:147)
	at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:124)
	at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:289)
	... 12 more

The 3.1.3 implementation relied on the annotation metadata of the annotation directly applied to the processed type - perhaps it is possible to reintroduce such an approach in favor of the recursion in getImports.


Affects: 3.1.4, 4.2.5, 4.3.1

Attachments:

Issue Links:

Backported to: 4.2.8

0 votes, 6 watchers

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)status: backportedAn issue that has been backported to maintenance branchestype: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions