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:
ramansahasi
2017-06-13 20:43:16 +05:30
committed by maibin
parent 74aa2c9ec9
commit 69b4a05eb8
8 changed files with 60 additions and 58 deletions
@@ -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.";
}
}