Skip to content

Commit 69b4a05

Browse files
ramansahasimaibin
authored andcommitted
BAEL-859 (updated) How to display/list all Spring-managed beans? (eugenp#2064)
* Separating displayAllBeans logic from main method * Removing as 'Person' bean is no longer used * Removing as 'Person' bean is no longer used * Removing as 'Person' bean is no longer used * Added FooService and FooController * Changed test cases as per new configuration * Template to handle front-end view * Fixed spacing issue * Fixed spaces * Fixed spacing issue
1 parent 74aa2c9 commit 69b4a05

8 files changed

Lines changed: 60 additions & 58 deletions

File tree

‎spring-boot/src/main/java/com/baeldung/displayallbeans/Application.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ public class Application {
1111
public static void main(String[] args) {
1212
applicationContext = SpringApplication.run(Application.class, args);
1313

14+
displayAllBeans();
15+
}
16+
17+
public static void displayAllBeans() {
1418
String[] allBeanNames = applicationContext.getBeanDefinitionNames();
1519
for(String beanName : allBeanNames) {
1620
System.out.println(beanName);

‎spring-boot/src/main/java/com/baeldung/displayallbeans/SpringConfig.java‎

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.displayallbeans.controller;
2+
3+
import java.util.Map;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Controller;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
9+
import com.baeldung.displayallbeans.service.FooService;
10+
11+
@Controller
12+
public class FooController {
13+
@Autowired
14+
private FooService fooService;
15+
16+
@RequestMapping(value="/displayallbeans")
17+
public String getHeaderAndBody (Map<String, Object> model){
18+
model.put("header", fooService.getHeader());
19+
model.put("message", fooService.getBody());
20+
return "displayallbeans";
21+
}
22+
}

‎spring-boot/src/main/java/com/baeldung/displayallbeans/controller/PersonController.java‎

Lines changed: 0 additions & 19 deletions
This file was deleted.

‎spring-boot/src/main/java/com/baeldung/displayallbeans/model/Person.java‎

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.displayallbeans.service;
2+
3+
import org.springframework.stereotype.Service;
4+
5+
@Service
6+
public class FooService {
7+
8+
public String getHeader() {
9+
return "Display All Beans";
10+
}
11+
12+
public String getBody() {
13+
return "This is a sample application that displays all beans "
14+
+ "in Spring IoC container using ListableBeanFactory interface "
15+
+ "and Spring Boot Actuators.";
16+
}
17+
18+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<html>
2+
<head>
3+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
4+
<title>Baeldung</title>
5+
</head>
6+
<body>
7+
<h2 th:text="${header}"></h2>
8+
<p th:text="${message}"></p>
9+
</body>
10+
</html>

‎spring-boot/src/test/java/com/baeldung/displayallbeans/DisplayBeanIntegrationTest.java‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@ public class DisplayBeanIntegrationTest {
4444

4545
@Test
4646
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);
47+
ResponseEntity<String> entity = this.testRestTemplate.getForEntity(
48+
"http://localhost:" + this.port + "/displayallbeans", String.class);
5049

5150
then(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
5251
}
@@ -69,16 +68,16 @@ public void givenRestTemplate_whenAccessEndpointUrl_thenReturnsBeanNames() throw
6968
List<Map<String, Object>> allBeans = (List) ((Map) entity.getBody().get(0)).get("beans");
7069
List<String> beanNamesList = allBeans.stream().map(x -> (String) x.get("bean")).collect(Collectors.toList());
7170

72-
assertThat( beanNamesList, hasItem("personController"));
73-
assertThat( beanNamesList, hasItem("person"));
71+
assertThat( beanNamesList, hasItem("fooController"));
72+
assertThat( beanNamesList, hasItem("fooService"));
7473
}
7574

7675
@Test
7776
public void givenWebApplicationContext_whenAccessGetBeanDefinitionNames_thenReturnsBeanNames() throws Exception {
7877
String[] beanNames = context.getBeanDefinitionNames();
7978

8079
List<String> beanNamesList = Arrays.asList(beanNames);
81-
assertTrue(beanNamesList.contains("personController"));
82-
assertTrue(beanNamesList.contains("person"));
80+
assertTrue(beanNamesList.contains("fooController"));
81+
assertTrue(beanNamesList.contains("fooService"));
8382
}
8483
}

0 commit comments

Comments
 (0)