Spring - 在未找到bean时注入null而不是抛出异常

时间:2014-03-07 19:19:05

标签: java spring

我在课堂上注射了

@Inject IMyInterface controller;

但是没有实现IMyInterface的类。如何使spring注入null而不是抛出NoSuchBeanDefinitionException,因为我不一定需要它存在?

1 个答案:

答案 0 :(得分:4)

使用@Inject(假设它是javax.inject.Inject),我认为没有直接的方法可以使绑定成为可选的。但是,如果您使用@Autowired,则可以将其required属性设置为false

@Autowired(required = false)
IMyInterface controller;
相关问题