t777
t777

Reputation: 3329

How to inject a custom BeanFactory

For a custom Scope (I cannot post here because of legal reasons) I need a custom BeanFactory that overrides the getBean(Class requiredType) method, like:

public class MyBeanFactory implements BeanFactory {

    @Override
    public <T> T getBean(Class<T> requiredType) throws BeansException {
        if(MyScope.someSetting) {
            return useBeanA();
        } else {
            return useBeanB();
        }
    }
}

This BeanFactory must be used in my whole spring container for getting beans (by type). How can I inject it in the ApplicationContext (or do I need a custom ApplicationContext)?

I cannot use a FactoryBean, because the logic is not special to one (or a few) bean(s). I cannot use a InstantiationAwareBeanPostProcessor because its postProcessBeforeInstantiation() method is called only onced.

Upvotes: 2

Views: 2095

Answers (1)

t777
t777

Reputation: 3329

As suggested by 'spacetrucker' (see the comments to the question), I solved the problem quite different. I added my bean instantiation / selection logic to my custom Scope and injected a BeanFactory by the constructor to this Scope.

Upvotes: 1

Related Questions