modifieds example to creates less cognitive load for a reader (#8018)

This commit is contained in:
Devender Kumar
2019-10-15 04:24:18 +02:00
committed by maibin
parent 10a7723cf7
commit 3e4c964e6a
7 changed files with 21 additions and 24 deletions

View File

@@ -5,10 +5,10 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType; import org.springframework.context.annotation.FilterType;
@Configuration @Configuration
@ComponentScan(includeFilters = @ComponentScan.Filter(type = FilterType.ASPECTJ, @ComponentScan(includeFilters = @ComponentScan.Filter(type = FilterType.ASPECTJ,
pattern = "com.baeldung.componentscan.filter.aspectj.* " pattern = "com.baeldung.componentscan.filter.aspectj.* "
+ "&& !(com.baeldung.componentscan.filter.aspectj.L* " + "&& !(com.baeldung.componentscan.filter.aspectj.L* "
+ "|| com.baeldung.componentscan.filter.aspectj.C*)")) + "|| com.baeldung.componentscan.filter.aspectj.C*)"))
public class ComponentScanAspectJFilterApp { public class ComponentScanAspectJFilterApp {
public static void main(String[] args) { public static void main(String[] args) {
} }

View File

@@ -1,4 +1,4 @@
package com.baeldung.componentscan.filter.custom; package com.baeldung.componentscan.filter.custom;
public class Cat extends Pet { public class Cat {
} }

View File

@@ -12,11 +12,8 @@ public class ComponentScanCustomFilter implements TypeFilter {
@Override @Override
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
ClassMetadata classMetadata = metadataReader.getClassMetadata(); ClassMetadata classMetadata = metadataReader.getClassMetadata();
String superClass = classMetadata.getSuperClassName(); String fullyQualifiedName = classMetadata.getClassName();
if (Pet.class.getName() String className = fullyQualifiedName.substring(fullyQualifiedName.lastIndexOf(".") + 1);
.equalsIgnoreCase(superClass)) { return className.length() > 5 ? true : false;
return true;
}
return false;
} }
} }

View File

@@ -1,4 +1,4 @@
package com.baeldung.componentscan.filter.custom; package com.baeldung.componentscan.filter.custom;
public class Pet { public class Elephant {
} }

View File

@@ -5,7 +5,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType; import org.springframework.context.annotation.FilterType;
@Configuration @Configuration
@ComponentScan(includeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = ".*[t]")) @ComponentScan(includeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = ".*(nt)"))
public class ComponentScanRegexFilterApp { public class ComponentScanRegexFilterApp {
public static void main(String[] args) { public static void main(String[] args) {

View File

@@ -1,18 +1,18 @@
package com.baeldung.componentscan.filter.custom; package com.baeldung.componentscan.filter.custom;
import static org.junit.Assert.assertThat;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(classes = ComponentScanCustomFilterApp.class) @SpringBootTest(classes = ComponentScanCustomFilterApp.class)
@@ -22,9 +22,10 @@ public class ComponentScanCustomFilterAppIntegrationTest {
public void whenCustomFilterIsUsed_thenComponentScanShouldRegisterBeanMatchingCustomFilter() { public void whenCustomFilterIsUsed_thenComponentScanShouldRegisterBeanMatchingCustomFilter() {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ComponentScanCustomFilterApp.class); ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ComponentScanCustomFilterApp.class);
List<String> beans = Arrays.stream(applicationContext.getBeanDefinitionNames()) List<String> beans = Arrays.stream(applicationContext.getBeanDefinitionNames())
.filter(bean -> !bean.contains("org.springframework") && !bean.contains("componentScanCustomFilterApp")) .filter(bean -> !bean.contains("org.springframework") && !bean.contains("componentScanCustomFilterApp")
.collect(Collectors.toList()); && !bean.contains("componentScanCustomFilter"))
.collect(Collectors.toList());
assertThat(beans.size(), equalTo(1)); assertThat(beans.size(), equalTo(1));
assertThat(beans.get(0), equalTo("cat")); assertThat(beans.get(0), equalTo("elephant"));
} }
} }

View File

@@ -24,8 +24,7 @@ public class ComponentScanRegexFilterAppIntegrationTest {
List<String> beans = Arrays.stream(applicationContext.getBeanDefinitionNames()) List<String> beans = Arrays.stream(applicationContext.getBeanDefinitionNames())
.filter(bean -> !bean.contains("org.springframework") && !bean.contains("componentScanRegexFilterApp")) .filter(bean -> !bean.contains("org.springframework") && !bean.contains("componentScanRegexFilterApp"))
.collect(Collectors.toList()); .collect(Collectors.toList());
assertThat(beans.size(), equalTo(2)); assertThat(beans.size(), equalTo(1));
assertThat(beans.contains("cat"), equalTo(true));
assertThat(beans.contains("elephant"), equalTo(true)); assertThat(beans.contains("elephant"), equalTo(true));
} }
} }