BAEL-859 (updated) How to display/list all Spring-managed beans? (#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
This commit is contained in:
parent
74aa2c9ec9
commit
69b4a05eb8
|
@ -11,6 +11,10 @@ public class Application {
|
|||
public static void main(String[] args) {
|
||||
applicationContext = SpringApplication.run(Application.class, args);
|
||||
|
||||
displayAllBeans();
|
||||
}
|
||||
|
||||
public static void displayAllBeans() {
|
||||
String[] allBeanNames = applicationContext.getBeanDefinitionNames();
|
||||
for(String beanName : allBeanNames) {
|
||||
System.out.println(beanName);
|
||||
|
|
|
@ -1,19 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.displayallbeans.controller;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import com.baeldung.displayallbeans.service.FooService;
|
||||
|
||||
@Controller
|
||||
public class FooController {
|
||||
@Autowired
|
||||
private FooService fooService;
|
||||
|
||||
@RequestMapping(value="/displayallbeans")
|
||||
public String getHeaderAndBody (Map<String, Object> model){
|
||||
model.put("header", fooService.getHeader());
|
||||
model.put("message", fooService.getBody());
|
||||
return "displayallbeans";
|
||||
}
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
package com.baeldung.displayallbeans.model;
|
||||
|
||||
public class Person {
|
||||
String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.displayallbeans.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class FooService {
|
||||
|
||||
public String getHeader() {
|
||||
return "Display All Beans";
|
||||
}
|
||||
|
||||
public String getBody() {
|
||||
return "This is a sample application that displays all beans "
|
||||
+ "in Spring IoC container using ListableBeanFactory interface "
|
||||
+ "and Spring Boot Actuators.";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
|
||||
<title>Baeldung</title>
|
||||
</head>
|
||||
<body>
|
||||
<h2 th:text="${header}"></h2>
|
||||
<p th:text="${message}"></p>
|
||||
</body>
|
||||
</html>
|
|
@ -44,9 +44,8 @@ public class DisplayBeanIntegrationTest {
|
|||
|
||||
@Test
|
||||
public void givenRestTemplate_whenAccessServerUrl_thenHttpStatusOK() throws Exception {
|
||||
@SuppressWarnings("rawtypes")
|
||||
ResponseEntity<Map> entity = this.testRestTemplate.getForEntity(
|
||||
"http://localhost:" + this.port + "/getPerson", Map.class);
|
||||
ResponseEntity<String> entity = this.testRestTemplate.getForEntity(
|
||||
"http://localhost:" + this.port + "/displayallbeans", String.class);
|
||||
|
||||
then(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
}
|
||||
|
@ -69,8 +68,8 @@ public class DisplayBeanIntegrationTest {
|
|||
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"));
|
||||
assertThat( beanNamesList, hasItem("fooController"));
|
||||
assertThat( beanNamesList, hasItem("fooService"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -78,7 +77,7 @@ public class DisplayBeanIntegrationTest {
|
|||
String[] beanNames = context.getBeanDefinitionNames();
|
||||
|
||||
List<String> beanNamesList = Arrays.asList(beanNames);
|
||||
assertTrue(beanNamesList.contains("personController"));
|
||||
assertTrue(beanNamesList.contains("person"));
|
||||
assertTrue(beanNamesList.contains("fooController"));
|
||||
assertTrue(beanNamesList.contains("fooService"));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue