BAEL-3286 added component scan filter example (#7786)
* BAEL-3286 added component scan filter example * BAEL-3286 added component scan filter example * Fix alingemnt * Remove unused class * Corrected formatting * formatted code
This commit is contained in:
parent
ac8c5efb74
commit
7d65b1fb24
@ -16,6 +16,10 @@
|
|||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.aspectj</groupId>
|
||||||
|
<artifactId>aspectjweaver</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
@ -0,0 +1,10 @@
|
|||||||
|
package com.baeldung.componentscan.filter.annotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target(ElementType.TYPE)
|
||||||
|
public @interface Animal { }
|
@ -0,0 +1,21 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
package com.baeldung.componentscan.filter.annotation;
|
||||||
|
|
||||||
|
@Animal
|
||||||
|
public class Elephant { }
|
@ -0,0 +1,3 @@
|
|||||||
|
package com.baeldung.componentscan.filter.aspectj;
|
||||||
|
|
||||||
|
public class Cat { }
|
@ -0,0 +1,24 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,3 @@
|
|||||||
|
package com.baeldung.componentscan.filter.aspectj;
|
||||||
|
|
||||||
|
public class Elephant { }
|
@ -0,0 +1,3 @@
|
|||||||
|
package com.baeldung.componentscan.filter.aspectj;
|
||||||
|
|
||||||
|
public class Loin { }
|
@ -0,0 +1,3 @@
|
|||||||
|
package com.baeldung.componentscan.filter.assignable;
|
||||||
|
|
||||||
|
public interface Animal { }
|
@ -0,0 +1,3 @@
|
|||||||
|
package com.baeldung.componentscan.filter.assignable;
|
||||||
|
|
||||||
|
public class Cat implements Animal { }
|
@ -0,0 +1,21 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,3 @@
|
|||||||
|
package com.baeldung.componentscan.filter.assignable;
|
||||||
|
|
||||||
|
public class Elephant implements Animal { }
|
@ -0,0 +1,3 @@
|
|||||||
|
package com.baeldung.componentscan.filter.custom;
|
||||||
|
|
||||||
|
public class Cat extends Pet { }
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.baeldung.componentscan.filter.custom;
|
||||||
|
|
||||||
|
import org.springframework.core.type.ClassMetadata;
|
||||||
|
import org.springframework.core.type.classreading.MetadataReader;
|
||||||
|
import org.springframework.core.type.classreading.MetadataReaderFactory;
|
||||||
|
import org.springframework.core.type.filter.TypeFilter;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class ComponentScanCustomFilter implements TypeFilter {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
|
||||||
|
throws IOException {
|
||||||
|
ClassMetadata classMetadata = metadataReader.getClassMetadata();
|
||||||
|
String superClass = classMetadata.getSuperClassName();
|
||||||
|
if (Pet.class.getName().equalsIgnoreCase(superClass)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,3 @@
|
|||||||
|
package com.baeldung.componentscan.filter.custom;
|
||||||
|
|
||||||
|
public class Loin { }
|
@ -0,0 +1,3 @@
|
|||||||
|
package com.baeldung.componentscan.filter.custom;
|
||||||
|
|
||||||
|
public class Pet { }
|
@ -0,0 +1,3 @@
|
|||||||
|
package com.baeldung.componentscan.filter.regex;
|
||||||
|
|
||||||
|
public class Cat { }
|
@ -0,0 +1,20 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,3 @@
|
|||||||
|
package com.baeldung.componentscan.filter.regex;
|
||||||
|
|
||||||
|
public class Elephant { }
|
@ -0,0 +1,3 @@
|
|||||||
|
package com.baeldung.componentscan.filter.regex;
|
||||||
|
|
||||||
|
public class Loin { }
|
Loading…
x
Reference in New Issue
Block a user