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:
Devender Kumar 2019-09-20 08:09:27 +02:00 committed by maibin
parent ac8c5efb74
commit 7d65b1fb24
21 changed files with 227 additions and 44 deletions

View File

@ -1,55 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot-di</artifactId>
<name>spring-boot-di</name>
<packaging>jar</packaging>
<description>Module For Spring Boot DI</description>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot-di</artifactId>
<name>spring-boot-di</name>
<packaging>jar</packaging>
<description>Module For Spring Boot DI</description>
<parent>
<artifactId>parent-boot-2</artifactId>
<groupId>com.baeldung</groupId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-boot-2</relativePath>
</parent>
<parent>
<artifactId>parent-boot-2</artifactId>
<groupId>com.baeldung</groupId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-boot-2</relativePath>
</parent>
<dependencies>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.baeldung.SpringBootDiApplication</mainClass>
<layout>JAR</layout>
</configuration>
</plugin>
</plugins>
</build>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.baeldung.SpringBootDiApplication</mainClass>
<layout>JAR</layout>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<start-class>com.baeldung.SpringBootDiApplication</start-class>
</properties>
<properties>
<start-class>com.baeldung.SpringBootDiApplication</start-class>
</properties>
</project>

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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