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

View File

@ -0,0 +1,9 @@
package com.baeldung.countingbeans.latestsspring;
import org.springframework.stereotype.Component;
@Component
@MyCustomAnnotation
public class MyComponent {
}

View File

@ -0,0 +1,15 @@
package com.baeldung.countingbeans.latestsspring;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyConfigurationBean {
@Bean
@MyCustomAnnotation
MyService myService() {
return new MyService();
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.countingbeans.latestsspring;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention( RetentionPolicy.RUNTIME )
public @interface MyCustomAnnotation {
}

View File

@ -0,0 +1,5 @@
package com.baeldung.countingbeans.latestsspring;
public class MyService {
}

View File

@ -0,0 +1,19 @@
package com.baeldung.countingbeans.olderspring.factorybeans;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.stereotype.Component;
@Component
public class AnnotatedBeansComponent {
@Autowired
GenericApplicationContext applicationContext;
public List<String> getBeansWithAnnotation(Class<?> annotationClass) {
return BeanUtils.getBeansWithAnnotation(applicationContext, annotationClass);
}
}

View File

@ -0,0 +1,48 @@
package com.baeldung.countingbeans.olderspring.factorybeans;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
public class BeanUtils {
// NB : this method lists only beans created via factory methods
public static List<String> getBeansWithAnnotation(GenericApplicationContext applicationContext, Class<?> annotationClass) {
List<String> result = new ArrayList<String>();
ConfigurableListableBeanFactory factory = applicationContext.getBeanFactory();
for(String name : factory.getBeanDefinitionNames()) {
BeanDefinition bd = factory.getBeanDefinition(name);
if(bd.getSource() instanceof AnnotatedTypeMetadata) {
AnnotatedTypeMetadata metadata = (AnnotatedTypeMetadata) bd.getSource();
if (metadata.getAnnotationAttributes(annotationClass.getName()) != null) {
result.add(name);
}
}
}
return result;
}
// NB : list beans created via factory methods using streams (same method as before, written differently)
public static List<String> getBeansWithAnnotation_StreamVersion(GenericApplicationContext applicationContext, Class<?> annotationClass) {
ConfigurableListableBeanFactory factory = applicationContext.getBeanFactory();
return Arrays.stream(factory.getBeanDefinitionNames())
.filter(name -> isAnnotated(factory, name, annotationClass))
.collect(Collectors.toList());
}
private static boolean isAnnotated(ConfigurableListableBeanFactory factory, String beanName, Class<?> clazz) {
BeanDefinition beanDefinition = factory.getBeanDefinition(beanName);
if(beanDefinition.getSource() instanceof AnnotatedTypeMetadata) {
AnnotatedTypeMetadata metadata = (AnnotatedTypeMetadata) beanDefinition.getSource();
return metadata.getAnnotationAttributes(clazz.getName()) != null;
}
return false;
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.countingbeans.olderspring.factorybeans;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyConfigurationBean {
@Bean
@MyCustomAnnotation
MyService myService() {
return new MyService();
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.countingbeans.olderspring.factorybeans;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention( RetentionPolicy.RUNTIME )
public @interface MyCustomAnnotation {
}

View File

@ -0,0 +1,5 @@
package com.baeldung.countingbeans.olderspring.factorybeans;
public class MyService {
}

View File

@ -0,0 +1,9 @@
package com.baeldung.countingbeans.olderspring.qualifier;
import org.springframework.stereotype.Component;
@Component
@MyCustomAnnotation
public class MyComponent {
}

View File

@ -0,0 +1,15 @@
package com.baeldung.countingbeans.olderspring.qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyConfigurationBean {
@Bean
@MyCustomAnnotation
MyService myService() {
return new MyService();
}
}

View File

@ -0,0 +1,12 @@
package com.baeldung.countingbeans.olderspring.qualifier;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import org.springframework.beans.factory.annotation.Qualifier;
@Retention( RetentionPolicy.RUNTIME )
@Qualifier
public @interface MyCustomAnnotation {
}

View File

@ -0,0 +1,5 @@
package com.baeldung.countingbeans.olderspring.qualifier;
public class MyService {
}

View File

@ -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")));
}
}
}

View File

@ -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));
}
}

View File

@ -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")));
}
}