Intentionally setting a Spring bean to null

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

    For anyone coming to this question, keep in mind that simply setting the @Autowired annotation as optional will do the trick (i.e. Spring will leave the reference null if no qualifying bean is found).

    @Autowired(required = false)
    private SomeClass someBean
    

    Note that you would have to do this everywhere the bean is referenced, which may be a bigger hassle than creating a null-factory as mentioned above.

提交回复
热议问题