Intentionally setting a Spring bean to null

后端 未结 7 1932
孤独总比滥情好
孤独总比滥情好 2020-12-05 06:32

I\'m using Spring to inject JMS connection factory into my Java application. Since this factory is only required within the production environment, not while I\'m developing

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-05 07:36

    factory-bean/factory-method doesn't work with null, but a custom FactoryBean implementation works fine:

    public class NullFactoryBean implements FactoryBean {
    
        public Void getObject() throws Exception {
            return null;
        }
    
        public Class getObjectType() {
            return null;
        }
    
        public boolean isSingleton() {
            return true;
        }
    }
    
    
    

提交回复
热议问题