Skip to content

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

Closed

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:

Activity

  1. spring-projects-issues commented on Dec 6, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    re-introduce formatting accidentally stripped out earlier.

  2. added
    status: declined
    type: enhancement
    in: core
    on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: corestatus: declinedtype: enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions