diff --git a/spring-boot/src/main/java/com/baeldung/displayallbeans/Application.java b/spring-boot/src/main/java/com/baeldung/displayallbeans/Application.java index fcce2725e5..cd015e6554 100644 --- a/spring-boot/src/main/java/com/baeldung/displayallbeans/Application.java +++ b/spring-boot/src/main/java/com/baeldung/displayallbeans/Application.java @@ -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); diff --git a/spring-boot/src/main/java/com/baeldung/displayallbeans/SpringConfig.java b/spring-boot/src/main/java/com/baeldung/displayallbeans/SpringConfig.java deleted file mode 100644 index 1d4a97f843..0000000000 --- a/spring-boot/src/main/java/com/baeldung/displayallbeans/SpringConfig.java +++ /dev/null @@ -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; - } -} \ No newline at end of file diff --git a/spring-boot/src/main/java/com/baeldung/displayallbeans/controller/FooController.java b/spring-boot/src/main/java/com/baeldung/displayallbeans/controller/FooController.java new file mode 100644 index 0000000000..9f483b18d1 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/displayallbeans/controller/FooController.java @@ -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 model){ + model.put("header", fooService.getHeader()); + model.put("message", fooService.getBody()); + return "displayallbeans"; + } +} 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 deleted file mode 100644 index 93fc278b1c..0000000000 --- a/spring-boot/src/main/java/com/baeldung/displayallbeans/controller/PersonController.java +++ /dev/null @@ -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; - } -} \ 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 deleted file mode 100644 index dde471aedb..0000000000 --- a/spring-boot/src/main/java/com/baeldung/displayallbeans/model/Person.java +++ /dev/null @@ -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; - } -} \ No newline at end of file diff --git a/spring-boot/src/main/java/com/baeldung/displayallbeans/service/FooService.java b/spring-boot/src/main/java/com/baeldung/displayallbeans/service/FooService.java new file mode 100644 index 0000000000..e29f18c94a --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/displayallbeans/service/FooService.java @@ -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."; + } + +} diff --git a/spring-boot/src/main/resources/templates/displayallbeans.html b/spring-boot/src/main/resources/templates/displayallbeans.html new file mode 100644 index 0000000000..5fc78a7fca --- /dev/null +++ b/spring-boot/src/main/resources/templates/displayallbeans.html @@ -0,0 +1,10 @@ + + + + Baeldung + + +

+

+ + diff --git a/spring-boot/src/test/java/com/baeldung/displayallbeans/DisplayBeanIntegrationTest.java b/spring-boot/src/test/java/com/baeldung/displayallbeans/DisplayBeanIntegrationTest.java index 84d5669aef..530b8e9cb3 100644 --- a/spring-boot/src/test/java/com/baeldung/displayallbeans/DisplayBeanIntegrationTest.java +++ b/spring-boot/src/test/java/com/baeldung/displayallbeans/DisplayBeanIntegrationTest.java @@ -44,9 +44,8 @@ public class DisplayBeanIntegrationTest { @Test public void givenRestTemplate_whenAccessServerUrl_thenHttpStatusOK() throws Exception { - @SuppressWarnings("rawtypes") - ResponseEntity entity = this.testRestTemplate.getForEntity( - "http://localhost:" + this.port + "/getPerson", Map.class); + ResponseEntity 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> 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")); + assertThat( beanNamesList, hasItem("fooController")); + assertThat( beanNamesList, hasItem("fooService")); } @Test @@ -78,7 +77,7 @@ public class DisplayBeanIntegrationTest { String[] beanNames = context.getBeanDefinitionNames(); List beanNamesList = Arrays.asList(beanNames); - assertTrue(beanNamesList.contains("personController")); - assertTrue(beanNamesList.contains("person")); + assertTrue(beanNamesList.contains("fooController")); + assertTrue(beanNamesList.contains("fooService")); } }