From 40dc54755872275d69e66b0b0c8454f9e24908a4 Mon Sep 17 00:00:00 2001 From: ramansahasi Date: Fri, 9 Jun 2017 02:04:43 +0530 Subject: [PATCH] BAEL-859 How to display/list all Spring-managed beans? (#2021) * Display all beans in Spring Container * Display all spring managed beans Add code to configure 'beans' endpoint. * Added 'spring-boot-starter-test' dependency Added 'spring-boot-starter-test' dependency with scope 'test' * Display all spring managed beans Test cases Added test cases for 'Display all spring managed beans' module. --- spring-boot/pom.xml | 8 +- .../baeldung/displayallbeans/Application.java | 19 +++++ .../displayallbeans/SpringConfig.java | 19 +++++ .../controller/PersonController.java | 19 +++++ .../displayallbeans/model/Person.java | 13 +++ .../src/main/resources/application.properties | 5 +- .../DisplayBeanIntegrationTest.java | 84 +++++++++++++++++++ 7 files changed, 165 insertions(+), 2 deletions(-) create mode 100644 spring-boot/src/main/java/com/baeldung/displayallbeans/Application.java create mode 100644 spring-boot/src/main/java/com/baeldung/displayallbeans/SpringConfig.java create mode 100644 spring-boot/src/main/java/com/baeldung/displayallbeans/controller/PersonController.java create mode 100644 spring-boot/src/main/java/com/baeldung/displayallbeans/model/Person.java create mode 100644 spring-boot/src/test/java/com/baeldung/displayallbeans/DisplayBeanIntegrationTest.java diff --git a/spring-boot/pom.xml b/spring-boot/pom.xml index 691137e495..2a1de00039 100644 --- a/spring-boot/pom.xml +++ b/spring-boot/pom.xml @@ -45,7 +45,13 @@ spring-boot-starter-tomcat provided - + + + org.springframework.boot + spring-boot-starter-test + test + + org.apache.tomcat.embed tomcat-embed-core diff --git a/spring-boot/src/main/java/com/baeldung/displayallbeans/Application.java b/spring-boot/src/main/java/com/baeldung/displayallbeans/Application.java new file mode 100644 index 0000000000..fcce2725e5 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/displayallbeans/Application.java @@ -0,0 +1,19 @@ +package com.baeldung.displayallbeans; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ApplicationContext; + +@SpringBootApplication +public class Application { + private static ApplicationContext applicationContext; + + public static void main(String[] args) { + applicationContext = SpringApplication.run(Application.class, args); + + String[] allBeanNames = applicationContext.getBeanDefinitionNames(); + for(String beanName : allBeanNames) { + System.out.println(beanName); + } + } +} diff --git a/spring-boot/src/main/java/com/baeldung/displayallbeans/SpringConfig.java b/spring-boot/src/main/java/com/baeldung/displayallbeans/SpringConfig.java new file mode 100644 index 0000000000..1d4a97f843 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/displayallbeans/SpringConfig.java @@ -0,0 +1,19 @@ +package com.baeldung.displayallbeans; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; + +import com.baeldung.displayallbeans.model.Person; + +@Configuration +@ComponentScan(basePackages = "com.baeldung.displayallbeans") +public class SpringConfig { + @Bean + public Person person() { + Person person = new Person(); + person.setName("Jon Doe"); + return person; + } +} \ No newline at end of file diff --git a/spring-boot/src/main/java/com/baeldung/displayallbeans/controller/PersonController.java b/spring-boot/src/main/java/com/baeldung/displayallbeans/controller/PersonController.java new file mode 100644 index 0000000000..93fc278b1c --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/displayallbeans/controller/PersonController.java @@ -0,0 +1,19 @@ +package com.baeldung.displayallbeans.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +import com.baeldung.displayallbeans.model.Person; + +@Controller +public class PersonController { + @Autowired + Person person; + + @RequestMapping("/getPerson") + public @ResponseBody Person getPerson() { + return person; + } +} \ No newline at end of file diff --git a/spring-boot/src/main/java/com/baeldung/displayallbeans/model/Person.java b/spring-boot/src/main/java/com/baeldung/displayallbeans/model/Person.java new file mode 100644 index 0000000000..dde471aedb --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/displayallbeans/model/Person.java @@ -0,0 +1,13 @@ +package com.baeldung.displayallbeans.model; + +public class Person { + String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} \ No newline at end of file diff --git a/spring-boot/src/main/resources/application.properties b/spring-boot/src/main/resources/application.properties index 444f68d50a..458b4e0d46 100644 --- a/spring-boot/src/main/resources/application.properties +++ b/spring-boot/src/main/resources/application.properties @@ -45,4 +45,7 @@ servlet.mapping=/dispatcherExampleURL #banner.image.margin= //TODO #banner.image.invert= //TODO -contactInfoType=email \ No newline at end of file +contactInfoType=email + +endpoints.beans.id=springbeans +endpoints.beans.sensitive=false diff --git a/spring-boot/src/test/java/com/baeldung/displayallbeans/DisplayBeanIntegrationTest.java b/spring-boot/src/test/java/com/baeldung/displayallbeans/DisplayBeanIntegrationTest.java new file mode 100644 index 0000000000..84d5669aef --- /dev/null +++ b/spring-boot/src/test/java/com/baeldung/displayallbeans/DisplayBeanIntegrationTest.java @@ -0,0 +1,84 @@ +package com.baeldung.displayallbeans; + +import static org.assertj.core.api.BDDAssertions.then; +import static org.hamcrest.CoreMatchers.hasItem; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.web.context.WebApplicationContext; + +import com.baeldung.displayallbeans.Application; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@TestPropertySource(properties = {"management.port=0", "endpoints.beans.id=springbeans", "endpoints.beans.sensitive=false"}) +public class DisplayBeanIntegrationTest { + + @LocalServerPort + private int port; + + @Value("${local.management.port}") + private int mgt; + + @Autowired + private TestRestTemplate testRestTemplate; + + @Autowired + private WebApplicationContext context; + + @Test + public void givenRestTemplate_whenAccessServerUrl_thenHttpStatusOK() throws Exception { + @SuppressWarnings("rawtypes") + ResponseEntity entity = this.testRestTemplate.getForEntity( + "http://localhost:" + this.port + "/getPerson", Map.class); + + then(entity.getStatusCode()).isEqualTo(HttpStatus.OK); + } + + @Test + public void givenRestTemplate_whenAccessEndpointUrl_thenHttpStatusOK() throws Exception { + @SuppressWarnings("rawtypes") + ResponseEntity entity = this.testRestTemplate.getForEntity( + "http://localhost:" + this.mgt + "/springbeans", List.class); + + then(entity.getStatusCode()).isEqualTo(HttpStatus.OK); + } + + @Test + public void givenRestTemplate_whenAccessEndpointUrl_thenReturnsBeanNames() throws Exception { + @SuppressWarnings("rawtypes") + ResponseEntity entity = this.testRestTemplate.getForEntity( + "http://localhost:" + this.mgt + "/springbeans", List.class); + + List> allBeans = (List) ((Map) entity.getBody().get(0)).get("beans"); + List beanNamesList = allBeans.stream().map(x -> (String) x.get("bean")).collect(Collectors.toList()); + + assertThat( beanNamesList, hasItem("personController")); + assertThat( beanNamesList, hasItem("person")); + } + + @Test + public void givenWebApplicationContext_whenAccessGetBeanDefinitionNames_thenReturnsBeanNames() throws Exception { + String[] beanNames = context.getBeanDefinitionNames(); + + List beanNamesList = Arrays.asList(beanNames); + assertTrue(beanNamesList.contains("personController")); + assertTrue(beanNamesList.contains("person")); + } +}