How to disable Spring autowiring for a certain bean?

前端 未结 4 1047
天命终不由人
天命终不由人 2020-12-15 22:39

There are some class in jar (external library), that uses Spring internally. So library class has structure like a:

         


        
4条回答
  •  被撕碎了的回忆
    2020-12-15 23:23

    Based on @Juan's answer, created a helper to wrap a bean not to be autowired:

    public static  FactoryBean preventAutowire(T bean) {
        return new FactoryBean() {
            public T getObject() throws Exception {
                return bean;
            }
    
            public Class getObjectType() {
                return bean.getClass();
            }
    
            public boolean isSingleton() {
                return true;
            }
        };
    }
    
    ...
    
    @Bean
    static FactoryBean myBean() {
        return preventAutowire(new MyBean());
    }
    

提交回复
热议问题