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.
This commit is contained in:
ramansahasi 2017-06-09 02:04:43 +05:30 committed by maibin
parent 5b785b3ad2
commit 40dc547558
7 changed files with 165 additions and 2 deletions

View File

@ -46,6 +46,12 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>

View File

@ -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);
}
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -46,3 +46,6 @@ servlet.mapping=/dispatcherExampleURL
#banner.image.invert= //TODO
contactInfoType=email
endpoints.beans.id=springbeans
endpoints.beans.sensitive=false

View File

@ -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<Map> 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<List> 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<List> entity = this.testRestTemplate.getForEntity(
"http://localhost:" + this.mgt + "/springbeans", List.class);
List<Map<String, Object>> allBeans = (List) ((Map) entity.getBody().get(0)).get("beans");
List<String> 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<String> beanNamesList = Arrays.asList(beanNames);
assertTrue(beanNamesList.contains("personController"));
assertTrue(beanNamesList.contains("person"));
}
}