Skip to content

Dependency injection not supported on @FeatureConfiguration classes [SPR-7975] #12630

@spring-projects-issues

Description

@spring-projects-issues

Keith Donald opened SPR-7975 and commented

I'm unable to inject environment properties or ApplicationContext services such as ResourceLoader into @FeatureConfiguration classes. This appears possible with @Configuration classes but not @FeatureConfiguration classes.

An example of a @FeatureConfiguration that fails:

@FeatureConfiguration
public class MvcFeatureConfig {
	
    @Inject
    private ResourceLoader resourceLoader;
	
    @Feature
    public MvcAnnotationDriven annotatedControllers(Validator validator,
        @Value("#{environment['facebook.appId']}") String facebookAppId,
        @Value("#{environment['facebook.appSecret']}") String facebookAppSecret) {
        
        return new MvcAnnotationDriven().validator(validator).
            argumentResolvers(new DeviceWebArgumentResolver(),
            new FacebookWebArgumentResolver(facebookAppId, facebookAppSecret),
            new AccountWebArgumentResolver(),
            new DateTimeZoneWebArgumentResolver(),
            new LocationWebArgumentResolver());
    }
	
    @Feature
    public MvcResources resourceHandler() {
        return new MvcResources("/resources/**", resourceLoader.getResource("/resources/"));
    }
}

This will first fail because Spring tries to subclass the String environment @Values facebookAppId and facebookAppSecret, instead of injecting them. Changing those to member fields doesn't work either, as no injection gets performed on the @Value fields. The ResourceLoader field also remains null, resulting in a NullPointerException when resourceHandler() is invoked.


Affects: 3.1 M1

Attachments:

Issue Links:

Referenced from: commits 704b229, 5cfbed8

1 votes, 2 watchers

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions