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:
Devender Kumar 2019-09-29 17:38:58 +02:00 committed by maibin
parent b86270a6aa
commit 56cae916aa
29 changed files with 210 additions and 79 deletions

View File

@ -36,6 +36,11 @@
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

View File

@ -7,4 +7,5 @@ import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Animal { }
public @interface Animal {
}

View File

@ -1,21 +1,13 @@
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.Configuration;
import org.springframework.context.annotation.FilterType;
import java.util.Arrays;
@Configuration
@ComponentScan(includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Animal.class))
public class ComponentScanAnnotationFilterApp {
private static ApplicationContext applicationContext;
public static void main(String[] args) {
applicationContext = new AnnotationConfigApplicationContext(ComponentScanAnnotationFilterApp.class);
Arrays.stream(applicationContext.getBeanDefinitionNames())
.forEach(System.out::println);
}
}

View File

@ -1,4 +1,5 @@
package com.baeldung.componentscan.filter.annotation;
@Animal
public class Elephant { }
public class Elephant {
}

View File

@ -1,3 +1,4 @@
package com.baeldung.componentscan.filter.aspectj;
public class Cat { }
public class Cat {
}

View File

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

View File

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

View File

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

View File

@ -1,3 +1,4 @@
package com.baeldung.componentscan.filter.aspectj;
public class Loin { }
public class Loin {
}

View File

@ -1,3 +1,4 @@
package com.baeldung.componentscan.filter.assignable;
public interface Animal { }
public interface Animal {
}

View File

@ -1,3 +1,4 @@
package com.baeldung.componentscan.filter.assignable;
public class Cat implements Animal { }
public class Cat implements Animal {
}

View File

@ -1,21 +1,13 @@
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.Configuration;
import org.springframework.context.annotation.FilterType;
import java.util.Arrays;
@Configuration
@ComponentScan(includeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = Animal.class))
public class ComponentScanAssignableTypeFilterApp {
private static ApplicationContext applicationContext;
public static void main(String[] args) {
applicationContext = new AnnotationConfigApplicationContext(ComponentScanAssignableTypeFilterApp.class);
Arrays.stream(applicationContext.getBeanDefinitionNames())
.forEach(System.out::println);
}
}

View File

@ -1,3 +1,4 @@
package com.baeldung.componentscan.filter.assignable;
public class Elephant implements Animal { }
public class Elephant implements Animal {
}

View File

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

View File

@ -10,11 +10,11 @@ import java.io.IOException;
public class ComponentScanCustomFilter implements TypeFilter {
@Override
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
throws IOException {
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
ClassMetadata classMetadata = metadataReader.getClassMetadata();
String superClass = classMetadata.getSuperClassName();
if (Pet.class.getName().equalsIgnoreCase(superClass)) {
if (Pet.class.getName()
.equalsIgnoreCase(superClass)) {
return true;
}
return false;

View File

@ -1,21 +1,13 @@
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.Configuration;
import org.springframework.context.annotation.FilterType;
import java.util.Arrays;
@Configuration
@ComponentScan(includeFilters = @ComponentScan.Filter(type = FilterType.CUSTOM, classes = ComponentScanCustomFilter.class))
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);
}
}

View File

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

View File

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

View File

@ -1,3 +1,4 @@
package com.baeldung.componentscan.filter.regex;
public class Cat { }
public class Cat {
}

View File

@ -1,20 +1,13 @@
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.Configuration;
import org.springframework.context.annotation.FilterType;
import java.util.Arrays;
@Configuration
@ComponentScan(includeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = ".*[t]"))
public class ComponentScanRegexFilterApp {
private static ApplicationContext applicationContext;
public static void main(String[] args) {
applicationContext = new AnnotationConfigApplicationContext(ComponentScanRegexFilterApp.class);
Arrays.stream(applicationContext.getBeanDefinitionNames()).forEach(System.out::println);
}
}

View File

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

View File

@ -1,3 +1,4 @@
package com.baeldung.componentscan.filter.regex;
public class Loin { }
public class Loin {
}

View File

@ -10,10 +10,10 @@ import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
//@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = Rose.class))
//@ComponentScan(basePackages = "com.baeldung.componentscan.springapp")
//@ComponentScan(basePackages = "com.baeldung.componentscan.springapp.animals")
//@ComponentScan (excludeFilters = @ComponentScan.Filter(type=FilterType.REGEX,pattern="com\\.baeldung\\.componentscan\\.springapp\\.flowers\\..*"))
// @ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = Rose.class))
// @ComponentScan(basePackages = "com.baeldung.componentscan.springapp")
// @ComponentScan(basePackages = "com.baeldung.componentscan.springapp.animals")
// @ComponentScan (excludeFilters = @ComponentScan.Filter(type=FilterType.REGEX,pattern="com\\.baeldung\\.componentscan\\.springapp\\.flowers\\..*"))
public class SpringComponentScanApp {
private static ApplicationContext applicationContext;

View File

@ -8,9 +8,9 @@ import org.springframework.context.annotation.Bean;
import com.baeldung.componentscan.ExampleBean;
@SpringBootApplication
//@ComponentScan(basePackages = "com.baeldung.componentscan.springbootapp.animals")
//@ComponentScan ( excludeFilters = @ComponentScan.Filter(type=FilterType.REGEX,pattern="com\\.baeldung\\.componentscan\\.springbootapp\\.flowers\\..*"))
//@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = Rose.class))
// @ComponentScan(basePackages = "com.baeldung.componentscan.springbootapp.animals")
// @ComponentScan ( excludeFilters = @ComponentScan.Filter(type=FilterType.REGEX,pattern="com\\.baeldung\\.componentscan\\.springbootapp\\.flowers\\..*"))
// @ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = Rose.class))
public class SpringBootComponentScanApp {
private static ApplicationContext applicationContext;

View File

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

View File

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

View File

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

View File

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

View File

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