Skip to content

Plain FactoryBean declaration on @Bean method leads to early call (pre injection) [SPR-12141] #16755

Closed
@spring-projects-issues

Description

@spring-projects-issues

Michał Jaśtak opened SPR-12141 and commented

Suppose that I have class annotated with @Configuration, in which I want to use Environment, injected by Spring Framework, something like this:

@Configuration
public class MyConfig {

@Inject
private Environment env;
...
}

When I declare bean using HttpInvokerProxyFactoryBean, and try to access injected environment to use one or the properties as for ex. service URL

@Bean
    public FactoryBean someService() {
        final HttpInvokerProxyFactoryBean factory = new HttpInvokerProxyFactoryBean();
        factory.setServiceInterface(SomeService.class);
        factory.setServiceUrl(env.getRequiredProperty("service.uri"));
        return factory;
    }

I get NPEx because Spring Framework is trying to instantiate the factory before Environment is injected.

The problem doesn't exist, if I declare returned type as FactoryBean<SomeService>, but because of current signature of HttpInvokerProxyFactoryBean, I'm unable to use it directly (it implements FactoryBean<Object>).


Affects: 3.2.10, 4.0.6, 4.1 RC2

Issue Links:

Referenced from: commits 4432c41, f4f7f40, 5da8a16, bff2bf2

Backported to: 4.0.7, 3.2.11

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