Intentionally setting a Spring bean to null

后端 未结 7 1952
孤独总比滥情好
孤独总比滥情好 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:17

    In tests null beans can also be injected like this:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = NullTest.class)
    @Configuration
    public class NullTest {
    
        @Bean(name = "em")
        public IEntityManager em() { return null; }
        @Bean
        public PlatformTransactionManager tm() { return null; }
    
        @Resource
        private SomeBean someBean; // this would get em and tm fields autowired to nulls
    

提交回复
热议问题