JAVA-84: Moved 1 article to spring-boot-di
This commit is contained in:
@@ -9,3 +9,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
||||
|
||||
- [Spring Component Scanning](https://www.baeldung.com/spring-component-scanning)
|
||||
- [Spring @ComponentScan – Filter Types](https://www.baeldung.com/spring-componentscan-filter-type)
|
||||
- [How to Get All Spring-Managed Beans?](https://www.baeldung.com/spring-show-all-beans)
|
||||
@@ -27,6 +27,12 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-tomcat</artifactId>
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
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);
|
||||
|
||||
displayAllBeans();
|
||||
}
|
||||
|
||||
public static void displayAllBeans() {
|
||||
String[] allBeanNames = applicationContext.getBeanDefinitionNames();
|
||||
for (String beanName : allBeanNames) {
|
||||
System.out.println(beanName);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.displayallbeans.controller;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
import com.baeldung.displayallbeans.service.FooService;
|
||||
|
||||
@Controller
|
||||
public class FooController {
|
||||
@Autowired
|
||||
private FooService fooService;
|
||||
|
||||
@GetMapping(value = "/displayallbeans")
|
||||
public ResponseEntity<String> getHeaderAndBody(Map<String, Object> model) {
|
||||
model.put("header", fooService.getHeader());
|
||||
model.put("message", fooService.getBody());
|
||||
return ResponseEntity.ok("displayallbeans");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
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,101 @@
|
||||
package com.baeldung.displayallbeans;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.BDDAssertions.then;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.actuate.beans.BeansEndpoint.ContextBeans;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.RequestEntity;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
@TestPropertySource(properties = { "management.port=0", "management.endpoints.web.exposure.include=*" })
|
||||
public class DisplayBeanIntegrationTest {
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Value("${local.management.port}")
|
||||
private int mgt;
|
||||
|
||||
@Autowired
|
||||
private TestRestTemplate testRestTemplate;
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext context;
|
||||
|
||||
private static final String ACTUATOR_PATH = "/actuator";
|
||||
|
||||
@Test
|
||||
public void givenRestTemplate_whenAccessServerUrl_thenHttpStatusOK() throws Exception {
|
||||
ResponseEntity<String> entity = this.testRestTemplate.getForEntity("http://localhost:" + this.port + "/displayallbeans", String.class);
|
||||
|
||||
then(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRestTemplate_whenAccessEndpointUrl_thenHttpStatusOK() throws Exception {
|
||||
ParameterizedTypeReference<Map<String, ContextBeans>> responseType = new ParameterizedTypeReference<Map<String, ContextBeans>>() {
|
||||
};
|
||||
RequestEntity<Void> requestEntity = RequestEntity.get(new URI("http://localhost:" + this.mgt + ACTUATOR_PATH + "/beans"))
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.build();
|
||||
ResponseEntity<Map<String, ContextBeans>> entity = this.testRestTemplate.exchange(requestEntity, responseType);
|
||||
|
||||
then(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRestTemplate_whenAccessEndpointUrl_thenReturnsBeanNames() throws Exception {
|
||||
RequestEntity<Void> requestEntity = RequestEntity.get(new URI("http://localhost:" + this.mgt + ACTUATOR_PATH + "/beans"))
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.build();
|
||||
ResponseEntity<BeanActuatorResponse> entity = this.testRestTemplate.exchange(requestEntity, BeanActuatorResponse.class);
|
||||
|
||||
Collection<String> beanNamesList = entity.getBody()
|
||||
.getBeans();
|
||||
|
||||
assertThat(beanNamesList).contains("fooController", "fooService");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWebApplicationContext_whenAccessGetBeanDefinitionNames_thenReturnsBeanNames() throws Exception {
|
||||
String[] beanNames = context.getBeanDefinitionNames();
|
||||
|
||||
List<String> beanNamesList = Arrays.asList(beanNames);
|
||||
assertThat(beanNamesList).contains("fooController", "fooService");
|
||||
}
|
||||
|
||||
private static class BeanActuatorResponse {
|
||||
private Map<String, Map<String, Map<String, Map<String, Object>>>> contexts;
|
||||
|
||||
public Collection<String> getBeans() {
|
||||
return this.contexts.get("application")
|
||||
.get("beans")
|
||||
.keySet();
|
||||
}
|
||||
|
||||
public Map<String, Map<String, Map<String, Map<String, Object>>>> getContexts() {
|
||||
return contexts;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user