### Question:
BeanFactoryUtils { public static <T> T getOptionalBean(ListableBeanFactory beanFactory, String beanName, Class<T> beanType) { String[] allBeanNames = beanNamesForTypeIncludingAncestors(beanFactory, beanType); if (!StringUtils.isContains(allBeanNames, beanName)) { return null; } Map<String, T> beansOfType = beansOfTypeIncludingAncestors(beanFactory, beanType); return beansOfType.get(beanName); }  static T getOptionalBean(ListableBeanFactory beanFactory, String beanName, Class<T> beanType); static List<T> getBeans(ListableBeanFactory beanFactory, String[] beanNames, Class<T> beanType); static boolean addApplicationListener(ApplicationContext applicationContext, ApplicationListener listener);  }### Answer:
@Test public void testGetOptionalBean() { applicationContext.register(TestBean.class); applicationContext.refresh(); TestBean testBean = BeanFactoryUtils.getOptionalBean(applicationContext, "testBean", TestBean.class); Assertions.assertNotNull(testBean); Assertions.assertEquals("Hello,World", testBean.getName()); }
@Test public void testGetOptionalBeanIfAbsent() { applicationContext.refresh(); TestBean testBean = BeanFactoryUtils.getOptionalBean(applicationContext, "testBean", TestBean.class); Assertions.assertNull(testBean); }