BAEL-5451 Code for the Finding All Beans with a Custom Annotation article (#12143)

This commit is contained in:
thibaultfaure
2022-06-10 17:29:16 +02:00
committed by GitHub
parent a21841a9f1
commit fc3d29a9b4
16 changed files with 285 additions and 0 deletions
@@ -0,0 +1,28 @@
package com.baeldung.countingbeans.latestsspring;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Arrays;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class AnnotatedBeansIntegrationTest {
/**
* Note : this test fails with any spring version < 2.2
* Before, the getBeansWithAnnotation method was not checking the beans created via factory method
* Please find the change here : https://github.com/spring-projects/spring-framework/commit/e0fe32af05ac525ef5e11c3ac5195a08759bb85e
*/
@Test
void whenApplicationContextStarted_ThenShouldDetectAllAnnotatedBeans() {
try (AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext( MyComponent.class, MyConfigurationBean.class )) {
Map<String,Object> beans = applicationContext.getBeansWithAnnotation(MyCustomAnnotation.class);
assertEquals(2, beans.size());
assertTrue(beans.keySet().containsAll(Arrays.asList("myComponent", "myService")));
}
}
}
@@ -0,0 +1,47 @@
package com.baeldung.countingbeans.olderspring.factorybeans;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {MyConfigurationBean.class, AnnotatedBeansComponent.class})
public class AnnotatedBeansIntegrationTest {
@Autowired
AnnotatedBeansComponent annotatedBeansComponent;
@Test
void whenBeanUtilsGetBeansWithAnnotation_ThenShouldListAnnotatedBean() {
try (AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfigurationBean.class)) {
List<String> result = BeanUtils.getBeansWithAnnotation(applicationContext, MyCustomAnnotation.class);
assertEquals(1, result.size());
assertEquals("myService", result.get(0));
}
}
@Test
void whenBeanUtilsGetBeansWithAnnotationStreamVersion_ThenShouldListAnnotatedBean() {
try (AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfigurationBean.class)) {
List<String> result = BeanUtils.getBeansWithAnnotation(applicationContext, MyCustomAnnotation.class);
assertEquals(1, result.size());
assertEquals("myService", result.get(0));
}
}
@Test
void whenAnnotatedBeansComponentGetBeansWithAnnotation_ThenShouldListAnnotatedBean() {
List<String> result = annotatedBeansComponent.getBeansWithAnnotation(MyCustomAnnotation.class);
assertEquals(1, result.size());
assertEquals("myService", result.get(0));
}
}
@@ -0,0 +1,35 @@
package com.baeldung.countingbeans.olderspring.qualifier;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {MyComponent.class, MyConfigurationBean.class})
public class AnnotatedBeansIntegrationTest {
@Autowired
@MyCustomAnnotation
private List<Object> annotatedBeans;
@Test
void whenAutowiring_ThenShouldDetectAllAnnotatedBeans() {
assertEquals(2, annotatedBeans.size());
List<String> classNames = annotatedBeans.stream()
.map(Object::getClass)
.map(Class::getName)
.map(s -> s.substring(s.lastIndexOf(".") + 1))
.collect(Collectors.toList());
assertTrue(classNames.containsAll(Arrays.asList("MyComponent", "MyService")));
}
}