Skip to content

Commit 40dc547

Browse files
ramansahasimaibin
authored andcommitted
BAEL-859 How to display/list all Spring-managed beans? (eugenp#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.
1 parent 5b785b3 commit 40dc547

7 files changed

Lines changed: 165 additions & 2 deletions

File tree

‎spring-boot/pom.xml‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,13 @@
4545
<artifactId>spring-boot-starter-tomcat</artifactId>
4646
<scope>provided</scope>
4747
</dependency>
48-
48+
49+
<dependency>
50+
<groupId>org.springframework.boot</groupId>
51+
<artifactId>spring-boot-starter-test</artifactId>
52+
<scope>test</scope>
53+
</dependency>
54+
4955
<dependency>
5056
<groupId>org.apache.tomcat.embed</groupId>
5157
<artifactId>tomcat-embed-core</artifactId>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.displayallbeans;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.context.ApplicationContext;
6+
7+
@SpringBootApplication
8+
public class Application {
9+
private static ApplicationContext applicationContext;
10+
11+
public static void main(String[] args) {
12+
applicationContext = SpringApplication.run(Application.class, args);
13+
14+
String[] allBeanNames = applicationContext.getBeanDefinitionNames();
15+
for(String beanName : allBeanNames) {
16+
System.out.println(beanName);
17+
}
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.displayallbeans;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
7+
8+
import com.baeldung.displayallbeans.model.Person;
9+
10+
@Configuration
11+
@ComponentScan(basePackages = "com.baeldung.displayallbeans")
12+
public class SpringConfig {
13+
@Bean
14+
public Person person() {
15+
Person person = new Person();
16+
person.setName("Jon Doe");
17+
return person;
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.displayallbeans.controller;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Controller;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.ResponseBody;
7+
8+
import com.baeldung.displayallbeans.model.Person;
9+
10+
@Controller
11+
public class PersonController {
12+
@Autowired
13+
Person person;
14+
15+
@RequestMapping("/getPerson")
16+
public @ResponseBody Person getPerson() {
17+
return person;
18+
}
19+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.displayallbeans.model;
2+
3+
public class Person {
4+
String name;
5+
6+
public String getName() {
7+
return name;
8+
}
9+
10+
public void setName(String name) {
11+
this.name = name;
12+
}
13+
}

‎spring-boot/src/main/resources/application.properties‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,7 @@ servlet.mapping=/dispatcherExampleURL
4545
#banner.image.margin= //TODO
4646
#banner.image.invert= //TODO
4747

48-
contactInfoType=email
48+
contactInfoType=email
49+
50+
endpoints.beans.id=springbeans
51+
endpoints.beans.sensitive=false
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.baeldung.displayallbeans;
2+
3+
import static org.assertj.core.api.BDDAssertions.then;
4+
import static org.hamcrest.CoreMatchers.hasItem;
5+
import static org.junit.Assert.assertThat;
6+
import static org.junit.Assert.assertTrue;
7+
8+
import java.util.Arrays;
9+
import java.util.List;
10+
import java.util.Map;
11+
import java.util.stream.Collectors;
12+
13+
import org.junit.Test;
14+
import org.junit.runner.RunWith;
15+
import org.springframework.beans.factory.annotation.Autowired;
16+
import org.springframework.beans.factory.annotation.Value;
17+
import org.springframework.boot.context.embedded.LocalServerPort;
18+
import org.springframework.boot.test.context.SpringBootTest;
19+
import org.springframework.boot.test.web.client.TestRestTemplate;
20+
import org.springframework.http.HttpStatus;
21+
import org.springframework.http.ResponseEntity;
22+
import org.springframework.test.context.TestPropertySource;
23+
import org.springframework.test.context.junit4.SpringRunner;
24+
import org.springframework.web.context.WebApplicationContext;
25+
26+
import com.baeldung.displayallbeans.Application;
27+
28+
@RunWith(SpringRunner.class)
29+
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
30+
@TestPropertySource(properties = {"management.port=0", "endpoints.beans.id=springbeans", "endpoints.beans.sensitive=false"})
31+
public class DisplayBeanIntegrationTest {
32+
33+
@LocalServerPort
34+
private int port;
35+
36+
@Value("${local.management.port}")
37+
private int mgt;
38+
39+
@Autowired
40+
private TestRestTemplate testRestTemplate;
41+
42+
@Autowired
43+
private WebApplicationContext context;
44+
45+
@Test
46+
public void givenRestTemplate_whenAccessServerUrl_thenHttpStatusOK() throws Exception {
47+
@SuppressWarnings("rawtypes")
48+
ResponseEntity<Map> entity = this.testRestTemplate.getForEntity(
49+
"http://localhost:" + this.port + "/getPerson", Map.class);
50+
51+
then(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
52+
}
53+
54+
@Test
55+
public void givenRestTemplate_whenAccessEndpointUrl_thenHttpStatusOK() throws Exception {
56+
@SuppressWarnings("rawtypes")
57+
ResponseEntity<List> entity = this.testRestTemplate.getForEntity(
58+
"http://localhost:" + this.mgt + "/springbeans", List.class);
59+
60+
then(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
61+
}
62+
63+
@Test
64+
public void givenRestTemplate_whenAccessEndpointUrl_thenReturnsBeanNames() throws Exception {
65+
@SuppressWarnings("rawtypes")
66+
ResponseEntity<List> entity = this.testRestTemplate.getForEntity(
67+
"http://localhost:" + this.mgt + "/springbeans", List.class);
68+
69+
List<Map<String, Object>> allBeans = (List) ((Map) entity.getBody().get(0)).get("beans");
70+
List<String> beanNamesList = allBeans.stream().map(x -> (String) x.get("bean")).collect(Collectors.toList());
71+
72+
assertThat( beanNamesList, hasItem("personController"));
73+
assertThat( beanNamesList, hasItem("person"));
74+
}
75+
76+
@Test
77+
public void givenWebApplicationContext_whenAccessGetBeanDefinitionNames_thenReturnsBeanNames() throws Exception {
78+
String[] beanNames = context.getBeanDefinitionNames();
79+
80+
List<String> beanNamesList = Arrays.asList(beanNames);
81+
assertTrue(beanNamesList.contains("personController"));
82+
assertTrue(beanNamesList.contains("person"));
83+
}
84+
}

0 commit comments

Comments
 (0)