SpringJavaConfigService { public <T> T getBeanDefinition(Class<?> configFile, Project project, String id, Class<T> type) { List<Class<?>> configFiles = new ArrayList<>(); configFiles.add(configFile); configFiles.addAll(getConfigImports(configFile, project)); final Method[] beanDefinitionMethod = new Method[1]; for (Class<?> config : configFiles) { ReflectionUtils.doWithMethods(config, method -> { if (method.getName().equals(id)) { beanDefinitionMethod[0] = method; } else { Bean beanAnnotation = method.getAnnotation(Bean.class); if (beanAnnotation.value().length > 0 && beanAnnotation.value()[0].equals(id)) { beanDefinitionMethod[0] = method; } if (beanAnnotation.name().length > 0 && beanAnnotation.name()[0].equals(id)) { beanDefinitionMethod[0] = method; } } }, method -> method.getAnnotation(Bean.class) != null); if (beanDefinitionMethod[0] != null) { try { Object bean = beanDefinitionMethod[0].invoke(config.newInstance()); if (bean.getClass().equals(type)) { return (T) bean; } else if (type.equals(SpringBean.class)) { SpringBeanModelConverter<Object> springBeanModelConverter = new SpringBeanModelConverter(bean.getClass()); return (T) springBeanModelConverter.convert(id, bean); } else { for (ModelConverter converter : modelConverter) { if (converter.getSourceModelClass().equals(bean.getClass()) && converter.getTargetModelClass().equals(type)) { return (T) converter.convert(id, bean); } } } } catch (InvocationTargetException | InstantiationException | IllegalAccessException e) { throw new ApplicationRuntimeException("Failed to access bean definition method in Java config", e); } } } return null; }  List<Class<?>> getConfigImports(Class<?> configFile, Project project); T getBeanDefinition(Class<?> configFile, Project project, String id, Class<T> type); List<T> getBeanDefinitions(Class<?> configFile, Project project, Class<T> type); List<String> getBeanNames(Class<?> configFile, Project project, Class<?> type); void addBeanDefinition(File configFile, Project project, Object model); void removeBeanDefinition(File configFile, Project project, String id); void removeBeanDefinitions(File configFile, Project project, Class<?> type); void updateBeanDefinition(File configFile, Project project, String id, Object model); void updateBeanDefinitions(File configFile, Project project, Class<?> type, Object model);  }
@Test public void testGetBeanDefinition() throws Exception { JmsEndpointModel endpoint = springJavaConfigService.getBeanDefinition(GetBeanDefinitionConfig.class, project, "jmsInboundEndpoint", JmsEndpointModel.class); Assert.assertEquals(endpoint.getId(), "jmsInboundEndpoint"); Assert.assertEquals(endpoint.getDestinationName(), "jms.inbound.queue"); endpoint = springJavaConfigService.getBeanDefinition(GetBeanDefinitionConfig.class, project, "jmsOutboundEndpoint", JmsEndpointModel.class); Assert.assertEquals(endpoint.getId(), "jmsOutboundEndpoint"); Assert.assertEquals(endpoint.getDestinationName(), "jms.outbound.queue"); HttpClientModel client = springJavaConfigService.getBeanDefinition(GetBeanDefinitionConfig.class, project, "httpClient", HttpClientModel.class); Assert.assertEquals(client.getId(), "httpClient"); Assert.assertEquals(client.getRequestUrl(), "http: String sampleBean = springJavaConfigService.getBeanDefinition(ImportConfig.class, project, "sampleBean", String.class); Assert.assertEquals(sampleBean, "bean"); }