remove sys and added test cases (#7894)
* BAEL-3286 added component scan filter example * Remove unused class * Corrected formatting * formatted code * Update code * added test cases * Fix tests name
This commit is contained in:
parent
b86270a6aa
commit
56cae916aa
@ -36,6 +36,11 @@
|
|||||||
<artifactId>tomcat-embed-jasper</artifactId>
|
<artifactId>tomcat-embed-jasper</artifactId>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
@ -7,4 +7,5 @@ import java.lang.annotation.Target;
|
|||||||
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Target(ElementType.TYPE)
|
@Target(ElementType.TYPE)
|
||||||
public @interface Animal { }
|
public @interface Animal {
|
||||||
|
}
|
||||||
|
@ -1,21 +1,13 @@
|
|||||||
package com.baeldung.componentscan.filter.annotation;
|
package com.baeldung.componentscan.filter.annotation;
|
||||||
|
|
||||||
import org.springframework.context.ApplicationContext;
|
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.FilterType;
|
import org.springframework.context.annotation.FilterType;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@ComponentScan(includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Animal.class))
|
@ComponentScan(includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Animal.class))
|
||||||
public class ComponentScanAnnotationFilterApp {
|
public class ComponentScanAnnotationFilterApp {
|
||||||
private static ApplicationContext applicationContext;
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
applicationContext = new AnnotationConfigApplicationContext(ComponentScanAnnotationFilterApp.class);
|
|
||||||
Arrays.stream(applicationContext.getBeanDefinitionNames())
|
|
||||||
.forEach(System.out::println);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
package com.baeldung.componentscan.filter.annotation;
|
package com.baeldung.componentscan.filter.annotation;
|
||||||
|
|
||||||
@Animal
|
@Animal
|
||||||
public class Elephant { }
|
public class Elephant {
|
||||||
|
}
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
package com.baeldung.componentscan.filter.aspectj;
|
package com.baeldung.componentscan.filter.aspectj;
|
||||||
|
|
||||||
public class Cat { }
|
public class Cat {
|
||||||
|
}
|
||||||
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.baeldung.componentscan.filter.aspectj;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.FilterType;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@ComponentScan(includeFilters = @ComponentScan.Filter(type = FilterType.ASPECTJ,
|
||||||
|
pattern = "com.baeldung.componentscan.filter.aspectj.* "
|
||||||
|
+ "&& !(com.baeldung.componentscan.filter.aspectj.L* "
|
||||||
|
+ "|| com.baeldung.componentscan.filter.aspectj.C*)"))
|
||||||
|
public class ComponentScanAspectJFilterApp {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
}
|
||||||
|
}
|
@ -1,24 +0,0 @@
|
|||||||
package com.baeldung.componentscan.filter.aspectj;
|
|
||||||
|
|
||||||
import org.springframework.context.ApplicationContext;
|
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.context.annotation.FilterType;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
@ComponentScan(includeFilters = @ComponentScan.Filter(type = FilterType.ASPECTJ,
|
|
||||||
pattern = "com.baeldung.componentscan.filter.aspectj.* "
|
|
||||||
+ "&& !(com.baeldung.componentscan.filter.aspectj.L* "
|
|
||||||
+ "|| com.baeldung.componentscan.filter.aspectj.E*)"))
|
|
||||||
public class ComponentScanCustomFilterApp {
|
|
||||||
private static ApplicationContext applicationContext;
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
applicationContext = new AnnotationConfigApplicationContext(ComponentScanCustomFilterApp.class);
|
|
||||||
Arrays.stream(applicationContext.getBeanDefinitionNames())
|
|
||||||
.forEach(System.out::println);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,3 +1,4 @@
|
|||||||
package com.baeldung.componentscan.filter.aspectj;
|
package com.baeldung.componentscan.filter.aspectj;
|
||||||
|
|
||||||
public class Elephant { }
|
public class Elephant {
|
||||||
|
}
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
package com.baeldung.componentscan.filter.aspectj;
|
package com.baeldung.componentscan.filter.aspectj;
|
||||||
|
|
||||||
public class Loin { }
|
public class Loin {
|
||||||
|
}
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
package com.baeldung.componentscan.filter.assignable;
|
package com.baeldung.componentscan.filter.assignable;
|
||||||
|
|
||||||
public interface Animal { }
|
public interface Animal {
|
||||||
|
}
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
package com.baeldung.componentscan.filter.assignable;
|
package com.baeldung.componentscan.filter.assignable;
|
||||||
|
|
||||||
public class Cat implements Animal { }
|
public class Cat implements Animal {
|
||||||
|
}
|
||||||
|
@ -1,21 +1,13 @@
|
|||||||
package com.baeldung.componentscan.filter.assignable;
|
package com.baeldung.componentscan.filter.assignable;
|
||||||
|
|
||||||
import org.springframework.context.ApplicationContext;
|
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.FilterType;
|
import org.springframework.context.annotation.FilterType;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@ComponentScan(includeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = Animal.class))
|
@ComponentScan(includeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = Animal.class))
|
||||||
public class ComponentScanAssignableTypeFilterApp {
|
public class ComponentScanAssignableTypeFilterApp {
|
||||||
private static ApplicationContext applicationContext;
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
applicationContext = new AnnotationConfigApplicationContext(ComponentScanAssignableTypeFilterApp.class);
|
|
||||||
Arrays.stream(applicationContext.getBeanDefinitionNames())
|
|
||||||
.forEach(System.out::println);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
package com.baeldung.componentscan.filter.assignable;
|
package com.baeldung.componentscan.filter.assignable;
|
||||||
|
|
||||||
public class Elephant implements Animal { }
|
public class Elephant implements Animal {
|
||||||
|
}
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
package com.baeldung.componentscan.filter.custom;
|
package com.baeldung.componentscan.filter.custom;
|
||||||
|
|
||||||
public class Cat extends Pet { }
|
public class Cat extends Pet {
|
||||||
|
}
|
||||||
|
@ -10,11 +10,11 @@ import java.io.IOException;
|
|||||||
public class ComponentScanCustomFilter implements TypeFilter {
|
public class ComponentScanCustomFilter implements TypeFilter {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
|
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
|
||||||
throws IOException {
|
|
||||||
ClassMetadata classMetadata = metadataReader.getClassMetadata();
|
ClassMetadata classMetadata = metadataReader.getClassMetadata();
|
||||||
String superClass = classMetadata.getSuperClassName();
|
String superClass = classMetadata.getSuperClassName();
|
||||||
if (Pet.class.getName().equalsIgnoreCase(superClass)) {
|
if (Pet.class.getName()
|
||||||
|
.equalsIgnoreCase(superClass)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
@ -1,21 +1,13 @@
|
|||||||
package com.baeldung.componentscan.filter.custom;
|
package com.baeldung.componentscan.filter.custom;
|
||||||
|
|
||||||
import org.springframework.context.ApplicationContext;
|
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.FilterType;
|
import org.springframework.context.annotation.FilterType;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@ComponentScan(includeFilters = @ComponentScan.Filter(type = FilterType.CUSTOM, classes = ComponentScanCustomFilter.class))
|
@ComponentScan(includeFilters = @ComponentScan.Filter(type = FilterType.CUSTOM, classes = ComponentScanCustomFilter.class))
|
||||||
public class ComponentScanCustomFilterApp {
|
public class ComponentScanCustomFilterApp {
|
||||||
private static ApplicationContext applicationContext;
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
applicationContext = new AnnotationConfigApplicationContext(ComponentScanCustomFilterApp.class);
|
|
||||||
Arrays.stream(applicationContext.getBeanDefinitionNames())
|
|
||||||
.forEach(System.out::println);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
package com.baeldung.componentscan.filter.custom;
|
package com.baeldung.componentscan.filter.custom;
|
||||||
|
|
||||||
public class Loin { }
|
public class Loin {
|
||||||
|
}
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
package com.baeldung.componentscan.filter.custom;
|
package com.baeldung.componentscan.filter.custom;
|
||||||
|
|
||||||
public class Pet { }
|
public class Pet {
|
||||||
|
}
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
package com.baeldung.componentscan.filter.regex;
|
package com.baeldung.componentscan.filter.regex;
|
||||||
|
|
||||||
public class Cat { }
|
public class Cat {
|
||||||
|
}
|
||||||
|
@ -1,20 +1,13 @@
|
|||||||
package com.baeldung.componentscan.filter.regex;
|
package com.baeldung.componentscan.filter.regex;
|
||||||
|
|
||||||
import org.springframework.context.ApplicationContext;
|
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.FilterType;
|
import org.springframework.context.annotation.FilterType;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@ComponentScan(includeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = ".*[t]"))
|
@ComponentScan(includeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = ".*[t]"))
|
||||||
public class ComponentScanRegexFilterApp {
|
public class ComponentScanRegexFilterApp {
|
||||||
private static ApplicationContext applicationContext;
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
applicationContext = new AnnotationConfigApplicationContext(ComponentScanRegexFilterApp.class);
|
|
||||||
Arrays.stream(applicationContext.getBeanDefinitionNames()).forEach(System.out::println);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
package com.baeldung.componentscan.filter.regex;
|
package com.baeldung.componentscan.filter.regex;
|
||||||
|
|
||||||
public class Elephant { }
|
public class Elephant {
|
||||||
|
}
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
package com.baeldung.componentscan.filter.regex;
|
package com.baeldung.componentscan.filter.regex;
|
||||||
|
|
||||||
public class Loin { }
|
public class Loin {
|
||||||
|
}
|
||||||
|
@ -0,0 +1,30 @@
|
|||||||
|
package com.baeldung.componentscan.filter.annotation;
|
||||||
|
|
||||||
|
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.runner.RunWith;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
import static org.hamcrest.CoreMatchers.*;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(classes = ComponentScanAnnotationFilterApp.class)
|
||||||
|
public class ComponentScanAnnotationFilterAppIntegrationTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBean() {
|
||||||
|
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ComponentScanAnnotationFilterApp.class);
|
||||||
|
List<String> beans = Arrays.stream(applicationContext.getBeanDefinitionNames())
|
||||||
|
.filter(bean -> !bean.contains("org.springframework") && !bean.contains("componentScanAnnotationFilterApp"))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
assertThat(beans.size(), equalTo(1));
|
||||||
|
assertThat(beans.get(0), equalTo("elephant"));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
package com.baeldung.componentscan.filter.aspectj;
|
||||||
|
|
||||||
|
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.runner.RunWith;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
import static org.hamcrest.CoreMatchers.*;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(classes = ComponentScanAspectJFilterApp.class)
|
||||||
|
public class ComponentScanAspectJFilterAppIntegrationTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBean() {
|
||||||
|
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ComponentScanAspectJFilterApp.class);
|
||||||
|
List<String> beans = Arrays.stream(applicationContext.getBeanDefinitionNames())
|
||||||
|
.filter(bean -> !bean.contains("org.springframework") && !bean.contains("componentScanCustomFilterApp"))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
assertThat(beans.size(), equalTo(1));
|
||||||
|
assertThat(beans.get(0), equalTo("elephant"));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
package com.baeldung.componentscan.filter.assignable;
|
||||||
|
|
||||||
|
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.runner.RunWith;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
import static org.hamcrest.CoreMatchers.*;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(classes = ComponentScanAssignableTypeFilterApp.class)
|
||||||
|
public class ComponentScanAssignableTypeFilterAppIntegrationTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBean() {
|
||||||
|
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ComponentScanAssignableTypeFilterApp.class);
|
||||||
|
List<String> beans = Arrays.stream(applicationContext.getBeanDefinitionNames())
|
||||||
|
.filter(bean -> !bean.contains("org.springframework") && !bean.contains("componentScanAssignableTypeFilterApp"))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
assertThat(beans.size(), equalTo(2));
|
||||||
|
assertThat(beans.contains("cat"), equalTo(true));
|
||||||
|
assertThat(beans.contains("elephant"), equalTo(true));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
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.runner.RunWith;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
import static org.hamcrest.CoreMatchers.*;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(classes = ComponentScanCustomFilterApp.class)
|
||||||
|
public class ComponentScanCustomFilterAppIntegrationTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBean() {
|
||||||
|
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ComponentScanCustomFilterApp.class);
|
||||||
|
List<String> beans = Arrays.stream(applicationContext.getBeanDefinitionNames())
|
||||||
|
.filter(bean -> !bean.contains("org.springframework") && !bean.contains("componentScanCustomFilterApp"))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
assertThat(beans.size(), equalTo(1));
|
||||||
|
assertThat(beans.get(0), equalTo("cat"));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
package com.baeldung.componentscan.filter.regex;
|
||||||
|
|
||||||
|
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.runner.RunWith;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
import static org.hamcrest.CoreMatchers.*;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(classes = ComponentScanRegexFilterApp.class)
|
||||||
|
public class ComponentScanRegexFilterAppIntegrationTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBean() {
|
||||||
|
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ComponentScanRegexFilterApp.class);
|
||||||
|
List<String> beans = Arrays.stream(applicationContext.getBeanDefinitionNames())
|
||||||
|
.filter(bean -> !bean.contains("org.springframework") && !bean.contains("componentScanRegexFilterApp"))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
assertThat(beans.size(), equalTo(2));
|
||||||
|
assertThat(beans.contains("cat"), equalTo(true));
|
||||||
|
assertThat(beans.contains("elephant"), equalTo(true));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user