{"id":4464,"date":"2021-07-10T03:06:21","date_gmt":"2021-07-10T03:06:21","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=4464"},"modified":"2021-07-10T04:04:14","modified_gmt":"2021-07-10T04:04:14","slug":"how-to-access-beans-in-application-context","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/how-to-access-beans-in-application-context\/","title":{"rendered":"How to access Beans in application context"},"content":{"rendered":"<p>}   Below is a static util class that can be used to check the list of beans available in the application context<\/p>\n<p><strong>ExampleConfigurationTest.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.org.utils;\r\n\r\nimport org.springframework.beans.BeansException;\r\nimport org.springframework.context.ApplicationContext;\r\nimport org.springframework.context.ApplicationContextAware;\r\nimport org.springframework.stereotype.Service;\r\n\r\n@Service\r\npublic class SpringUtils implements ApplicationContextAware {\r\n    private static ApplicationContext ctx;\r\n\r\n    @Override\r\n    public void setApplicationContext(ApplicationContext appContext)\r\n            throws BeansException {\r\n        ctx = appContext;\r\n    }\r\n\r\n    public static ApplicationContext getApplicationContext() {\r\n        return ctx;\r\n    }\r\n}\r\n<\/pre>\n<p><strong>SomeRandomClass.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nclass SomeRandomClass\r\n{\r\n    @Autowired\r\n    SpringUtils springUtils;\r\n\r\n    \r\n    public void getBeans() \r\n    {  \r\n        String&#x5B;] beans = springUtils.getApplicationContext().getBeanDefinitionNames();\r\n        Arrays.sort(beans);\r\n        for (String bean : beans)\r\n        {\r\n            System.out.println(bean + &quot; of Type :: &quot; + springUtils.getApplicationContext().getBean(bean).getClass());\r\n        }\r\n}\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nDBConfig of Type :: class com.mugil.org.configs.DBConfig$$EnhancerBySpringCGLIB$$d32ee4ac\r\ndbProperties of Type :: class com.mugil.org.configs.DBProperties\r\nspring.datasource-com.mugil.org.configs.DBProperties of Type :: class com.mugil.org.configs.DBProperties\r\nspringUtils of Type :: class com.mugil.org.utils.SpringUtils\r\n<\/pre>\n<p>In case if you don&#8217;t want to create a separate class and wanted to access beans in the context you can implement the ApplicationContextAware interface and specify the for loop in overridden setApplicationContext method.<\/p>\n<p><strong>How to load a single class into ApplicationContext<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n.\r\n.\r\nApplicationContextRunner context = new ApplicationContextRunner().withUserConfiguration(ExampleConfiguration.class);\r\n.\r\n.\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>} Below is a static util class that can be used to check the list of beans available in the application context ExampleConfigurationTest.java package com.mugil.org.utils; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Service; @Service public class SpringUtils implements ApplicationContextAware { private static ApplicationContext ctx; @Override public void setApplicationContext(ApplicationContext appContext) throws BeansException { ctx = appContext;&hellip; <a href=\"https:\/\/codethataint.com\/blog\/how-to-access-beans-in-application-context\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[336],"tags":[],"class_list":["post-4464","post","type-post","status-publish","format-standard","hentry","category-integration-testing"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4464","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=4464"}],"version-history":[{"count":5,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4464\/revisions"}],"predecessor-version":[{"id":4470,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4464\/revisions\/4470"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=4464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=4464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=4464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}