Skip to content

Relative imports in @Configuration classes [SPR-8356] #13003

Closed
@spring-projects-issues

Description

@spring-projects-issues

Keith Donald opened SPR-8356 and commented

The following @Configuration class declared in package com.springsource.greenhouse.config, which uses imports, could be simplified from:

@Configuration
@ComponentScan("com.springsource.greenhouse")
@Import({ComponentConfig.Embedded.class, ComponentConfig.Standard.class})
public class ComponentConfig {
	
	/**
	 * Properties to support the 'embedded' mode of operation.
	 */
	@Configuration
	@Profile("embedded")
	@PropertySource("classpath:com/springsource/greenhouse/config/embedded.properties")
        static class Embedded {
	}

	/**
	 * Properties to support the 'standard' mode of operation.
	 */
	@Configuration
	@Profile("standard")
	@PropertySource("classpath:application.properties")
        static class Standard {
	}
}

to something like:

@Configuration
@ComponentScan("${topLevelPackage}")
@Import({ComponentConfig.Embedded.class, ComponentConfig.Standard.class})
public class ComponentConfig {
	
	/**
	 * Properties to support the 'embedded' mode of operation.
	 */
	@Configuration
	@Profile("embedded")
	@PropertySource("embedded.properties") <!-- Class-relative import- ->
        static class Embedded {
	}

	/**
	 * Properties to support the 'standard' mode of operation.
	 */
	@Configuration
	@Profile("standard")
	@PropertySource("/application.properties") <!-- Absolute class-path path -->
        static class Standard {
	}

}

This would make things more concise, future-proof to refactoring changes, and consistent with how beans:import tag works (imported resources there are relative to the location of the enclosing resource).

Also, it seems imports are now webapp-root relative for web applications. I'm not so sure this is desirable, since these are @Configuration classes in the classpath. I would expect imports to always be classpath resources unless an alternative resource loading scheme was used.


Issue Links:

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)status: declinedA suggestion or change that we don't feel we should currently applytype: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions