BAEL-859 How to display/list all Spring-managed beans? (#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.
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.displayallbeans;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
private static ApplicationContext applicationContext;
|
||||
|
||||
public static void main(String[] args) {
|
||||
applicationContext = SpringApplication.run(Application.class, args);
|
||||
|
||||
String[] allBeanNames = applicationContext.getBeanDefinitionNames();
|
||||
for(String beanName : allBeanNames) {
|
||||
System.out.println(beanName);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.displayallbeans.model;
|
||||
|
||||
public class Person {
|
||||
String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user