BAEL-5451 Code for the Finding All Beans with a Custom Annotation article (#12143)
This commit is contained in:
+28
@@ -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")));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+47
@@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
+35
@@ -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")));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user