{"action":"opened","issue":{"url":"https://api.github.com/repos/spring-projects/spring-security/issues/5538","repository_url":"https://api.github.com/repos/spring-projects/spring-security","labels_url":"https://api.github.com/repos/spring-projects/spring-security/issues/5538/labels{/name}","comments_url":"https://api.github.com/repos/spring-projects/spring-security/issues/5538/comments","events_url":"https://api.github.com/repos/spring-projects/spring-security/issues/5538/events","html_url":"https://github.com/spring-projects/spring-security/issues/5538","id":342400824,"node_id":"MDU6SXNzdWUzNDI0MDA4MjQ=","number":5538,"title":"Favor ApplicationContext#getBean","user":{"login":"jzheaux","id":3627351,"node_id":"MDQ6VXNlcjM2MjczNTE=","avatar_url":"https://avatars0.githubusercontent.com/u/3627351?v=4","gravatar_id":"","url":"https://api.github.com/users/jzheaux","html_url":"https://github.com/jzheaux","followers_url":"https://api.github.com/users/jzheaux/followers","following_url":"https://api.github.com/users/jzheaux/following{/other_user}","gists_url":"https://api.github.com/users/jzheaux/gists{/gist_id}","starred_url":"https://api.github.com/users/jzheaux/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jzheaux/subscriptions","organizations_url":"https://api.github.com/users/jzheaux/orgs","repos_url":"https://api.github.com/users/jzheaux/repos","events_url":"https://api.github.com/users/jzheaux/events{/privacy}","received_events_url":"https://api.github.com/users/jzheaux/received_events","type":"User","site_admin":false},"labels":[{"id":555556091,"node_id":"MDU6TGFiZWw1NTU1NTYwOTE=","url":"https://api.github.com/repos/spring-projects/spring-security/labels/OAuth2","name":"OAuth2","color":"000000","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-07-18T16:22:17Z","updated_at":"2018-07-18T16:22:17Z","closed_at":null,"author_association":"CONTRIBUTOR","body":"A couple of the OAuth2 configurer classes use\r\n\r\n```java\r\nMap<String, T> : BeanFactoryUtils#beansOfTypeIncludingAncestors(ApplicationContext, Class<T>)\r\n```\r\nto retrieve the desired bean from the application context. In most cases, the code subsequently selects a single item from the map or errors if the map has more than one entry.\r\n\r\n`ApplicationContext#getBean(Class<T>)` may suffice for this behavior.\r\n\r\nTypically, `beansOfTypeIncludingAncestors` can be a way to get beans lazily, as `AuthenticationConfiguration` does. Or it is a way to fully customize the resolution logic when more than one of the same type is a legitimate use case.\r\n\r\nIf the OAuth2 configurer code wants to eagerly pull these values, then calling `getBean` may prove less code, especially in circumstances where the code is throwing an error when there are duplicate beans:\r\n\r\n```java\r\nMap<String, ClientRegistrationRepository> clientRegistrationRepositoryMap = BeanFactoryUtils.beansOfTypeIncludingAncestors(\r\n\t\tbuilder.getSharedObject(ApplicationContext.class), ClientRegistrationRepository.class);\r\nif (clientRegistrationRepositoryMap.isEmpty()) {\r\n\tthrow new NoSuchBeanDefinitionException(ClientRegistrationRepository.class);\r\n} else if (clientRegistrationRepositoryMap.size() > 1) {\r\n\tthrow new NoUniqueBeanDefinitionException(ClientRegistrationRepository.class, clientRegistrationRepositoryMap.size(),\r\n\t\t\t\"Expected single matching bean of type '\" + ClientRegistrationRepository.class.getName() + \"' but found \" +\r\n\t\t\t\t\tclientRegistrationRepositoryMap.size() + \": \" + StringUtils.collectionToCommaDelimitedString(clientRegistrationRepositoryMap.keySet()));\r\n}\r\nreturn clientRegistrationRepositoryMap.values().iterator().next();\r\n```\r\ncould be reduced to\r\n\r\n```java\r\nbuilder.getSharedObject(ApplicationContext.class)\r\n\t\t.getBean(ClientRegistrationRepository.class);\r\n```"}}
{"actor":{"display_login":"jzheaux"}}